blob: ef622a68f4112afe874ffce1302768e92922e140 [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 Hofmeyr334af5d2015-11-17 14:24:46 +0100917 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200918 (int)nrm->expiry_entry.expiry,
919 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100920 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200921
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 Hofmeyr334af5d2015-11-17 14:24:46 +0100963 LOG(LOGL_DEBUG, "New %s TEI: (from %s, TEI %u) <-- TEI %u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200964 gtphub_plane_idx_names[plane_idx],
965 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +0100966 (unsigned int)orig_tei, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200967
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
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001020static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
1021 struct gtphub *hub,
1022 struct gtp_packet_desc *p,
1023 struct gtphub_peer_port *from_port)
1024{
1025 OSMO_ASSERT(p->version == 1);
1026 *to_port_p = NULL;
1027
1028 /* If the header's TEI is zero, no PDP context has been established
1029 * yet. If nonzero, a mapping should actually already exist for this
1030 * TEI, since it must have been announced in a PDP context creation. */
1031 uint32_t tei = p->header_tei;
1032 if (!tei)
1033 return 0;
1034
1035 /* to_peer has previously announced a TEI, which was stored and
1036 * mapped in from_peer's tei_map. */
1037 struct nr_mapping *nrm;
1038 nrm = nr_map_get_inv(&hub->tei_map[p->plane_idx], tei);
1039 if (!nrm) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001040 LOG(LOGL_ERROR, "Received unknown TEI %" PRIu32 " from %s\n",
1041 tei, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001042 return -1;
1043 }
1044
1045 struct gtphub_peer_port *to_port = nrm->origin;
1046 uint32_t unmapped_tei = nrm->orig;
1047 set_tei(p, unmapped_tei);
1048
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001049 LOG(LOGL_DEBUG, "Unmapped TEI coming from %s: %u -> %u (to %s)\n",
1050 gtphub_port_str(from_port),
1051 (unsigned int)tei, (unsigned int)unmapped_tei,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001052 gtphub_port_str2(to_port));
1053
1054 *to_port_p = to_port;
1055 return 0;
1056}
1057
1058/* Read GSN address IEs from p, and make sure these peer addresses exist in
1059 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1060 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1061 * the packet p. */
1062static int gtphub_handle_pdp_ctx_ies(struct gtphub *hub,
1063 struct gtphub_bind from_bind[],
1064 struct gtphub_bind to_bind[],
1065 struct gtp_packet_desc *p,
1066 time_t now)
1067{
1068 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1069
1070 int rc;
1071 int plane_idx;
1072
1073 switch (p->type) {
1074 case GTP_CREATE_PDP_REQ:
1075 case GTP_CREATE_PDP_RSP:
1076 /* Go for it below */
1077 break;
1078 default:
1079 /* Nothing to do for this message type. */
1080 return 0;
1081 }
1082
1083 /* TODO enforce a Request only from SGSN, a Response only from GGSN? */
1084
1085 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1086 plane_nrs_match_GSN_addr_IE_indices);
1087
1088 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1089 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
1090
1091 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
1092 struct gsn_addr addr_from_ie;
1093 uint32_t tei_from_ie;
1094 int ie_idx;
1095
1096 /* Fetch GSN Address and TEI from IEs */
1097 rc = gsn_addr_get(&addr_from_ie, p, plane_idx);
1098 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001099 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1100 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001101 return -1;
1102 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001103 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001104 gtphub_plane_idx_names[plane_idx],
1105 gsn_addr_to_str(&addr_from_ie),
1106 addr_from_ie.len);
1107
1108 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1109 if (ie_idx < 0) {
1110 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001111 LOG(LOGL_ERROR,
1112 "Create PDP Context message invalid:"
1113 " missing IE %d\n",
1114 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001115 return -1;
1116 }
1117 tei_from_ie = 0;
1118 }
1119 else
1120 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1121
1122 /* Make sure an entry for this peer address with default port
1123 * exists */
1124 struct gtphub_peer_port *peer_from_ie =
1125 gtphub_port_have(hub, &from_bind[plane_idx],
1126 &addr_from_ie,
1127 gtphub_plane_idx_default_port[plane_idx]);
1128
1129 if (tei_from_ie) {
1130 /* Create TEI mapping and replace in GTP packet IE */
1131 uint32_t mapped_tei =
1132 gtphub_tei_mapping_have(hub, plane_idx,
1133 peer_from_ie,
1134 tei_from_ie,
1135 now);
1136 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
1137 }
1138
1139 /* Replace the GSN address to reflect gtphub. */
1140 rc = gsn_addr_put(&to_bind[plane_idx].local_addr, p, plane_idx);
1141 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001142 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1143 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001144 return -1;
1145 }
1146 }
1147
1148 return 0;
1149}
1150
1151static int gtphub_write(const struct osmo_fd *to,
1152 const struct osmo_sockaddr *to_addr,
1153 const uint8_t *buf, size_t buf_len)
1154{
1155 errno = 0;
1156 ssize_t sent = sendto(to->fd, buf, buf_len, 0,
1157 (struct sockaddr*)&to_addr->a, to_addr->l);
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001158 LOG(LOGL_DEBUG, "to %s\n", osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001159
1160 if (sent == -1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001161 LOG(LOGL_ERROR, "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001162 return -EINVAL;
1163 }
1164
1165 if (sent != buf_len)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001166 LOG(LOGL_ERROR, "sent(%d) != data_len(%d)\n",
1167 (int)sent, (int)buf_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001168 else
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001169 LOG(LOGL_DEBUG, "Sent %d\n%s\n",
1170 (int)sent, osmo_hexdump(buf, sent));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001171
1172 return 0;
1173}
1174
1175static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1176{
1177 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1178 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001179 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1180 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001181 if (!(what & BSC_FD_READ))
1182 return 0;
1183
1184 struct gtphub *hub = from_ggsns_ofd->data;
1185
1186 static uint8_t buf[4096];
1187 struct osmo_sockaddr from_addr;
1188 struct osmo_sockaddr to_addr;
1189 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001190 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001191 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001192
1193 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1194 if (len < 1)
1195 return 0;
1196
1197 len = gtphub_from_ggsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1198 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001199 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001200 if (len < 1)
1201 return 0;
1202
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001203 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001204}
1205
1206static int gtphub_unmap(struct gtphub *hub,
1207 struct gtp_packet_desc *p,
1208 struct gtphub_peer_port *from,
1209 struct gtphub_peer_port *to_proxy,
1210 struct gtphub_peer_port **final_unmapped,
1211 struct gtphub_peer_port **unmapped_from_seq,
1212 struct gtphub_peer_port **unmapped_from_tei)
1213{
1214 /* Always (try to) unmap sequence and TEI numbers, which need to be
1215 * replaced in the packet. Either way, give precedence to the proxy, if
1216 * configured. */
1217
1218 struct gtphub_peer_port *from_seq = NULL;
1219 struct gtphub_peer_port *from_tei = NULL;
1220 struct gtphub_peer_port *unmapped = NULL;
1221
1222 if (unmapped_from_seq)
1223 *unmapped_from_seq = from_seq;
1224 if (unmapped_from_tei)
1225 *unmapped_from_tei = from_tei;
1226 if (final_unmapped)
1227 *final_unmapped = unmapped;
1228
1229 from_seq = gtphub_unmap_seq(p, from);
1230
1231 if (gtphub_unmap_header_tei(&from_tei, hub, p, from) < 0)
1232 return -1;
1233
1234 struct gtphub_peer *from_peer = from->peer_addr->peer;
1235 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001236 LOG(LOGL_DEBUG,
1237 "Seq unmap and TEI unmap yield two different peers."
1238 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001239 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1240 gtphub_plane_idx_names[p->plane_idx],
1241 gtphub_peer_str(from_peer),
1242 (int)p->seq,
1243 gtphub_port_str(from_seq),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001244 (unsigned int)p->header_tei,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001245 gtphub_port_str2(from_tei)
1246 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001247 }
1248 unmapped = (from_seq? from_seq : from_tei);
1249
1250 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001251 LOG(LOGL_NOTICE,
1252 "Unmap yields a different peer than the configured proxy."
1253 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001254 " unmapped: %s proxy: %s\n",
1255 gtphub_port_str(unmapped),
1256 gtphub_port_str2(to_proxy)
1257 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001258 }
1259 unmapped = (to_proxy? to_proxy : unmapped);
1260
1261 if (!unmapped) {
1262 /* Return no error, but returned pointers are all NULL. */
1263 return 0;
1264 }
1265
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001266 LOG(LOGL_DEBUG, "from seq %p; from tei %p; unmapped => %p\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001267 from_seq, from_tei, unmapped);
1268
1269 if (unmapped_from_seq)
1270 *unmapped_from_seq = from_seq;
1271 if (unmapped_from_tei)
1272 *unmapped_from_tei = from_tei;
1273 if (final_unmapped)
1274 *final_unmapped = unmapped;
1275 return 0;
1276}
1277
1278static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1279 uint16_t port,
1280 struct osmo_sockaddr *dst)
1281{
1282 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
1283}
1284
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001285/* If p is an Echo request, replace p's data with the matching response and
1286 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
1287 * detected. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001288static int gtphub_handle_echo(struct gtphub *hub, struct gtp_packet_desc *p,
1289 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001290{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001291 if (p->type != GTP_ECHO_REQ)
1292 return 0;
1293
1294 static uint8_t echo_response_data[14] = {
1295 0x32, /* flags */
1296 GTP_ECHO_RSP,
1297 0x00, 14 - 8, /* Length in network byte order */
1298 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
1299 0, 0, /* Seq, to be replaced */
1300 0, 0, /* no extensions */
1301 0x0e, /* Recovery IE */
1302 0 /* Recovery counter, to be replaced */
1303 };
1304 uint16_t *seq = (uint16_t*)&echo_response_data[8];
1305 uint8_t *recovery = &echo_response_data[13];
1306
1307 *seq = hton16(p->seq);
1308 *recovery = hub->restart_counter;
1309
1310 *reply_buf = echo_response_data;
1311
1312 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001313}
1314
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001315struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
1316 const struct osmo_sockaddr *addr);
1317
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001318/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
1319 * address to forward to. Return a pointer to the osmo_fd, but copy the
1320 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
1321 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
1322 * bytes to forward, 0 or less on failure. */
1323int gtphub_from_ggsns_handle_buf(struct gtphub *hub,
1324 unsigned int plane_idx,
1325 const struct osmo_sockaddr *from_addr,
1326 uint8_t *buf,
1327 size_t received,
1328 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001329 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001330 struct osmo_fd **to_ofd,
1331 struct osmo_sockaddr *to_addr)
1332{
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001333 LOG(LOGL_DEBUG, "<- rx %s from GGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001334 gtphub_plane_idx_names[plane_idx],
1335 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001336
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001337 static struct gtp_packet_desc p;
1338 gtp_decode(buf, received, plane_idx, &p);
1339
1340 if (p.rc <= 0)
1341 return -1;
1342
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001343 int reply_len;
1344 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1345 if (reply_len > 0) {
1346 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001347 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001348 *to_ofd = &hub->to_ggsns[plane_idx].ofd;
1349 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001350 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001351 if (reply_len < 0)
1352 return -1;
1353
1354 *to_ofd = &hub->to_sgsns[plane_idx].ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001355
1356 /* If a GGSN proxy is configured, check that it's indeed that proxy
1357 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1358 * gtphub, so no-one else is allowed to talk to us from that side. */
1359 struct gtphub_peer_port *ggsn = hub->ggsn_proxy[plane_idx];
1360 if (ggsn) {
1361 if (osmo_sockaddr_cmp(&ggsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001362 LOG(LOGL_ERROR,
1363 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001364 " received on GGSN bind is from another sender:"
1365 " proxy: %s sender: %s\n",
1366 gtphub_port_str(ggsn),
1367 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001368 return -1;
1369 }
1370 }
1371
1372 if (!ggsn) {
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001373 /* Find a GGSN peer with a matching address. The sender's port
1374 * may in fact differ. */
1375 ggsn = gtphub_known_addr_have_port(&hub->to_ggsns[plane_idx],
1376 from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001377 }
1378
1379 /* If any PDP context has been created, we already have an entry for
1380 * this GGSN. If we don't have an entry, the GGSN has nothing to tell
1381 * us about. */
1382 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001383 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
1384 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001385 return -1;
1386 }
1387
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001388 LOG(LOGL_DEBUG, "GGSN peer: %s\n", gtphub_port_str(ggsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001389
1390 struct gtphub_peer_port *sgsn_from_seq;
1391 struct gtphub_peer_port *sgsn;
1392 if (gtphub_unmap(hub, &p, ggsn,
1393 hub->sgsn_proxy[plane_idx],
1394 &sgsn, &sgsn_from_seq,
1395 NULL /* not interested, got it in &sgsn already */
1396 )
1397 != 0) {
1398 return -1;
1399 }
1400
1401 if (!sgsn) {
1402 /* A GGSN initiated request would go to a known TEI. So this is
1403 * bogus. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001404 LOG(LOGL_ERROR, "No SGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001405 return -1;
1406 }
1407
1408 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001409 /* This may be a Create PDP Context response. If it is, there
1410 * are other addresses in the GTP message to set up apart from
1411 * the sender. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001412 if (gtphub_handle_pdp_ctx_ies(hub, hub->to_ggsns,
1413 hub->to_sgsns, &p, now)
1414 != 0)
1415 return -1;
1416 }
1417
1418 gtphub_check_restart_counter(hub, &p, ggsn);
1419 gtphub_map_restart_counter(hub, &p, ggsn, sgsn);
1420
1421 /* If the GGSN is replying to an SGSN request, the sequence nr has
1422 * already been unmapped above (sgsn_from_seq != NULL), and we need not
1423 * create a new mapping. */
1424 if (!sgsn_from_seq)
1425 gtphub_map_seq(&p, ggsn, sgsn, now);
1426
1427 osmo_sockaddr_copy(to_addr, &sgsn->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001428
1429 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001430
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001431 LOG(LOGL_DEBUG, "<-- Forward to SGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001432 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001433 return received;
1434}
1435
1436static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1437{
1438 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1439 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001440 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1441 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001442
1443 if (!(what & BSC_FD_READ))
1444 return 0;
1445
1446 struct gtphub *hub = from_sgsns_ofd->data;
1447
1448 static uint8_t buf[4096];
1449 struct osmo_sockaddr from_addr;
1450 struct osmo_sockaddr to_addr;
1451 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001452 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001453 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001454
1455 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1456 if (len < 1)
1457 return 0;
1458
1459 len = gtphub_from_sgsns_handle_buf(hub, plane_idx, &from_addr, buf, len,
1460 gtphub_now(),
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001461 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001462 if (len < 1)
1463 return 0;
1464
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001465 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001466}
1467
1468/* Analogous to gtphub_from_ggsns_handle_buf(), see the comment there. */
1469int gtphub_from_sgsns_handle_buf(struct gtphub *hub,
1470 unsigned int plane_idx,
1471 const struct osmo_sockaddr *from_addr,
1472 uint8_t *buf,
1473 size_t received,
1474 time_t now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001475 uint8_t **reply_buf,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001476 struct osmo_fd **to_ofd,
1477 struct osmo_sockaddr *to_addr)
1478{
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001479 LOG(LOGL_DEBUG, "-> rx %s from SGSN %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001480 gtphub_plane_idx_names[plane_idx],
1481 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001482
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001483 static struct gtp_packet_desc p;
1484 gtp_decode(buf, received, plane_idx, &p);
1485
1486 if (p.rc <= 0)
1487 return -1;
1488
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001489 int reply_len;
1490 reply_len = gtphub_handle_echo(hub, &p, reply_buf);
1491 if (reply_len > 0) {
1492 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001493 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyr6187e012015-11-20 00:57:05 +01001494 *to_ofd = &hub->to_sgsns[plane_idx].ofd;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001495 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001496 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001497 if (reply_len < 0)
1498 return -1;
1499
1500 *to_ofd = &hub->to_ggsns[plane_idx].ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001501
1502 /* If an SGSN proxy is configured, check that it's indeed that proxy
1503 * talking to us. A proxy is a forced 1:1 connection, e.g. to another
1504 * gtphub, so no-one else is allowed to talk to us from that side. */
1505 struct gtphub_peer_port *sgsn = hub->sgsn_proxy[plane_idx];
1506 if (sgsn) {
1507 if (osmo_sockaddr_cmp(&sgsn->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001508 LOG(LOGL_ERROR,
1509 "Rejecting: GGSN proxy configured, but GTP packet"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001510 " received on GGSN bind is from another sender:"
1511 " proxy: %s sender: %s\n",
1512 gtphub_port_str(sgsn),
1513 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001514 return -1;
1515 }
1516 }
1517
1518 if (!sgsn) {
1519 /* If any contact has been made before, we already have an
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001520 * entry for this SGSN. The port may differ. */
1521 sgsn = gtphub_known_addr_have_port(&hub->to_sgsns[plane_idx],
1522 from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001523 }
1524
1525 if (!sgsn) {
1526 /* A new peer. If this is on the Ctrl plane, an SGSN may make
1527 * first contact without being known yet, so create the peer
1528 * struct for the current sender. */
1529 if (plane_idx != GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001530 LOG(LOGL_ERROR,
1531 "User plane peer was not announced by PDP Context,"
1532 " discarding: %s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001533 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001534 return -1;
1535 }
1536
1537 struct gsn_addr from_gsna;
1538 uint16_t from_port;
1539 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
1540 return -1;
1541
1542 sgsn = gtphub_port_have(hub, &hub->to_sgsns[plane_idx],
1543 &from_gsna, from_port);
1544 }
1545
1546 if (!sgsn) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001547 /* This could theoretically happen for invalid address data or
1548 * somesuch. */
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001549 LOG(LOGL_ERROR, "Dropping packet: invalid SGSN peer: %s\n",
1550 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001551 return -1;
1552 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001553 LOG(LOGL_DEBUG, "SGSN peer: %s\n", gtphub_port_str(sgsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001554
1555 struct gtphub_peer_port *ggsn_from_seq;
1556 struct gtphub_peer_port *ggsn;
1557 if (gtphub_unmap(hub, &p, sgsn,
1558 hub->ggsn_proxy[plane_idx],
1559 &ggsn, &ggsn_from_seq,
1560 NULL /* not interested, got it in &ggsn already */
1561 )
1562 != 0) {
1563 return -1;
1564 }
1565
Neels Hofmeyra208c732015-11-11 19:26:09 +01001566 if (!ggsn) {
1567 if (gtphub_resolve_ggsn(hub, &p, &ggsn) < 0)
1568 return -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001569 }
1570
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001571 if (!ggsn) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001572 LOG(LOGL_ERROR, "No GGSN to send to. Dropping packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001573 return -1;
1574 }
1575
1576 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001577 /* This may be a Create PDP Context requst. If it is, there are
1578 * other addresses in the GTP message to set up apart from the
1579 * sender. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001580 if (gtphub_handle_pdp_ctx_ies(hub, hub->to_sgsns,
1581 hub->to_ggsns, &p, now)
1582 != 0)
1583 return -1;
1584 }
1585
1586 gtphub_check_restart_counter(hub, &p, sgsn);
1587 gtphub_map_restart_counter(hub, &p, sgsn, ggsn);
1588
1589 /* If the SGSN is replying to a GGSN request, the sequence nr has
1590 * already been unmapped above (unmap_ggsn != NULL), and we need not
1591 * create a new outgoing sequence map. */
1592 if (!ggsn_from_seq)
1593 gtphub_map_seq(&p, sgsn, ggsn, now);
1594
1595 osmo_sockaddr_copy(to_addr, &ggsn->sa);
1596
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001597 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01001598
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001599 LOG(LOGL_DEBUG, "--> Forward to GGSN: %d bytes to %s\n",
Neels Hofmeyre921e322015-11-11 00:45:50 +01001600 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001601 return received;
1602}
1603
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001604static void resolved_gssn_del_cb(struct expiring_item *expi)
1605{
1606 struct gtphub_resolved_ggsn *ggsn;
1607 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
1608
1609 gtphub_port_ref_count_dec(ggsn->peer);
1610 llist_del(&ggsn->entry);
1611
1612 ggsn->expiry_entry.del_cb = 0;
1613 expiring_item_del(&ggsn->expiry_entry);
1614
1615 talloc_free(ggsn);
1616}
1617
1618void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
1619 struct gsn_addr *resolved_addr,
1620 time_t now)
1621{
1622 struct gtphub_peer_port *pp;
1623 struct gtphub_resolved_ggsn *ggsn;
1624
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001625 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001626 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
1627 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001628
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001629 pp = gtphub_port_have(hub, &hub->to_ggsns[GTPH_PLANE_CTRL],
1630 resolved_addr, 2123);
1631 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001632 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
1633 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001634 return;
1635 }
1636
1637 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
1638 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01001639 INIT_LLIST_HEAD(&ggsn->entry);
1640 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001641
1642 ggsn->peer = pp;
1643 gtphub_port_ref_count_inc(pp);
1644
1645 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001646 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001647
1648 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
1649 expiry_add(&hub->expire_tei_maps, &ggsn->expiry_entry, now);
1650
1651 llist_add(&ggsn->entry, &hub->resolved_ggsns);
1652}
1653
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001654static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
1655{
1656 return pp->ref_count == 0;
1657}
1658
1659static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
1660{
1661 struct gtphub_peer_port *pp, *npp;
1662 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
1663 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001664 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001665 gtphub_port_str(pp));
1666 gtphub_peer_port_del(pp);
1667 }
1668 }
1669 return llist_empty(&pa->ports);
1670}
1671
1672static int gtphub_gc_peer(struct gtphub_peer *p)
1673{
1674 struct gtphub_peer_addr *pa, *npa;
1675 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
1676 if (gtphub_gc_peer_addr(pa)) {
1677 gtphub_peer_addr_del(pa);
1678 }
1679 }
1680
1681 /* Note that there's a ref_count in each gtphub_peer_port instance
1682 * listed within p->addresses, referenced by TEI mappings from
1683 * hub->tei_map. As long as those don't expire, this peer will stay. */
1684
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001685 return llist_empty(&p->addresses)
1686 && nr_map_empty(&p->seq_map);
1687}
1688
1689static void gtphub_gc_bind(struct gtphub_bind *b)
1690{
1691 struct gtphub_peer *p, *n;
1692 llist_for_each_entry_safe(p, n, &b->peers, entry) {
1693 if (gtphub_gc_peer(p)) {
1694 gtphub_peer_del(p);
1695 }
1696 }
1697}
1698
1699void gtphub_gc(struct gtphub *hub, time_t now)
1700{
1701 int expired;
1702 expired = expiry_tick(&hub->expire_seq_maps, now);
1703 expired += expiry_tick(&hub->expire_tei_maps, now);
1704
1705 /* ... */
1706
1707 if (expired) {
1708 int i;
1709 for (i = 0; i < GTPH_PLANE_N; i++) {
1710 gtphub_gc_bind(&hub->to_sgsns[i]);
1711 gtphub_gc_bind(&hub->to_ggsns[i]);
1712 }
1713 }
1714}
1715
1716static void gtphub_gc_cb(void *data)
1717{
1718 struct gtphub *hub = data;
1719 gtphub_gc(hub, gtphub_now());
1720 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1721}
1722
1723static void gtphub_gc_start(struct gtphub *hub)
1724{
1725 hub->gc_timer.cb = gtphub_gc_cb;
1726 hub->gc_timer.data = hub;
1727
1728 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
1729}
1730
1731/* called by unit tests */
1732void gtphub_init(struct gtphub *hub)
1733{
1734 gtphub_zero(hub);
1735
1736 expiry_init(&hub->expire_seq_maps, GTPH_SEQ_MAPPING_EXPIRY_SECS);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001737 expiry_init(&hub->expire_tei_maps,
1738 GTPH_TEI_MAPPING_EXPIRY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001739
1740 int plane_idx;
1741 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1742 nr_pool_init(&hub->tei_pool[plane_idx]);
1743 nr_map_init(&hub->tei_map[plane_idx],
1744 &hub->tei_pool[plane_idx],
1745 &hub->expire_tei_maps);
1746
1747 gtphub_bind_init(&hub->to_ggsns[plane_idx]);
1748 gtphub_bind_init(&hub->to_sgsns[plane_idx]);
1749 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01001750
1751 hub->to_sgsns[GTPH_PLANE_CTRL].label = "SGSN Ctrl";
1752 hub->to_ggsns[GTPH_PLANE_CTRL].label = "GGSN Ctrl";
1753 hub->to_sgsns[GTPH_PLANE_USER].label = "SGSN User";
1754 hub->to_ggsns[GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001755}
1756
1757static int gtphub_make_proxy(struct gtphub *hub,
1758 struct gtphub_peer_port **pp,
1759 struct gtphub_bind *bind,
1760 const struct gtphub_cfg_addr *addr)
1761{
1762 if (!addr->addr_str)
1763 return 0;
1764
1765 struct gsn_addr gsna;
1766 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
1767 return -1;
1768
1769 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
1770
1771 /* This is *the* proxy. Make sure it is never expired. */
1772 gtphub_port_ref_count_inc(*pp);
1773 return 0;
1774}
1775
1776int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg)
1777{
1778 int rc;
1779
1780 gtphub_init(hub);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001781 gtphub_ares_init(hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001782
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001783 /* TODO set hub->restart_counter from external file. */
1784
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001785 int plane_idx;
1786 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1787 rc = gtphub_bind_start(&hub->to_ggsns[plane_idx],
1788 &cfg->to_ggsns[plane_idx],
1789 from_ggsns_read_cb, hub, plane_idx);
1790 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001791 LOG(LOGL_FATAL, "Failed to bind for GGSNs (%s)\n",
1792 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001793 return rc;
1794 }
1795
1796 rc = gtphub_bind_start(&hub->to_sgsns[plane_idx],
1797 &cfg->to_sgsns[plane_idx],
1798 from_sgsns_read_cb, hub, plane_idx);
1799 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001800 LOG(LOGL_FATAL, "Failed to bind for SGSNs (%s)\n",
1801 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001802 return rc;
1803 }
1804 }
1805
1806
1807 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1808 if (gtphub_make_proxy(hub,
1809 &hub->sgsn_proxy[plane_idx],
1810 &hub->to_sgsns[plane_idx],
1811 &cfg->sgsn_proxy[plane_idx])
1812 != 0) {
Neels Hofmeyr3d3aa8f2015-11-17 12:26:02 +01001813 LOG(LOGL_FATAL, "Cannot configure SGSN proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001814 " %s port %d.\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001815 cfg->sgsn_proxy[plane_idx].addr_str,
1816 (int)cfg->sgsn_proxy[plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001817 return -1;
1818 }
1819 if (gtphub_make_proxy(hub,
1820 &hub->ggsn_proxy[plane_idx],
1821 &hub->to_ggsns[plane_idx],
1822 &cfg->ggsn_proxy[plane_idx])
1823 != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001824 LOG(LOGL_ERROR, "Cannot configure GGSN proxy.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001825 return -1;
1826 }
1827 }
1828
1829 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
1830 if (hub->sgsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001831 LOG(LOGL_NOTICE, "Using SGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001832 gtphub_plane_idx_names[plane_idx],
1833 gtphub_port_str(hub->sgsn_proxy[plane_idx]));
1834 }
1835
1836 for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) {
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001837 if (hub->ggsn_proxy[plane_idx])
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001838 LOG(LOGL_NOTICE, "Using GGSN %s proxy %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001839 gtphub_plane_idx_names[plane_idx],
1840 gtphub_port_str(hub->ggsn_proxy[plane_idx]));
1841 }
1842
1843 gtphub_gc_start(hub);
1844 return 0;
1845}
1846
1847static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
1848 const struct gsn_addr *addr)
1849{
1850 struct gtphub_peer_addr *a;
1851 llist_for_each_entry(a, &peer->addresses, entry) {
1852 if (gsn_addr_same(&a->addr, addr))
1853 return a;
1854 }
1855 return NULL;
1856}
1857
1858static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
1859 uint16_t port)
1860{
1861 OSMO_ASSERT(port);
1862 struct gtphub_peer_port *pp;
1863 llist_for_each_entry(pp, &a->ports, entry) {
1864 if (pp->port == port)
1865 return pp;
1866 }
1867 return NULL;
1868}
1869
1870static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
1871 const struct gsn_addr *addr)
1872{
1873 struct gtphub_peer *peer;
1874 llist_for_each_entry(peer, &bind->peers, entry) {
1875 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
1876 if (a)
1877 return a;
1878 }
1879 return NULL;
1880}
1881
1882static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
1883 const struct gsn_addr *addr,
1884 uint16_t port)
1885{
1886 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
1887 if (!a)
1888 return NULL;
1889 return gtphub_addr_find_port(a, port);
1890}
1891
1892struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
1893 const struct osmo_sockaddr *addr)
1894{
1895 struct gsn_addr gsna;
1896 uint16_t port;
1897 gsn_addr_from_sockaddr(&gsna, &port, addr);
1898 return gtphub_port_find(bind, &gsna, port);
1899}
1900
1901static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
1902 struct gtphub_bind *bind)
1903{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001904 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
1905 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001906 OSMO_ASSERT(peer);
1907
1908 INIT_LLIST_HEAD(&peer->addresses);
1909
1910 nr_pool_init(&peer->seq_pool);
1911 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_seq_maps);
1912
1913 /* TODO use something random to pick the initial sequence nr.
1914 0x6d31 produces the ASCII character sequence 'm1', currently used in
1915 gtphub_nc_test.sh. */
1916 peer->seq_pool.last_nr = 0x6d31 - 1;
1917
1918 llist_add(&peer->entry, &bind->peers);
1919 return peer;
1920}
1921
1922static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
1923 const struct gsn_addr *addr)
1924{
1925 struct gtphub_peer_addr *a;
1926 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
1927 OSMO_ASSERT(a);
1928 a->peer = peer;
1929 gsn_addr_copy(&a->addr, addr);
1930 INIT_LLIST_HEAD(&a->ports);
1931 llist_add(&a->entry, &peer->addresses);
1932
1933 return a;
1934}
1935
1936static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
1937 struct gtphub_bind *bind,
1938 const struct gsn_addr *addr)
1939{
1940 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
1941 if (a)
1942 return a;
1943
1944 /* If we haven't found an address, that means we need to create an
1945 * entirely new peer for the new address. More addresses may be added
1946 * to this peer later, but not via this function. */
1947 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01001948
1949 a = gtphub_peer_add_addr(peer, addr);
1950
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001951 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01001952 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01001953 gsn_addr_to_str(&a->addr));
1954
1955 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001956}
1957
1958static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
1959 uint16_t port)
1960{
1961 struct gtphub_peer_port *pp;
1962
1963 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
1964 OSMO_ASSERT(pp);
1965 pp->peer_addr = a;
1966 pp->port = port;
1967
1968 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
1969 talloc_free(pp);
1970 return NULL;
1971 }
1972
1973 llist_add(&pp->entry, &a->ports);
1974
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001975 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001976 gsn_addr_to_str(&a->addr),
1977 (int)port);
1978
1979 return pp;
1980}
1981
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001982struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
1983 struct gtphub_bind *bind,
1984 const struct gsn_addr *addr,
1985 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001986{
1987 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
1988
1989 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
1990 if (pp)
1991 return pp;
1992
1993 return gtphub_addr_add_port(a, port);
1994}
1995
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01001996/* Find a GGSN peer with a matching address. If the address is known but the
1997 * port not, create a new port for that peer address. */
1998struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
1999 const struct osmo_sockaddr *addr)
2000{
2001 struct gtphub_peer_addr *pa;
2002 struct gtphub_peer_port *pp;
2003
2004 struct gsn_addr gsna;
2005 uint16_t port;
2006 gsn_addr_from_sockaddr(&gsna, &port, addr);
2007
2008 pa = gtphub_addr_find(bind, &gsna);
2009 if (!pa)
2010 return NULL;
2011
2012 pp = gtphub_addr_find_port(pa, port);
2013
2014 if (!pp)
2015 pp = gtphub_addr_add_port(pa, port);
2016
2017 return pp;
2018}
2019
2020
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002021/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2022 * resolution should be possible but failed, and 1 if resolution was
2023 * successful. *pp will be set to NULL if <1 is returned. */
2024static int gtphub_resolve_ggsn(struct gtphub *hub,
2025 struct gtp_packet_desc *p,
2026 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002027{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002028 *pp = NULL;
2029
2030 /* TODO determine from message type whether IEs should be present? */
2031
2032 int rc;
2033 const char *imsi_str;
2034 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2035 if (rc < 1)
2036 return rc;
2037 OSMO_ASSERT(imsi_str);
2038
2039 const char *apn_str;
2040 rc = get_ie_apn_str(p->ie, &apn_str);
2041 if (rc < 1)
2042 return rc;
2043 OSMO_ASSERT(apn_str);
2044
2045 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2046 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002047}
2048
2049
2050/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002051/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002052/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2053 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002054static int _osmo_getaddrinfo(struct addrinfo **result,
2055 uint16_t family, uint16_t type, uint8_t proto,
2056 const char *host, uint16_t port)
2057{
2058 struct addrinfo hints;
2059 char portbuf[16];
2060
2061 sprintf(portbuf, "%u", port);
2062 memset(&hints, '\0', sizeof(struct addrinfo));
2063 hints.ai_family = family;
2064 if (type == SOCK_RAW) {
2065 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2066 * SOCK_RAW and IPPROTO_GRE is used.
2067 */
2068 hints.ai_socktype = SOCK_DGRAM;
2069 hints.ai_protocol = IPPROTO_UDP;
2070 } else {
2071 hints.ai_socktype = type;
2072 hints.ai_protocol = proto;
2073 }
2074
2075 return getaddrinfo(host, portbuf, &hints, result);
2076}
2077
2078/* TODO move to osmocom/core/socket.c ? */
2079int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2080 uint16_t family, uint16_t type, uint8_t proto,
2081 const char *host, uint16_t port)
2082{
2083 struct addrinfo *res;
2084 int rc;
2085 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2086
2087 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002088 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002089 return -EINVAL;
2090 }
2091
2092 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2093 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2094 addr->l = res->ai_addrlen;
2095 freeaddrinfo(res);
2096
2097 return 0;
2098}
2099
2100int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2101 char *port_str, size_t port_str_len,
2102 const struct osmo_sockaddr *addr,
2103 int flags)
2104{
2105 int rc;
2106
2107 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2108 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2109 return -1;
2110 }
2111
2112 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002113 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2114 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002115 return -1;
2116 }
2117
2118 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2119 addr_str, addr_str_len,
2120 port_str, port_str_len,
2121 flags);
2122
2123 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002124 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2125 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2126 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002127
2128 return rc;
2129}
2130
2131const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2132 char *buf, size_t buf_len)
2133{
2134 const int portbuf_len = 6;
2135 OSMO_ASSERT(buf_len > portbuf_len);
2136 char *portbuf = buf + buf_len - portbuf_len;
2137 buf_len -= portbuf_len;
2138 if (osmo_sockaddr_to_strs(buf, buf_len,
2139 portbuf, portbuf_len,
2140 addr,
2141 NI_NUMERICHOST | NI_NUMERICSERV))
2142 return NULL;
2143
2144 char *pos = buf + strnlen(buf, buf_len-1);
2145 size_t len = buf_len - (pos - buf);
2146
2147 snprintf(pos, len, " port %s", portbuf);
2148 buf[buf_len-1] = '\0';
2149
2150 return buf;
2151}
2152
2153const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2154{
2155 static char buf[256];
2156 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2157 if (! result)
2158 return "(invalid)";
2159 return result;
2160}
2161
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002162int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2163 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002164{
2165 if (a == b)
2166 return 0;
2167 if (!a)
2168 return -1;
2169 if (!b)
2170 return 1;
2171 if (a->l != b->l) {
2172 /* Lengths are not the same, but determine the order. Will
2173 * anyone ever sort a list by osmo_sockaddr though...? */
2174 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2175 if (cmp == 0) {
2176 if (a->l < b->l)
2177 return -1;
2178 else
2179 return 1;
2180 }
2181 return cmp;
2182 }
2183 return memcmp(&a->a, &b->a, a->l);
2184}
2185
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002186void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2187 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002188{
2189 OSMO_ASSERT(src->l <= sizeof(dst->a));
2190 memcpy(&dst->a, &src->a, src->l);
2191 dst->l = src->l;
2192}