blob: 676282575d3ef475c01ceb573aecc1d0a91822ec [file] [log] [blame]
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001/* GTP Hub Implementation */
2
3/* (C) 2015 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <string.h>
23#include <errno.h>
24#include <inttypes.h>
25#include <time.h>
26#include <limits.h>
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +010027#include <unistd.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020028#include <sys/socket.h>
29#include <netinet/in.h>
30#include <arpa/inet.h>
31
32#include <gtp.h>
33#include <gtpie.h>
34
35#include <openbsc/gtphub.h>
36#include <openbsc/debug.h>
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010037#include <openbsc/gprs_utils.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020038
39#include <osmocom/core/utils.h>
40#include <osmocom/core/logging.h>
41#include <osmocom/core/socket.h>
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +010042#include <osmocom/core/rate_ctr.h>
43#include <osmocom/core/stats.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020044
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010045
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020046static const int GTPH_GC_TICK_SECONDS = 1;
47
48void *osmo_gtphub_ctx;
49
Neels Hofmeyr063a8022015-11-16 14:35:13 +010050/* Convenience makro, note: only within this C file. */
51#define LOG(level, fmt, args...) \
52 LOGP(DGTPHUB, level, fmt, ##args)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020053
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010054#define ZERO_STRUCT(struct_pointer) memset(struct_pointer, '\0', \
55 sizeof(*(struct_pointer)))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020056
57/* TODO move this to osmocom/core/select.h ? */
58typedef int (*osmo_fd_cb_t)(struct osmo_fd *fd, unsigned int what);
59
60/* TODO move this to osmocom/core/linuxlist.h ? */
61#define __llist_first(head) (((head)->next == (head)) ? NULL : (head)->next)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010062#define llist_first(head, type, entry) \
63 llist_entry(__llist_first(head), type, entry)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020064
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +010065#define __llist_last(head) (((head)->next == (head)) ? NULL : (head)->prev)
66#define llist_last(head, type, entry) \
67 llist_entry(__llist_last(head), type, entry)
68
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020069/* TODO move GTP header stuff to openggsn/gtp/ ? See gtp_decaps*() */
70
71enum gtp_rc {
72 GTP_RC_UNKNOWN = 0,
73 GTP_RC_TINY = 1, /* no IEs (like ping/pong) */
Neels Hofmeyre921e322015-11-11 00:45:50 +010074 GTP_RC_PDU_C = 2, /* a real packet with IEs */
75 GTP_RC_PDU_U = 3, /* a real packet with User data */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020076
77 GTP_RC_TOOSHORT = -1,
78 GTP_RC_UNSUPPORTED_VERSION = -2,
79 GTP_RC_INVALID_IE = -3,
80};
81
82struct gtp_packet_desc {
83 union gtp_packet *data;
84 int data_len;
85 int header_len;
86 int version;
87 uint8_t type;
88 uint16_t seq;
Neels Hofmeyre54cd152015-11-24 13:31:06 +010089 uint32_t header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020090 uint32_t header_tei;
91 int rc; /* enum gtp_rc */
92 unsigned int plane_idx;
Neels Hofmeyre54cd152015-11-24 13:31:06 +010093 unsigned int side_idx;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +010094 struct gtphub_tunnel *tun;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +010095 time_t timestamp;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020096 union gtpie_member *ie[GTPIE_SIZE];
97};
98
Neels Hofmeyr10fc0242015-12-01 00:23:45 +010099struct pending_delete {
100 struct llist_head entry;
101 struct expiring_item expiry_entry;
102
103 struct gtphub_tunnel *tun;
104 uint8_t teardown_ind;
105 uint8_t nsapi;
106};
107
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100108
109/* counters */
110
111enum gtphub_counters_io {
112 GTPH_CTR_PKTS_IN = 0,
113 GTPH_CTR_PKTS_OUT,
114 GTPH_CTR_BYTES_IN,
115 GTPH_CTR_BYTES_OUT
116};
117
118static const struct rate_ctr_desc gtphub_counters_io_desc[] = {
119 { "packets.in", "Packets ( In)" },
120 { "packets.out", "Packets (Out)" },
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100121 { "bytes.in", "Bytes ( In)" },
122 { "bytes.out", "Bytes (Out)" },
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100123};
124
125static const struct rate_ctr_group_desc gtphub_ctrg_io_desc = {
126 .group_name_prefix = "gtphub.bind",
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100127 .group_description = "I/O Statistics",
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100128 .num_ctr = ARRAY_SIZE(gtphub_counters_io_desc),
129 .ctr_desc = gtphub_counters_io_desc,
130 .class_id = OSMO_STATS_CLASS_GLOBAL,
131};
132
133
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200134void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
135{
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +0100136 *gsna = *src;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200137}
138
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200139int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
140 const struct osmo_sockaddr *sa)
141{
142 char addr_str[256];
143 char port_str[6];
144
145 if (osmo_sockaddr_to_strs(addr_str, sizeof(addr_str),
146 port_str, sizeof(port_str),
147 sa, (NI_NUMERICHOST | NI_NUMERICSERV))
148 != 0) {
149 return -1;
150 }
151
152 if (port)
153 *port = atoi(port_str);
154
155 return gsn_addr_from_str(gsna, addr_str);
156}
157
158int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
159{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100160 if ((!gsna) || (!numeric_addr_str))
161 return -1;
162
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200163 int af = AF_INET;
164 gsna->len = 4;
165 const char *pos = numeric_addr_str;
166 for (; *pos; pos++) {
167 if (*pos == ':') {
168 af = AF_INET6;
169 gsna->len = 16;
170 break;
171 }
172 }
173
174 int rc = inet_pton(af, numeric_addr_str, gsna->buf);
175 if (rc != 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100176 LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n",
177 numeric_addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200178 return -1;
179 }
180 return 0;
181}
182
183const char *gsn_addr_to_str(const struct gsn_addr *gsna)
184{
185 static char buf[INET6_ADDRSTRLEN + 1];
186 return gsn_addr_to_strb(gsna, buf, sizeof(buf));
187}
188
189const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
190 char *strbuf,
191 int strbuf_len)
192{
193 int af;
194 switch (gsna->len) {
195 case 4:
196 af = AF_INET;
197 break;
198 case 16:
199 af = AF_INET6;
200 break;
201 default:
202 return NULL;
203 }
204
205 const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
206 if (!r) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100207 LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:"
208 " %s: len=%d, buf=%s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100209 strerror(errno),
210 (int)gsna->len,
211 osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200212 }
213 return r;
214}
215
216int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
217{
218 if (a == b)
219 return 1;
220 if ((!a) || (!b))
221 return 0;
222 if (a->len != b->len)
223 return 0;
224 return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
225}
226
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100227static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p,
228 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200229{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100230 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200231 return -1;
232
233 unsigned int len;
234 /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
235 if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
236 &len, gsna->buf, sizeof(gsna->buf))
237 != 0)
238 return -1;
239 gsna->len = len;
240 return 0;
241}
242
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100243static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p,
244 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200245{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100246 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200247 return -1;
248
249 int ie_idx;
250 ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx);
251
252 if (ie_idx < 0)
253 return -1;
254
255 struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
256 int ie_l = ntoh16(ie->l);
257 if (ie_l != gsna->len) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100258 LOG(LOGL_ERROR, "Not implemented:"
259 " replace an IE address of different size:"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200260 " replace %d with %d\n", (int)ie_l, (int)gsna->len);
261 return -1;
262 }
263
264 memcpy(ie->v, gsna->buf, (int)ie_l);
265 return 0;
266}
267
268/* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there.
269 */
270void validate_gtp0_header(struct gtp_packet_desc *p)
271{
272 const struct gtp0_header *pheader = &(p->data->gtp0.h);
273 p->rc = GTP_RC_UNKNOWN;
274 p->header_len = 0;
275
276 OSMO_ASSERT(p->data_len >= 1);
277 OSMO_ASSERT(p->version == 0);
278
279 if (p->data_len < GTP0_HEADER_SIZE) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100280 LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200281 p->rc = GTP_RC_TOOSHORT;
282 return;
283 }
284
285 p->type = ntoh8(pheader->type);
286 p->seq = ntoh16(pheader->seq);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100287 p->header_tei_rx = 0; /* TODO */
288 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200289
290 if (p->data_len == GTP0_HEADER_SIZE) {
291 p->rc = GTP_RC_TINY;
292 p->header_len = GTP0_HEADER_SIZE;
293 return;
294 }
295
296 /* Check packet length field versus length of packet */
297 if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100298 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
299 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100300 GTP0_HEADER_SIZE, (int)ntoh16(pheader->length),
301 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200302 p->rc = GTP_RC_TOOSHORT;
303 return;
304 }
305
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100306 LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200307 p->header_len = GTP0_HEADER_SIZE;
Neels Hofmeyre921e322015-11-11 00:45:50 +0100308 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200309}
310
311/* Validate GTP version 1 data, and update p->rc with the result, as well as
312 * p->header_len in case of a valid header. */
313void validate_gtp1_header(struct gtp_packet_desc *p)
314{
315 const struct gtp1_header_long *pheader = &(p->data->gtp1l.h);
316 p->rc = GTP_RC_UNKNOWN;
317 p->header_len = 0;
318
319 OSMO_ASSERT(p->data_len >= 1);
320 OSMO_ASSERT(p->version == 1);
321
322 if ((p->data_len < GTP1_HEADER_SIZE_LONG)
323 && (p->data_len != GTP1_HEADER_SIZE_SHORT)){
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100324 LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200325 p->rc = GTP_RC_TOOSHORT;
326 return;
327 }
328
329 p->type = ntoh8(pheader->type);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100330 p->header_tei_rx = ntoh32(pheader->tei);
331 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200332 p->seq = ntoh16(pheader->seq);
333
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100334 LOG(LOGL_DEBUG, "GTPv1\n"
335 "| type = %" PRIu8 " 0x%02" PRIx8 "\n"
336 "| length = %" PRIu16 " 0x%04" PRIx16 "\n"
337 "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n"
338 "| seq = %" PRIu16 " 0x%04" PRIx16 "\n"
339 "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n"
340 "| next = %" PRIu8 " 0x%02" PRIx8 "\n",
341 p->type, p->type,
342 ntoh16(pheader->length), ntoh16(pheader->length),
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100343 p->header_tei_rx, p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100344 p->seq, p->seq,
345 pheader->npdu, pheader->npdu,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200346 pheader->next, pheader->next);
347
348 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
349 p->rc = GTP_RC_TINY;
350 p->header_len = GTP1_HEADER_SIZE_SHORT;
351 return;
352 }
353
354 /* Check packet length field versus length of packet */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100355 int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT;
356 if (p->data_len != announced_len) {
357 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
358 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100359 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
360 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200361 p->rc = GTP_RC_TOOSHORT;
362 return;
363 }
364
Neels Hofmeyre921e322015-11-11 00:45:50 +0100365 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200366 p->header_len = GTP1_HEADER_SIZE_LONG;
367}
368
369/* Examine whether p->data of size p->data_len has a valid GTP header. Set
370 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
371 * gtp_rc). p->data must point at a buffer with p->data_len set. */
372void validate_gtp_header(struct gtp_packet_desc *p)
373{
374 p->rc = GTP_RC_UNKNOWN;
375
376 /* Need at least 1 byte in order to check version */
377 if (p->data_len < 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100378 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n",
379 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200380 p->rc = GTP_RC_TOOSHORT;
381 return;
382 }
383
384 p->version = p->data->flags >> 5;
385
386 switch (p->version) {
387 case 0:
388 validate_gtp0_header(p);
389 break;
390 case 1:
391 validate_gtp1_header(p);
392 break;
393 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100394 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200395 p->rc = GTP_RC_UNSUPPORTED_VERSION;
396 break;
397 }
398}
399
400
401/* Return the value of the i'th IMSI IEI by copying to *imsi.
402 * The first IEI is reached by passing i = 0.
403 * imsi must point at allocated space of (at least) 8 bytes.
404 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100405static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200406{
407 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
408}
409
410/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100411static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200412{
413 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
414}
415
416static char imsi_digit_to_char(uint8_t nibble)
417{
418 nibble &= 0x0f;
419 if (nibble > 9)
420 return (nibble == 0x0f) ? '\0' : '?';
421 return '0' + nibble;
422}
423
424/* Return a human readable IMSI string, in a static buffer.
425 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100426static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200427{
428 static char str[17];
429 int i;
430
431 for (i = 0; i < 8; i++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +0100432 char c;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100433 c = imsi_digit_to_char(imsi[i]);
434 if (c == '?')
435 return -1;
436 str[2*i] = c;
437
438 c = imsi_digit_to_char(imsi[i] >> 4);
439 if (c == '?')
440 return -1;
441 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200442 }
443 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100444 *imsi_str = str;
445 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200446}
447
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100448/* Return 0 if not present, 1 if present and decoded successfully, -1 if
449 * present but cannot be decoded. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100450static int get_ie_imsi_str(union gtpie_member *ie[], int i,
451 const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100452{
453 uint8_t imsi_buf[8];
454 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100455 return 0;
456 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100457}
458
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100459/* Return 0 if not present, 1 if present and decoded successfully, -1 if
460 * present but cannot be decoded. */
461static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100462{
463 static char apn_buf[GSM_APN_LENGTH];
464 unsigned int len;
465 if (gtpie_gettlv(ie, GTPIE_APN, 0,
466 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100467 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100468
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100469 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100470 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
471 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100472 return -1;
473 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100474
475 if (len > (sizeof(apn_buf) - 1))
476 len = sizeof(apn_buf) - 1;
477 apn_buf[len] = '\0';
478
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100479 *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
480 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100481 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
482 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100483 return -1;
484 }
485 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100486}
487
488
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200489/* Validate header, and index information elements. Write decoded packet
490 * information to *res. res->data will point at the given data buffer. On
491 * error, p->rc is set <= 0 (see enum gtp_rc). */
492static void gtp_decode(const uint8_t *data, int data_len,
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100493 unsigned int from_side_idx,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200494 unsigned int from_plane_idx,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100495 struct gtp_packet_desc *res,
496 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200497{
498 ZERO_STRUCT(res);
499 res->data = (union gtp_packet*)data;
500 res->data_len = data_len;
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100501 res->side_idx = from_side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200502 res->plane_idx = from_plane_idx;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100503 res->timestamp = now;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200504
505 validate_gtp_header(res);
506
Neels Hofmeyr87c83d02015-12-03 11:25:27 +0100507 if (res->rc <= 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200508 return;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200509
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100510 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200511
Neels Hofmeyre921e322015-11-11 00:45:50 +0100512 if (from_plane_idx == GTPH_PLANE_USER) {
513 res->rc = GTP_RC_PDU_U;
514 return;
515 }
516
517 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100518 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200519 return;
520 }
521
522 if (gtpie_decaps(res->ie, res->version,
523 (void*)(data + res->header_len),
524 res->data_len - res->header_len) != 0) {
525 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100526 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
527 " Dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200528 return;
529 }
530
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100531#if 1
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100532 /* TODO if (<loglevel is debug>) { ...
533 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200534 int i;
535
536 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100537 const char *imsi;
538 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200539 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100540 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200541 }
542
543 for (i = 0; i < 10; i++) {
544 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100545 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200546 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100547 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200548 }
549
550 for (i = 0; i < 2; i++) {
551 struct gsn_addr addr;
552 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100553 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200554 }
555
556 for (i = 0; i < 10; i++) {
557 uint32_t tei;
558 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
559 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100560 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200561 tei, tei);
562 }
563
564 for (i = 0; i < 10; i++) {
565 uint32_t tei;
566 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
567 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100568 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200569 tei, tei);
570 }
571#endif
572}
573
574
575/* expiry */
576
577void expiry_init(struct expiry *exq, int expiry_in_seconds)
578{
579 ZERO_STRUCT(exq);
580 exq->expiry_in_seconds = expiry_in_seconds;
581 INIT_LLIST_HEAD(&exq->items);
582}
583
584void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
585{
586 item->expiry = now + exq->expiry_in_seconds;
587
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +0100588 OSMO_ASSERT(llist_empty(&exq->items)
589 || (item->expiry
590 >= llist_last(&exq->items, struct expiring_item, entry)->expiry));
591
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200592 /* Add/move to the tail to always sort by expiry, ascending. */
593 llist_del(&item->entry);
594 llist_add_tail(&item->entry, &exq->items);
595}
596
597int expiry_tick(struct expiry *exq, time_t now)
598{
599 int expired = 0;
600 struct expiring_item *m, *n;
601 llist_for_each_entry_safe(m, n, &exq->items, entry) {
602 if (m->expiry <= now) {
603 expiring_item_del(m);
604 expired ++;
605 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200606 /* The items are added sorted by expiry. So when we hit
607 * an unexpired entry, only more unexpired ones will
608 * follow. */
609 break;
610 }
611 }
612 return expired;
613}
614
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100615void expiry_clear(struct expiry *exq)
616{
617 struct expiring_item *m, *n;
618 llist_for_each_entry_safe(m, n, &exq->items, entry) {
619 expiring_item_del(m);
620 }
621}
622
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200623void expiring_item_init(struct expiring_item *item)
624{
625 ZERO_STRUCT(item);
626 INIT_LLIST_HEAD(&item->entry);
627}
628
629void expiring_item_del(struct expiring_item *item)
630{
631 OSMO_ASSERT(item);
632 llist_del(&item->entry);
633 INIT_LLIST_HEAD(&item->entry);
634 if (item->del_cb) {
635 /* avoid loops */
636 del_cb_t del_cb = item->del_cb;
637 item->del_cb = 0;
638 (del_cb)(item);
639 }
640}
641
642
643/* nr_map, nr_pool */
644
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100645void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200646{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100647 *pool = (struct nr_pool){
648 .nr_min = nr_min,
649 .nr_max = nr_max,
650 .last_nr = nr_max
651 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200652}
653
654nr_t nr_pool_next(struct nr_pool *pool)
655{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100656 if (pool->last_nr >= pool->nr_max)
657 pool->last_nr = pool->nr_min;
658 else
659 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200660
661 return pool->last_nr;
662}
663
664void nr_map_init(struct nr_map *map, struct nr_pool *pool,
665 struct expiry *exq)
666{
667 ZERO_STRUCT(map);
668 map->pool = pool;
669 map->add_items_to_expiry = exq;
670 INIT_LLIST_HEAD(&map->mappings);
671}
672
673void nr_mapping_init(struct nr_mapping *m)
674{
675 ZERO_STRUCT(m);
676 INIT_LLIST_HEAD(&m->entry);
677 expiring_item_init(&m->expiry_entry);
678}
679
680void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
681{
682 /* Generate a mapped number */
683 mapping->repl = nr_pool_next(map->pool);
684
685 /* Add to the tail to always yield a list sorted by expiry, in
686 * ascending order. */
687 llist_add_tail(&mapping->entry, &map->mappings);
Neels Hofmeyr508514c2015-11-24 13:30:38 +0100688 nr_map_refresh(map, mapping, now);
689}
690
691void nr_map_refresh(struct nr_map *map, struct nr_mapping *mapping, time_t now)
692{
693 if (!map->add_items_to_expiry)
694 return;
695 expiry_add(map->add_items_to_expiry,
696 &mapping->expiry_entry,
697 now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200698}
699
700void nr_map_clear(struct nr_map *map)
701{
702 struct nr_mapping *m;
703 struct nr_mapping *n;
704 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
705 nr_mapping_del(m);
706 }
707}
708
709int nr_map_empty(const struct nr_map *map)
710{
711 return llist_empty(&map->mappings);
712}
713
714struct nr_mapping *nr_map_get(const struct nr_map *map,
715 void *origin, nr_t nr_orig)
716{
717 struct nr_mapping *mapping;
718 llist_for_each_entry(mapping, &map->mappings, entry) {
719 if ((mapping->origin == origin)
720 && (mapping->orig == nr_orig))
721 return mapping;
722 }
723 /* Not found. */
724 return NULL;
725}
726
727struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
728{
729 struct nr_mapping *mapping;
730 llist_for_each_entry(mapping, &map->mappings, entry) {
731 if (mapping->repl == nr_repl) {
732 return mapping;
733 }
734 }
735 /* Not found. */
736 return NULL;
737}
738
739void nr_mapping_del(struct nr_mapping *mapping)
740{
741 OSMO_ASSERT(mapping);
742 llist_del(&mapping->entry);
743 INIT_LLIST_HEAD(&mapping->entry);
744 expiring_item_del(&mapping->expiry_entry);
745}
746
747
748/* gtphub */
749
750const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
751 "CTRL",
752 "USER",
753};
754
755const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
756 2123,
757 2152,
758};
759
Neels Hofmeyra9905a52015-11-29 23:49:48 +0100760const char* const gtphub_side_idx_names[GTPH_SIDE_N] = {
761 "SGSN",
762 "GGSN",
763};
764
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200765time_t gtphub_now(void)
766{
767 struct timespec now_tp;
768 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
769 return now_tp.tv_sec;
770}
771
772/* Remove a gtphub_peer from its list and free it. */
773static void gtphub_peer_del(struct gtphub_peer *peer)
774{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100775 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200776 nr_map_clear(&peer->seq_map);
777 llist_del(&peer->entry);
778 talloc_free(peer);
779}
780
781static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
782{
783 OSMO_ASSERT(llist_empty(&pa->ports));
784 llist_del(&pa->entry);
785 talloc_free(pa);
786}
787
788static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
789{
790 OSMO_ASSERT(pp->ref_count == 0);
791 llist_del(&pp->entry);
792 talloc_free(pp);
793}
794
795/* From the information in the gtp_packet_desc, return the address of a GGSN.
796 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100797static int gtphub_resolve_ggsn(struct gtphub *hub,
798 struct gtp_packet_desc *p,
799 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200800
801/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100802struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
803 const char *imsi_str,
804 const char *apn_ni_str);
805int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200806
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200807static void gtphub_zero(struct gtphub *hub)
808{
809 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100810 INIT_LLIST_HEAD(&hub->ggsn_lookups);
811 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200812}
813
814static int gtphub_sock_init(struct osmo_fd *ofd,
815 const struct gtphub_cfg_addr *addr,
816 osmo_fd_cb_t cb,
817 void *data,
818 int ofd_id)
819{
820 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100821 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200822 return -1;
823 }
824 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100825 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200826 return -1;
827 }
828
829 ofd->when = BSC_FD_READ;
830 ofd->cb = cb;
831 ofd->data = data;
832 ofd->priv_nr = ofd_id;
833
834 int rc;
835 rc = osmo_sock_init_ofd(ofd,
836 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
837 addr->addr_str, addr->port,
838 OSMO_SOCK_F_BIND);
839 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100840 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
841 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200842 return -1;
843 }
844
845 return 0;
846}
847
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100848static void gtphub_sock_close(struct osmo_fd *ofd)
849{
850 close(ofd->fd);
851 osmo_fd_unregister(ofd);
852 ofd->cb = NULL;
853}
854
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200855static void gtphub_bind_init(struct gtphub_bind *b)
856{
857 ZERO_STRUCT(b);
858
859 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100860
861 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
862 &gtphub_ctrg_io_desc, 0);
863 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200864}
865
866static int gtphub_bind_start(struct gtphub_bind *b,
867 const struct gtphub_cfg_bind *cfg,
868 osmo_fd_cb_t cb, void *cb_data,
869 unsigned int ofd_id)
870{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100871 LOG(LOGL_DEBUG, "Starting bind %s\n", b->label);
872 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0) {
873 LOG(LOGL_FATAL, "Invalid bind address for %s: %s\n",
874 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200875 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100876 }
877 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0) {
878 LOG(LOGL_FATAL, "Cannot bind for %s: %s\n",
879 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200880 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100881 }
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100882 b->local_port = cfg->bind.port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200883 return 0;
884}
885
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100886static void gtphub_bind_free(struct gtphub_bind *b)
887{
888 OSMO_ASSERT(llist_empty(&b->peers));
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100889 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100890}
891
892static void gtphub_bind_stop(struct gtphub_bind *b) {
893 gtphub_sock_close(&b->ofd);
894 gtphub_bind_free(b);
895}
896
Neels Hofmeyr40348972015-11-17 12:22:28 +0100897/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200898 * Return the number of bytes read, zero on error. */
899static int gtphub_read(const struct osmo_fd *from,
900 struct osmo_sockaddr *from_addr,
901 uint8_t *buf, size_t buf_len)
902{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100903 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200904
Neels Hofmeyr40348972015-11-17 12:22:28 +0100905 /* recvfrom requires the available length set in *from_addr_len. */
906 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200907 errno = 0;
908 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100909 (struct sockaddr*)&from_addr->a,
910 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200911 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
912 * is not truncated. Then maybe reduce buf's size. */
913
914 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100915 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
916 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200917 return 0;
918 }
919
Neels Hofmeyr40348972015-11-17 12:22:28 +0100920 LOG(LOGL_DEBUG, "Received %d bytes from %s\n%s\n",
921 (int)received, osmo_sockaddr_to_str(from_addr),
922 osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200923
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200924 return received;
925}
926
927inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
928{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100929 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200930 OSMO_ASSERT(pp->ref_count < UINT_MAX);
931 pp->ref_count++;
932}
933
934inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
935{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100936 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200937 OSMO_ASSERT(pp->ref_count > 0);
938 pp->ref_count--;
939}
940
941inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
942{
943 OSMO_ASSERT(p->version == 1);
944 p->data->gtp1l.h.seq = hton16(seq);
945 p->seq = seq;
946}
947
948inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
949{
950 OSMO_ASSERT(p->version == 1);
951 p->data->gtp1l.h.tei = hton32(tei);
952 p->header_tei = tei;
953}
954
955static void gtphub_mapping_del_cb(struct expiring_item *expi);
956
957static struct nr_mapping *gtphub_mapping_new()
958{
959 struct nr_mapping *nrm;
960 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
961 OSMO_ASSERT(nrm);
962
963 nr_mapping_init(nrm);
964 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
965 return nrm;
966}
967
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100968
969#define APPEND(args...) \
970 l = snprintf(pos, left, args); \
971 pos += l; \
972 left -= l
973
974static const char *gtphub_tunnel_side_str(struct gtphub_tunnel *tun,
975 int side_idx)
976{
977 static char buf[256];
978 char *pos = buf;
979 int left = sizeof(buf);
980 int l;
981
982 struct gtphub_tunnel_endpoint *c, *u;
983 c = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
984 u = &tun->endpoint[side_idx][GTPH_PLANE_USER];
985
986 /* print both only if they differ. */
987 if (!c->peer) {
988 APPEND("(uninitialized)");
989 } else {
990 APPEND("%s", gsn_addr_to_str(&c->peer->peer_addr->addr));
991 }
992
993 if (!u->peer) {
994 if (c->peer) {
995 APPEND(" / (uninitialized)");
996 }
997 } else if ((!c->peer)
998 || (!gsn_addr_same(&u->peer->peer_addr->addr,
999 &c->peer->peer_addr->addr))) {
1000 APPEND(" / %s", gsn_addr_to_str(&u->peer->peer_addr->addr));
1001 }
1002
1003 APPEND(" (TEI C %x=%x / U %x=%x)",
1004 c->tei_orig, c->tei_repl,
1005 u->tei_orig, u->tei_repl);
1006 return buf;
1007}
1008
1009const char *gtphub_tunnel_str(struct gtphub_tunnel *tun)
1010{
1011 static char buf[512];
1012 char *pos = buf;
1013 int left = sizeof(buf);
1014 int l;
1015
1016 APPEND("%s", gtphub_tunnel_side_str(tun, GTPH_SIDE_SGSN));
1017 APPEND(" <-> %s", gtphub_tunnel_side_str(tun, GTPH_SIDE_GGSN));
1018
1019 return buf;
1020}
1021
1022#undef APPEND
1023
1024void gtphub_tunnel_endpoint_set_peer(struct gtphub_tunnel_endpoint *te,
1025 struct gtphub_peer_port *pp)
1026{
1027 if (te->peer)
1028 gtphub_port_ref_count_dec(te->peer);
1029 te->peer = pp;
1030 if (te->peer)
1031 gtphub_port_ref_count_inc(te->peer);
1032}
1033
1034int gtphub_tunnel_complete(struct gtphub_tunnel *tun)
1035{
1036 if (!tun)
1037 return 0;
1038 int side_idx;
1039 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001040 for_each_side_and_plane(side_idx, plane_idx) {
1041 struct gtphub_tunnel_endpoint *te =
1042 &tun->endpoint[side_idx][plane_idx];
1043 if (!(te->peer && te->tei_orig && te->tei_repl))
1044 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001045 }
1046 return 1;
1047}
1048
1049static void gtphub_tunnel_del_cb(struct expiring_item *expi)
1050{
1051 struct gtphub_tunnel *tun = container_of(expi,
1052 struct gtphub_tunnel,
1053 expiry_entry);
1054 LOG(LOGL_DEBUG, "expired: %s\n", gtphub_tunnel_str(tun));
1055
1056 llist_del(&tun->entry);
1057 INIT_LLIST_HEAD(&tun->entry); /* mark unused */
1058
1059 expi->del_cb = 0; /* avoid recursion loops */
1060 expiring_item_del(&tun->expiry_entry); /* usually already done, but make sure. */
1061
1062 int side_idx;
1063 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001064 for_each_side_and_plane(side_idx, plane_idx) {
1065 /* clear ref count */
1066 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1067 NULL);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001068 }
1069
1070 talloc_free(tun);
1071}
1072
1073static struct gtphub_tunnel *gtphub_tunnel_new()
1074{
1075 struct gtphub_tunnel *tun;
1076 tun = talloc_zero(osmo_gtphub_ctx, struct gtphub_tunnel);
1077 OSMO_ASSERT(tun);
1078
1079 INIT_LLIST_HEAD(&tun->entry);
1080 expiring_item_init(&tun->expiry_entry);
1081
1082 tun->expiry_entry.del_cb = gtphub_tunnel_del_cb;
1083 return tun;
1084}
1085
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001086static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
1087 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001088{
1089 if (llist_empty(&peer->addresses))
1090 return "(addressless)";
1091
1092 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
1093 struct gtphub_peer_addr,
1094 entry);
1095 return gsn_addr_to_strb(&a->addr, buf, buflen);
1096}
1097
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001098static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
1099 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001100{
1101 if (!port)
1102 return "(null port)";
1103
1104 snprintf(buf, buflen, "%s port %d",
1105 gsn_addr_to_str(&port->peer_addr->addr),
1106 (int)port->port);
1107 return buf;
1108}
1109
1110const char *gtphub_peer_str(struct gtphub_peer *peer)
1111{
1112 static char buf[256];
1113 return gtphub_peer_strb(peer, buf, sizeof(buf));
1114}
1115
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001116const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001117{
1118 static char buf[256];
1119 return gtphub_port_strb(port, buf, sizeof(buf));
1120}
1121
1122static const char *gtphub_port_str2(struct gtphub_peer_port *port)
1123{
1124 static char buf[256];
1125 return gtphub_port_strb(port, buf, sizeof(buf));
1126}
1127
1128static void gtphub_mapping_del_cb(struct expiring_item *expi)
1129{
1130 expi->del_cb = 0; /* avoid recursion loops */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001131 expiring_item_del(expi); /* usually already done, but make sure. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001132
1133 struct nr_mapping *nrm = container_of(expi,
1134 struct nr_mapping,
1135 expiry_entry);
1136 llist_del(&nrm->entry);
1137 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
1138
1139 /* Just for log */
1140 struct gtphub_peer_port *from = nrm->origin;
1141 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001142 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001143 (int)nrm->expiry_entry.expiry,
1144 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001145 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001146
1147 gtphub_port_ref_count_dec(from);
1148
1149 talloc_free(nrm);
1150}
1151
1152static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
1153 struct gtphub_peer_port *from,
1154 nr_t orig_nr,
1155 time_t now)
1156{
1157 struct nr_mapping *nrm;
1158
1159 nrm = nr_map_get(map, from, orig_nr);
1160
1161 if (!nrm) {
1162 nrm = gtphub_mapping_new();
1163 nrm->orig = orig_nr;
1164 nrm->origin = from;
1165 nr_map_add(map, nrm, now);
1166 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001167 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001168 gtphub_port_str(from),
1169 (int)(nrm->orig), (int)(nrm->repl));
1170 } else {
Neels Hofmeyr508514c2015-11-24 13:30:38 +01001171 nr_map_refresh(map, nrm, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001172 }
1173
1174 OSMO_ASSERT(nrm);
1175 return nrm;
1176}
1177
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001178static void gtphub_map_seq(struct gtp_packet_desc *p,
1179 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001180 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001181{
1182 /* Store a mapping in to_peer's map, so when we later receive a GTP
1183 * packet back from to_peer, the seq nr can be unmapped back to its
1184 * origin (from_peer here). */
1185 struct nr_mapping *nrm;
1186 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001187 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001188
1189 /* Change the GTP packet to yield the new, mapped seq nr */
1190 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001191}
1192
1193static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1194 struct gtphub_peer_port *responding_port)
1195{
1196 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001197 struct nr_mapping *nrm =
1198 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1199 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001200 if (!nrm)
1201 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001202 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1203 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001204 set_seq(p, nrm->orig);
1205 return nrm->origin;
1206}
1207
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001208static int gtphub_check_mapped_tei(struct gtphub_tunnel_endpoint *new_te,
1209 struct gtphub_tunnel_endpoint *iterated_te,
1210 uint32_t *tei_min,
1211 uint32_t *tei_max)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001212{
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001213 if (new_te->tei_repl != iterated_te->tei_repl)
1214 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001215
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001216 /* new_te->tei_repl is already taken. Try to find one out of the known
1217 * range. */
1218
1219 if ((*tei_max) < 0xffffffff) {
1220 (*tei_max)++;
1221 new_te->tei_repl = *tei_max;
1222 return 1;
1223 } else if ((*tei_min) > 1) {
1224 (*tei_min)--;
1225 new_te->tei_repl = *tei_min;
1226 return 1;
1227 }
1228
1229 /* None seems to be available. */
1230 return 0;
1231}
1232
1233static int gtphub_check_reused_teis(struct gtphub *hub,
1234 struct gtphub_tunnel *new_tun)
1235{
1236 uint32_t tei_min = 0xffffffff;
1237 uint32_t tei_max = 0;
1238 int side_idx;
1239 int plane_idx;
1240 int side_idx2;
1241 int plane_idx2;
1242 struct gtphub_tunnel_endpoint *te;
1243 struct gtphub_tunnel_endpoint *te2;
1244
1245 struct gtphub_tunnel *tun, *ntun;
1246
1247 llist_for_each_entry_safe(tun, ntun, &hub->tunnels, entry) {
1248 if (tun == new_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001249 continue;
1250
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001251 /* Check whether the GSN sent a TEI that it is reusing from a
1252 * previous tunnel. */
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001253 int tun_continue = 0;
1254 for_each_side(side_idx) {
1255 for_each_plane(plane_idx) {
1256 te = &tun->endpoint[side_idx][plane_idx];
1257 te2 = &new_tun->endpoint[side_idx][plane_idx];
1258 if ((te->tei_orig != te2->tei_orig)
1259 || (!te->peer)
1260 || (!te2->peer)
1261 || !gsn_addr_same(&te->peer->peer_addr->addr,
1262 &te2->peer->peer_addr->addr))
1263 continue;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001264
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001265 /* The peer is reusing a TEI that I believe to
1266 * be part of another tunnel. The other tunnel
1267 * must be stale, then. */
1268 LOG(LOGL_NOTICE,
1269 "Expiring tunnel due to reused TEI:"
1270 " peer %s sent %s TEI %x,"
1271 " previously used by tunnel %s...\n",
1272 gtphub_port_str(te->peer),
1273 gtphub_plane_idx_names[plane_idx],
1274 te->tei_orig,
1275 gtphub_tunnel_str(tun));
1276 LOG(LOGL_NOTICE, "...while establishing tunnel %s\n",
1277 gtphub_tunnel_str(new_tun));
Neels Hofmeyr52c0bd32015-12-02 14:14:32 +01001278
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001279 expiring_item_del(&tun->expiry_entry);
1280 /* continue to find more matches. There shouldn't be
1281 * any, but let's make sure. However, tun is deleted,
1282 * so we need to skip to the next tunnel. */
1283 tun_continue = 1;
1284 break;
1285 }
1286 if (tun_continue)
1287 break;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001288 }
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001289 if (tun_continue)
1290 continue;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001291
1292 /* Check whether the mapped TEIs assigned to the endpoints are
1293 * used anywhere else. */
1294 for_each_side_and_plane(side_idx, plane_idx) {
1295 te = &tun->endpoint[side_idx][plane_idx];
1296 tei_min = (tei_min < te->tei_repl)? tei_min : te->tei_repl;
1297 tei_max = (tei_max > te->tei_repl)? tei_max : te->tei_repl;
1298
1299 for_each_side_and_plane(side_idx2, plane_idx2) {
1300 te2 = &new_tun->endpoint[side_idx2][plane_idx2];
1301 if (!gtphub_check_mapped_tei(te2, te, &tei_min, &tei_max)) {
1302 LOG(LOGL_ERROR,
1303 "No mapped TEI is readily available."
1304 " Searching for holes between occupied"
1305 " TEIs not implemented.");
1306 return 0;
1307 }
1308 }
1309 }
1310
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001311 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001312
1313 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001314}
1315
1316static void gtphub_tunnel_refresh(struct gtphub *hub,
1317 struct gtphub_tunnel *tun,
1318 time_t now)
1319{
1320 expiry_add(&hub->expire_slowly,
1321 &tun->expiry_entry,
1322 now);
1323}
1324
1325static struct gtphub_tunnel_endpoint *gtphub_unmap_tei(struct gtphub *hub,
1326 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001327 struct gtphub_peer_port *from,
1328 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001329{
1330 OSMO_ASSERT(from);
1331 int other_side = other_side_idx(p->side_idx);
1332
1333 struct gtphub_tunnel *tun;
1334 llist_for_each_entry(tun, &hub->tunnels, entry) {
1335 struct gtphub_tunnel_endpoint *te_from =
1336 &tun->endpoint[p->side_idx][p->plane_idx];
1337 struct gtphub_tunnel_endpoint *te_to =
1338 &tun->endpoint[other_side][p->plane_idx];
1339 if ((te_to->tei_repl == p->header_tei_rx)
Neels Hofmeyrfc1be3a2015-12-02 00:31:31 +01001340 && te_from->peer
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001341 && gsn_addr_same(&te_from->peer->peer_addr->addr,
1342 &from->peer_addr->addr)) {
1343 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001344 if (unmapped_from_tun)
1345 *unmapped_from_tun = tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001346 return te_to;
1347 }
1348 }
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001349
1350 if (unmapped_from_tun)
1351 *unmapped_from_tun = NULL;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001352 return NULL;
1353}
1354
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001355static void gtphub_map_restart_counter(struct gtphub *hub,
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001356 struct gtp_packet_desc *p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001357{
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001358 if (p->rc != GTP_RC_PDU_C)
1359 return;
1360
1361 int ie_idx;
1362 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1363 if (ie_idx < 0)
1364 return;
1365
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001366 /* Always send gtphub's own restart counter */
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001367 p->ie[ie_idx]->tv1.v = hton8(hub->restart_counter);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001368}
1369
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001370static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001371 struct gtphub_tunnel **unmapped_from_tun,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001372 struct gtphub *hub,
1373 struct gtp_packet_desc *p,
1374 struct gtphub_peer_port *from_port)
1375{
1376 OSMO_ASSERT(p->version == 1);
1377 *to_port_p = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001378 if (unmapped_from_tun)
1379 *unmapped_from_tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001380
1381 /* If the header's TEI is zero, no PDP context has been established
1382 * yet. If nonzero, a mapping should actually already exist for this
1383 * TEI, since it must have been announced in a PDP context creation. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001384 if (!p->header_tei_rx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001385 return 0;
1386
1387 /* to_peer has previously announced a TEI, which was stored and
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001388 * mapped in a tunnel struct. */
1389 struct gtphub_tunnel_endpoint *to;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001390 to = gtphub_unmap_tei(hub, p, from_port, unmapped_from_tun);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001391 if (!to) {
1392 LOG(LOGL_ERROR, "Received unknown TEI %" PRIx32 " from %s\n",
1393 p->header_tei_rx, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001394 return -1;
1395 }
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001396 OSMO_ASSERT(*unmapped_from_tun);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001397
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001398 uint32_t unmapped_tei = to->tei_orig;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001399 set_tei(p, unmapped_tei);
1400
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001401 LOG(LOGL_DEBUG, "Unmapped TEI coming from: %s\n",
1402 gtphub_tunnel_str(*unmapped_from_tun));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001403
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001404 /* May be NULL for an invalidated tunnel. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001405 *to_port_p = to->peer;
1406
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001407 return 0;
1408}
1409
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001410static int gtphub_handle_create_pdp_ctx(struct gtphub *hub,
1411 struct gtp_packet_desc *p,
1412 struct gtphub_peer_port *from_ctrl,
1413 struct gtphub_peer_port *to_ctrl)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001414{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001415 int plane_idx;
1416
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001417 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1418 plane_nrs_match_GSN_addr_IE_indices);
1419
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001420 struct gtphub_tunnel *tun = p->tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001421
1422 if (p->type == GTP_CREATE_PDP_REQ) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001423 if (p->side_idx != GTPH_SIDE_SGSN) {
1424 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001425 " Request from the GGSN side: %s",
1426 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001427 return -1;
1428 }
1429
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001430 if (tun) {
1431 LOG(LOGL_ERROR, "Not implemented: Received"
1432 " Create PDP Context Request for an already"
1433 " established tunnel:"
1434 " from %s, tunnel %s\n",
1435 gtphub_port_str(from_ctrl),
1436 gtphub_tunnel_str(p->tun));
1437 return -1;
1438 }
1439
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001440 /* A new tunnel. */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001441 p->tun = tun = gtphub_tunnel_new();
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001442 llist_add(&tun->entry, &hub->tunnels);
1443 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001444 /* The endpoint peers on this side (SGSN) will be set from IEs
1445 * below. Also set the GGSN Ctrl endpoint, for logging. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001446 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001447 to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001448 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001449 if (p->side_idx != GTPH_SIDE_GGSN) {
1450 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001451 " Response from the SGSN side: %s",
1452 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001453 return -1;
1454 }
1455
Neels Hofmeyr8c5b0732015-12-03 13:45:15 +01001456 /* The tunnel should already have been resolved from the header
1457 * TEI and be available in tun (== p->tun). Just fill in the
1458 * GSN Addresses below.*/
1459 OSMO_ASSERT(tun);
1460 OSMO_ASSERT(tun->endpoint[other_side_idx(p->side_idx)][GTPH_PLANE_CTRL].tei_repl
1461 == p->header_tei_rx);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001462 OSMO_ASSERT(to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001463 }
1464
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001465 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1466 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001467 unsigned int side_idx = p->side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001468
1469 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01001470 int rc;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001471 struct gsn_addr use_addr;
1472 uint16_t use_port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001473 uint32_t tei_from_ie;
1474 int ie_idx;
1475
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001476 /* Fetch GSN Address and TEI from IEs. As ensured by above
1477 * static asserts, plane_idx corresponds to the GSN Address IE
1478 * index (the first one = 0 = ctrl, second one = 1 = user). */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001479 rc = gsn_addr_get(&use_addr, p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001480 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001481 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1482 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001483 return -1;
1484 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001485 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001486 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001487 gsn_addr_to_str(&use_addr),
1488 use_addr.len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001489
1490 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1491 if (ie_idx < 0) {
1492 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001493 LOG(LOGL_ERROR,
1494 "Create PDP Context message invalid:"
1495 " missing IE %d\n",
1496 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001497 return -1;
1498 }
1499 tei_from_ie = 0;
1500 }
1501 else
1502 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1503
1504 /* Make sure an entry for this peer address with default port
Neels Hofmeyrca2361c2015-12-03 14:12:44 +01001505 * exists.
1506 *
1507 * Exception: if sgsn_use_sender is set, instead use the
1508 * sender's address and port for Ctrl -- the User port is not
1509 * known until the first User packet arrives.
1510 *
1511 * Note: doing this here is just an optimization, because
1512 * gtphub_handle_buf() has code to replace the tunnel
1513 * endpoints' addresses with the sender (needed for User
1514 * plane). We could just ignore sgsn_use_sender here. But if we
1515 * set up a default port here and replace it in
1516 * gtphub_handle_buf(), we'd be creating a peer port just to
1517 * expire it right away. */
1518 if (hub->sgsn_use_sender && (side_idx == GTPH_SIDE_SGSN)) {
1519 gsn_addr_from_sockaddr(&use_addr, &use_port, &from_ctrl->sa);
1520 } else {
1521 use_port = gtphub_plane_idx_default_port[plane_idx];
1522
1523 }
1524
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001525 struct gtphub_peer_port *peer_from_ie;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001526 peer_from_ie = gtphub_port_have(hub,
1527 &hub->to_gsns[side_idx][plane_idx],
1528 &use_addr, use_port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001529
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001530 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1531 peer_from_ie);
1532
Neels Hofmeyr59c1b642015-12-03 11:30:43 +01001533 if (!tei_from_ie &&
1534 !tun->endpoint[side_idx][plane_idx].tei_orig) {
1535 LOG(LOGL_ERROR,
1536 "Create PDP Context message omits %s TEI, but the"
1537 " no TEI has been announced for this tunnel: %s",
1538 gtphub_plane_idx_names[plane_idx],
1539 gtphub_tunnel_str(tun));
1540 return -1;
1541 }
1542
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001543 if (tei_from_ie) {
1544 /* Create TEI mapping and replace in GTP packet IE */
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01001545 uint32_t mapped_tei = nr_pool_next(&hub->tei_pool);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001546
1547 tun->endpoint[side_idx][plane_idx].tei_orig = tei_from_ie;
1548 tun->endpoint[side_idx][plane_idx].tei_repl = mapped_tei;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001549 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001550
Neels Hofmeyrcd865d62015-11-30 12:58:48 +01001551 if (!gtphub_check_reused_teis(hub, tun)) {
1552 /* It's highly unlikely that all TEIs are
1553 * taken. But the code looking for an unused
1554 * TEI is, at the time of writing this comment,
1555 * not able to find gaps in the TEI space. To
1556 * explicitly alert the user of this problem,
1557 * rather abort than carry on. */
1558 LOG(LOGL_FATAL, "TEI range exhausted. Cannot create TEI mapping, aborting.\n");
1559 abort();
1560 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001561 }
1562
1563 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001564 rc = gsn_addr_put(&hub->to_gsns[other_side_idx(side_idx)][plane_idx].local_addr,
1565 p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001566 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001567 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1568 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001569 return -1;
1570 }
1571 }
1572
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001573 if (p->type == GTP_CREATE_PDP_REQ) {
1574 LOG(LOGL_DEBUG, "New tunnel, first half: %s\n",
1575 gtphub_tunnel_str(tun));
Neels Hofmeyr005f1752015-11-30 12:19:11 +01001576 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001577 LOG(LOGL_DEBUG, "New tunnel: %s\n",
1578 gtphub_tunnel_str(tun));
1579 }
1580
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001581 return 0;
1582}
1583
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001584static void pending_delete_del_cb(struct expiring_item *expi)
1585{
1586 struct pending_delete *pd;
1587 pd = container_of(expi, struct pending_delete, expiry_entry);
1588
1589 llist_del(&pd->entry);
1590 INIT_LLIST_HEAD(&pd->entry);
1591
1592 pd->expiry_entry.del_cb = 0;
1593 expiring_item_del(&pd->expiry_entry);
1594
1595 talloc_free(pd);
1596}
1597
1598static struct pending_delete *pending_delete_new(void)
1599{
1600 struct pending_delete *pd = talloc_zero(osmo_gtphub_ctx, struct pending_delete);
1601 INIT_LLIST_HEAD(&pd->entry);
1602 expiring_item_init(&pd->expiry_entry);
1603 pd->expiry_entry.del_cb = pending_delete_del_cb;
1604 return pd;
1605}
1606
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001607static int gtphub_handle_delete_pdp_ctx(struct gtphub *hub,
1608 struct gtp_packet_desc *p,
1609 struct gtphub_peer_port *from_ctrl,
1610 struct gtphub_peer_port *to_ctrl)
1611{
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001612 struct gtphub_tunnel *known_tun = p->tun;
1613
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001614 if (p->type == GTP_DELETE_PDP_REQ) {
1615 if (!known_tun) {
1616 LOG(LOGL_ERROR, "Cannot find tunnel for Delete PDP Context Request.\n");
1617 return -1;
1618 }
1619
1620 /* Store the Delete Request until a successful Response is seen. */
1621 uint8_t teardown_ind;
1622 uint8_t nsapi;
1623
1624 if (gtpie_gettv1(p->ie, GTPIE_TEARDOWN, 0, &teardown_ind) != 0) {
1625 LOG(LOGL_ERROR, "Missing Teardown Ind IE in Delete PDP Context Request.\n");
1626 return -1;
1627 }
1628
1629 if (gtpie_gettv1(p->ie, GTPIE_NSAPI, 0, &nsapi) != 0) {
1630 LOG(LOGL_ERROR, "Missing NSAPI IE in Delete PDP Context Request.\n");
1631 return -1;
1632 }
1633
1634 struct pending_delete *pd = NULL;
1635
1636 struct pending_delete *pdi = NULL;
1637 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1638 if ((pdi->tun == known_tun)
1639 && (pdi->teardown_ind == teardown_ind)
1640 && (pdi->nsapi == nsapi)) {
1641 pd = pdi;
1642 break;
1643 }
1644 }
1645
1646 if (!pd) {
1647 pd = pending_delete_new();
1648 pd->tun = known_tun;
1649 pd->teardown_ind = teardown_ind;
1650 pd->nsapi = nsapi;
1651
1652 LOG(LOGL_DEBUG, "Tunnel delete pending: %s\n",
1653 gtphub_tunnel_str(known_tun));
1654 llist_add(&pd->entry, &hub->pending_deletes);
1655 }
1656
1657 /* Add or refresh timeout. */
1658 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1659
1660 /* If a pending_delete should expire before the response to
1661 * indicate success comes in, the responding peer will have the
1662 * tunnel deactivated, while the requesting peer gets no reply
1663 * and keeps the tunnel. The hope is that the requesting peer
1664 * will try again and get a useful response. */
1665 } else if (p->type == GTP_DELETE_PDP_RSP) {
1666 /* Find the Delete Request for this Response. */
1667 struct pending_delete *pd = NULL;
1668
1669 struct pending_delete *pdi;
1670 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1671 if (known_tun == pdi->tun) {
1672 pd = pdi;
1673 break;
1674 }
1675 }
1676
1677 if (!pd) {
1678 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1679 " Cannot find matching request.");
1680 /* If we delete the tunnel now, anyone can send a
1681 * Delete response to kill tunnels at will. */
1682 return -1;
1683 }
1684
1685 /* TODO handle teardown_ind and nsapi */
1686
1687 expiring_item_del(&pd->expiry_entry);
1688
1689 uint8_t cause;
1690 if (gtpie_gettv1(p->ie, GTPIE_CAUSE, 0, &cause) != 0) {
1691 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1692 " Missing Cause IE.");
1693 /* If we delete the tunnel now, at least one of the
1694 * peers may still think it is active. */
1695 return -1;
1696 }
1697
1698 if (cause != GTPCAUSE_ACC_REQ) {
1699 LOG(LOGL_NOTICE,
1700 "Delete PDP Context Response indicates failure;"
1701 "for %s\n",
1702 gtphub_tunnel_str(known_tun));
1703 return -1;
1704 }
1705
1706 LOG(LOGL_DEBUG, "Delete PDP Context: removing tunnel %s\n",
1707 gtphub_tunnel_str(known_tun));
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001708 p->tun = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001709 expiring_item_del(&known_tun->expiry_entry);
1710 }
1711
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001712 return 0;
1713}
1714
1715static int gtphub_handle_update_pdp_ctx(struct gtphub *hub,
1716 struct gtp_packet_desc *p,
1717 struct gtphub_peer_port *from_ctrl,
1718 struct gtphub_peer_port *to_ctrl)
1719{
1720 /* TODO */
1721 return 0;
1722}
1723
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001724/* Read GSN address IEs from p, and make sure these peer addresses exist in
1725 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1726 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1727 * the packet p. */
1728static int gtphub_handle_pdp_ctx(struct gtphub *hub,
1729 struct gtp_packet_desc *p,
1730 struct gtphub_peer_port *from_ctrl,
1731 struct gtphub_peer_port *to_ctrl)
1732{
1733 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1734
1735 switch (p->type) {
1736 case GTP_CREATE_PDP_REQ:
1737 case GTP_CREATE_PDP_RSP:
1738 return gtphub_handle_create_pdp_ctx(hub, p,
1739 from_ctrl, to_ctrl);
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001740
1741 case GTP_DELETE_PDP_REQ:
1742 case GTP_DELETE_PDP_RSP:
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001743 return gtphub_handle_delete_pdp_ctx(hub, p,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001744 from_ctrl, to_ctrl);
1745
1746 case GTP_UPDATE_PDP_REQ:
1747 case GTP_UPDATE_PDP_RSP:
1748 return gtphub_handle_update_pdp_ctx(hub, p,
1749 from_ctrl, to_ctrl);
1750
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001751 default:
1752 /* Nothing to do for this message type. */
1753 return 0;
1754 }
1755
1756}
1757
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001758static int gtphub_send_del_pdp_ctx(struct gtphub *hub,
1759 struct gtphub_tunnel *tun,
1760 int to_side)
1761{
1762 static uint8_t del_ctx_msg[16] = {
1763 0x32, /* GTP v1 flags */
1764 GTP_DELETE_PDP_REQ,
1765 0x00, 0x08, /* Length in network byte order */
1766 0x00, 0x00, 0x00, 0x00, /* TEI to be replaced */
1767 0, 0, /* Seq, to be replaced */
1768 0, 0, /* no extensions */
1769 0x13, 0xff, /* 19: Teardown ind = 1 */
1770 0x14, 0 /* 20: NSAPI = 0 */
1771 };
1772
1773 uint32_t *tei = (uint32_t*)&del_ctx_msg[4];
1774 uint16_t *seq = (uint16_t*)&del_ctx_msg[8];
1775
1776 struct gtphub_tunnel_endpoint *te =
1777 &tun->endpoint[to_side][GTPH_PLANE_CTRL];
1778
1779 if (! te->peer)
1780 return 0;
1781
1782 *tei = hton32(te->tei_orig);
1783 *seq = hton16(nr_pool_next(&te->peer->peer_addr->peer->seq_pool));
1784
1785 struct gtphub_bind *to_bind = &hub->to_gsns[to_side][GTPH_PLANE_CTRL];
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01001786 int rc = gtphub_write(&to_bind->ofd, &te->peer->sa,
1787 del_ctx_msg, sizeof(del_ctx_msg));
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001788 if (rc != 0) {
1789 LOG(LOGL_ERROR,
1790 "Failed to send out-of-band Delete PDP Context Request to %s\n",
1791 gtphub_port_str(te->peer));
1792 }
1793 return rc;
1794}
1795
1796/* Tell all peers on the other end of tunnels that PDP contexts are void. */
1797static void gtphub_restarted(struct gtphub *hub,
1798 struct gtp_packet_desc *p,
1799 struct gtphub_peer_port *pp)
1800{
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001801 LOG(LOGL_DEBUG, "Peer has restarted: %s\n",
1802 gtphub_port_str(pp));
1803
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001804 struct gtphub_tunnel *tun;
1805 llist_for_each_entry(tun, &hub->tunnels, entry) {
1806 int side_idx;
1807 for_each_side(side_idx) {
Neels Hofmeyrc6d51f52015-12-02 15:44:34 +01001808 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
1809 struct gtphub_tunnel_endpoint *te2 = &tun->endpoint[other_side_idx(side_idx)][GTPH_PLANE_CTRL];
1810 if ((!te->peer)
1811 || (!te2->tei_orig)
1812 || (pp->peer_addr->peer != te->peer->peer_addr->peer))
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001813 continue;
1814
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001815 LOG(LOGL_DEBUG, "Deleting tunnel due to peer restart: %s\n",
1816 gtphub_tunnel_str(tun));
1817
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001818 /* Send a Delete PDP Context Request to the
1819 * peer on the other side, remember the pending
1820 * delete and wait for the response to delete
1821 * the tunnel. Clear this side of the tunnel to
1822 * make sure it isn't used.
1823 *
1824 * Should the delete message send fail, or if no
1825 * response is received, this tunnel will expire. If
1826 * its TEIs come up in a new PDP Context Request, it
1827 * will be removed. If messages for this tunnel should
1828 * come in (from the not restarted side), they will be
1829 * dropped because the tunnel is rendered unusable. */
1830 gtphub_send_del_pdp_ctx(hub, tun, other_side_idx(side_idx));
1831
1832 struct pending_delete *pd;
1833 pd = pending_delete_new();
1834 pd->tun = tun;
1835 pd->teardown_ind = 0xff;
1836 pd->nsapi = 0;
1837 llist_add(&pd->entry, &hub->pending_deletes);
1838 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1839
1840 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_CTRL],
1841 NULL);
1842 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_USER],
1843 NULL);
1844 }
1845 }
1846}
1847
1848static int get_restart_count(struct gtp_packet_desc *p)
1849{
1850 int ie_idx;
1851 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1852 if (ie_idx < 0)
1853 return -1;
1854 return ntoh8(p->ie[ie_idx]->tv1.v);
1855}
1856
1857static void gtphub_check_restart_counter(struct gtphub *hub,
1858 struct gtp_packet_desc *p,
1859 struct gtphub_peer_port *from)
1860{
1861 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1862 * that doesn't match the peer's previously sent restart counter, clear
1863 * that peer and cancel PDP contexts. */
1864
1865 int restart = get_restart_count(p);
1866
1867 if ((restart < 0) || (restart > 255))
1868 return;
1869
1870 if ((from->last_restart_count >= 0) && (from->last_restart_count <= 255)) {
1871 if (from->last_restart_count != restart) {
1872 gtphub_restarted(hub, p, from);
1873 }
1874 }
1875
1876 from->last_restart_count = restart;
1877}
1878
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001879static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1880{
1881 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1882 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
1883 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1884 gtphub_plane_idx_names[plane_idx]);
1885
1886 if (!(what & BSC_FD_READ))
1887 return 0;
1888
1889 struct gtphub *hub = from_sgsns_ofd->data;
1890
1891 static uint8_t buf[4096];
1892 struct osmo_sockaddr from_addr;
1893 struct osmo_sockaddr to_addr;
1894 struct osmo_fd *to_ofd;
1895 int len;
1896 uint8_t *reply_buf;
1897
1898 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1899 if (len < 1)
1900 return 0;
1901
1902 len = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, &from_addr,
1903 buf, len, gtphub_now(),
1904 &reply_buf, &to_ofd, &to_addr);
1905 if (len < 1)
1906 return 0;
1907
1908 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
1909}
1910
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001911static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1912{
1913 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1914 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001915 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1916 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001917 if (!(what & BSC_FD_READ))
1918 return 0;
1919
1920 struct gtphub *hub = from_ggsns_ofd->data;
1921
1922 static uint8_t buf[4096];
1923 struct osmo_sockaddr from_addr;
1924 struct osmo_sockaddr to_addr;
1925 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001926 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001927 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001928
1929 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1930 if (len < 1)
1931 return 0;
1932
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001933 len = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, &from_addr,
1934 buf, len, gtphub_now(),
1935 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001936 if (len < 1)
1937 return 0;
1938
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001939 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001940}
1941
1942static int gtphub_unmap(struct gtphub *hub,
1943 struct gtp_packet_desc *p,
1944 struct gtphub_peer_port *from,
1945 struct gtphub_peer_port *to_proxy,
1946 struct gtphub_peer_port **final_unmapped,
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001947 struct gtphub_peer_port **unmapped_from_seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001948{
1949 /* Always (try to) unmap sequence and TEI numbers, which need to be
1950 * replaced in the packet. Either way, give precedence to the proxy, if
1951 * configured. */
1952
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001953 if (unmapped_from_seq)
1954 *unmapped_from_seq = NULL;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001955 if (final_unmapped)
1956 *final_unmapped = NULL;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001957 p->tun = NULL;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001958
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001959 struct gtphub_peer_port *from_seq = NULL;
1960 struct gtphub_peer_port *from_tei = NULL;
1961 struct gtphub_peer_port *unmapped = NULL;
1962
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001963 from_seq = gtphub_unmap_seq(p, from);
1964
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001965 if (gtphub_unmap_header_tei(&from_tei, &p->tun, hub, p, from) < 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001966 return -1;
1967
1968 struct gtphub_peer *from_peer = from->peer_addr->peer;
1969 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001970 LOG(LOGL_DEBUG,
1971 "Seq unmap and TEI unmap yield two different peers."
1972 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001973 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1974 gtphub_plane_idx_names[p->plane_idx],
1975 gtphub_peer_str(from_peer),
1976 (int)p->seq,
1977 gtphub_port_str(from_seq),
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001978 (unsigned int)p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001979 gtphub_port_str2(from_tei)
1980 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001981 }
1982 unmapped = (from_seq? from_seq : from_tei);
1983
1984 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001985 LOG(LOGL_NOTICE,
1986 "Unmap yields a different peer than the configured proxy."
1987 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001988 " unmapped: %s proxy: %s\n",
1989 gtphub_port_str(unmapped),
1990 gtphub_port_str2(to_proxy)
1991 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001992 }
1993 unmapped = (to_proxy? to_proxy : unmapped);
1994
1995 if (!unmapped) {
1996 /* Return no error, but returned pointers are all NULL. */
1997 return 0;
1998 }
1999
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002000 if (unmapped_from_seq)
2001 *unmapped_from_seq = from_seq;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002002 if (final_unmapped)
2003 *final_unmapped = unmapped;
2004 return 0;
2005}
2006
2007static int gsn_addr_to_sockaddr(struct gsn_addr *src,
2008 uint16_t port,
2009 struct osmo_sockaddr *dst)
2010{
2011 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
2012}
2013
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002014/* If p is an Echo request, replace p's data with the matching response and
2015 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
2016 * detected. */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002017static int gtphub_handle_echo_req(struct gtphub *hub, struct gtp_packet_desc *p,
2018 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002019{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002020 if (p->type != GTP_ECHO_REQ)
2021 return 0;
2022
2023 static uint8_t echo_response_data[14] = {
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002024 0x32, /* GTP v1 flags */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002025 GTP_ECHO_RSP,
2026 0x00, 14 - 8, /* Length in network byte order */
2027 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
2028 0, 0, /* Seq, to be replaced */
2029 0, 0, /* no extensions */
2030 0x0e, /* Recovery IE */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002031 0 /* Restart counter, to be replaced */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002032 };
2033 uint16_t *seq = (uint16_t*)&echo_response_data[8];
2034 uint8_t *recovery = &echo_response_data[13];
2035
2036 *seq = hton16(p->seq);
2037 *recovery = hub->restart_counter;
2038
2039 *reply_buf = echo_response_data;
2040
2041 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002042}
2043
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002044struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2045 const struct osmo_sockaddr *addr);
2046
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002047/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
2048 * address to forward to. Return a pointer to the osmo_fd, but copy the
2049 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
2050 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
2051 * bytes to forward, 0 or less on failure. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002052int gtphub_handle_buf(struct gtphub *hub,
2053 unsigned int side_idx,
2054 unsigned int plane_idx,
2055 const struct osmo_sockaddr *from_addr,
2056 uint8_t *buf,
2057 size_t received,
2058 time_t now,
2059 uint8_t **reply_buf,
2060 struct osmo_fd **to_ofd,
2061 struct osmo_sockaddr *to_addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002062{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002063 struct gtphub_bind *from_bind = &hub->to_gsns[side_idx][plane_idx];
2064 struct gtphub_bind *to_bind = &hub->to_gsns[other_side_idx(side_idx)][plane_idx];
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002065
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002066 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
2067 received);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002068 LOG(LOGL_DEBUG, "%s rx %s from %s %s\n",
2069 (side_idx == GTPH_SIDE_GGSN)? "<-" : "->",
Neels Hofmeyre921e322015-11-11 00:45:50 +01002070 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002071 gtphub_side_idx_names[side_idx],
Neels Hofmeyre921e322015-11-11 00:45:50 +01002072 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002073
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01002074 struct gtp_packet_desc p;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002075 gtp_decode(buf, received, side_idx, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002076
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002077 if (p.rc <= 0) {
2078 LOG(LOGL_ERROR, "INVALID: dropping GTP packet from %s %s %s\n",
2079 gtphub_side_idx_names[side_idx],
2080 gtphub_plane_idx_names[plane_idx],
2081 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002082 return -1;
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002083 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002084
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002085 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2086
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002087 int reply_len;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002088 reply_len = gtphub_handle_echo_req(hub, &p, reply_buf);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002089 if (reply_len > 0) {
2090 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002091 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002092 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002093
2094 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2095 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2096 reply_len);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002097 LOG(LOGL_DEBUG, "%s Echo response to %s: %d bytes to %s\n",
2098 (side_idx == GTPH_SIDE_GGSN)? "-->" : "<--",
2099 gtphub_side_idx_names[side_idx],
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002100 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002101 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002102 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002103 if (reply_len < 0)
2104 return -1;
2105
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002106 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002107
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002108 /* If a proxy is configured, check that it's indeed that proxy talking
2109 * to us. A proxy is a forced 1:1 connection, e.g. to another gtphub,
2110 * so no-one else is allowed to talk to us from that side. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002111 struct gtphub_peer_port *from_peer = hub->proxy[side_idx][plane_idx];
2112 if (from_peer) {
2113 if (osmo_sockaddr_cmp(&from_peer->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002114 LOG(LOGL_ERROR,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002115 "Rejecting: %s proxy configured, but GTP packet"
2116 " received on %s bind is from another sender:"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002117 " proxy: %s sender: %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002118 gtphub_side_idx_names[side_idx],
2119 gtphub_side_idx_names[side_idx],
2120 gtphub_port_str(from_peer),
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002121 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002122 return -1;
2123 }
2124 }
2125
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002126 if (!from_peer) {
2127 /* Find or create a peer with a matching address. The sender's
2128 * port may in fact differ. */
2129 from_peer = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002130 }
2131
2132 /* If any PDP context has been created, we already have an entry for
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002133 * this GSN. If we don't have an entry, a GGSN has nothing to tell us
2134 * about, while an SGSN may initiate a PDP context. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002135 if (!from_peer) {
2136 if (side_idx == GTPH_SIDE_GGSN) {
2137 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
2138 osmo_sockaddr_to_str(from_addr));
2139 return -1;
2140 } else {
2141 /* SGSN */
2142 /* A new peer. If this is on the Ctrl plane, an SGSN
2143 * may make first contact without being known yet, so
2144 * create the peer struct for the current sender. */
2145 if (plane_idx != GTPH_PLANE_CTRL) {
2146 LOG(LOGL_ERROR,
2147 "Dropping packet: User plane peer was not"
2148 "announced by PDP Context: %s\n",
2149 osmo_sockaddr_to_str(from_addr));
2150 return -1;
2151 }
2152
2153 struct gsn_addr from_gsna;
2154 uint16_t from_port;
2155 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
2156 return -1;
2157
2158 from_peer = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
2159 }
2160 }
2161
2162 if (!from_peer) {
2163 /* This could theoretically happen for invalid address data or
2164 * somesuch. */
2165 LOG(LOGL_ERROR, "Dropping packet: invalid %s peer: %s\n",
2166 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002167 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002168 return -1;
2169 }
2170
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002171 LOG(LOGL_DEBUG, "from %s peer: %s\n", gtphub_side_idx_names[side_idx],
2172 gtphub_port_str(from_peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002173
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002174 struct gtphub_peer_port *to_peer_from_seq;
2175 struct gtphub_peer_port *to_peer;
2176 if (gtphub_unmap(hub, &p, from_peer,
2177 hub->proxy[other_side_idx(side_idx)][plane_idx],
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002178 &to_peer, &to_peer_from_seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002179 != 0) {
2180 return -1;
2181 }
2182
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002183 if ((!to_peer) && (side_idx == GTPH_SIDE_SGSN)) {
2184 if (gtphub_resolve_ggsn(hub, &p, &to_peer) < 0)
2185 return -1;
2186 }
2187
2188 if (!to_peer) {
2189 LOG(LOGL_ERROR, "No %s to send to. Dropping packet.\n",
2190 gtphub_side_idx_names[other_side_idx(side_idx)]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002191 return -1;
2192 }
2193
2194 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002195 /* This may be a Create PDP Context response. If it is, there
2196 * are other addresses in the GTP message to set up apart from
2197 * the sender. */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002198 if (gtphub_handle_pdp_ctx(hub, &p, from_peer, to_peer)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002199 != 0)
2200 return -1;
2201 }
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002202
2203 /* Either to_peer was resolved from an existing tunnel,
2204 * or a PDP Ctx and thus a tunnel has just been created,
2205 * or the tunnel has been deleted due to this message. */
2206 OSMO_ASSERT(p.tun || (p.type == GTP_DELETE_PDP_RSP));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002207
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002208 gtphub_check_restart_counter(hub, &p, from_peer);
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002209 gtphub_map_restart_counter(hub, &p);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002210
2211 /* If the GGSN is replying to an SGSN request, the sequence nr has
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002212 * already been unmapped above (to_peer_from_seq != NULL), and we need not
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002213 * create a new mapping. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002214 if (!to_peer_from_seq)
2215 gtphub_map_seq(&p, from_peer, to_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002216
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002217 osmo_sockaddr_copy(to_addr, &to_peer->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002218
2219 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01002220
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002221 if (received) {
2222 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2223 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2224 received);
2225 }
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002226 LOG(LOGL_DEBUG, "%s Forward to %s: %d bytes to %s\n",
2227 (side_idx == GTPH_SIDE_SGSN)? "-->" : "<--",
2228 gtphub_side_idx_names[other_side_idx(side_idx)],
Neels Hofmeyre921e322015-11-11 00:45:50 +01002229 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002230 return received;
2231}
2232
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002233static void resolved_gssn_del_cb(struct expiring_item *expi)
2234{
2235 struct gtphub_resolved_ggsn *ggsn;
2236 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
2237
2238 gtphub_port_ref_count_dec(ggsn->peer);
2239 llist_del(&ggsn->entry);
2240
2241 ggsn->expiry_entry.del_cb = 0;
2242 expiring_item_del(&ggsn->expiry_entry);
2243
2244 talloc_free(ggsn);
2245}
2246
2247void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
2248 struct gsn_addr *resolved_addr,
2249 time_t now)
2250{
2251 struct gtphub_peer_port *pp;
2252 struct gtphub_resolved_ggsn *ggsn;
2253
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002254 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002255 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
2256 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01002257
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002258 pp = gtphub_port_have(hub, &hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002259 resolved_addr, 2123);
2260 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002261 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
2262 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002263 return;
2264 }
2265
2266 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
2267 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01002268 INIT_LLIST_HEAD(&ggsn->entry);
2269 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002270
2271 ggsn->peer = pp;
2272 gtphub_port_ref_count_inc(pp);
2273
2274 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01002275 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002276
2277 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002278 expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002279
2280 llist_add(&ggsn->entry, &hub->resolved_ggsns);
2281}
2282
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002283static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
2284{
2285 return pp->ref_count == 0;
2286}
2287
2288static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
2289{
2290 struct gtphub_peer_port *pp, *npp;
2291 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
2292 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002293 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002294 gtphub_port_str(pp));
2295 gtphub_peer_port_del(pp);
2296 }
2297 }
2298 return llist_empty(&pa->ports);
2299}
2300
2301static int gtphub_gc_peer(struct gtphub_peer *p)
2302{
2303 struct gtphub_peer_addr *pa, *npa;
2304 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
2305 if (gtphub_gc_peer_addr(pa)) {
2306 gtphub_peer_addr_del(pa);
2307 }
2308 }
2309
2310 /* Note that there's a ref_count in each gtphub_peer_port instance
2311 * listed within p->addresses, referenced by TEI mappings from
2312 * hub->tei_map. As long as those don't expire, this peer will stay. */
2313
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002314 return llist_empty(&p->addresses)
2315 && nr_map_empty(&p->seq_map);
2316}
2317
2318static void gtphub_gc_bind(struct gtphub_bind *b)
2319{
2320 struct gtphub_peer *p, *n;
2321 llist_for_each_entry_safe(p, n, &b->peers, entry) {
2322 if (gtphub_gc_peer(p)) {
2323 gtphub_peer_del(p);
2324 }
2325 }
2326}
2327
2328void gtphub_gc(struct gtphub *hub, time_t now)
2329{
2330 int expired;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002331 expired = expiry_tick(&hub->expire_quickly, now);
2332 expired += expiry_tick(&hub->expire_slowly, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002333
2334 /* ... */
2335
2336 if (expired) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002337 int s, p;
2338 for_each_side_and_plane(s, p) {
2339 gtphub_gc_bind(&hub->to_gsns[s][p]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002340 }
2341 }
2342}
2343
2344static void gtphub_gc_cb(void *data)
2345{
2346 struct gtphub *hub = data;
2347 gtphub_gc(hub, gtphub_now());
2348 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2349}
2350
2351static void gtphub_gc_start(struct gtphub *hub)
2352{
2353 hub->gc_timer.cb = gtphub_gc_cb;
2354 hub->gc_timer.data = hub;
2355
2356 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2357}
2358
2359/* called by unit tests */
2360void gtphub_init(struct gtphub *hub)
2361{
2362 gtphub_zero(hub);
2363
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002364 INIT_LLIST_HEAD(&hub->tunnels);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002365 INIT_LLIST_HEAD(&hub->pending_deletes);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002366
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002367 expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS);
2368 expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002369
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01002370 nr_pool_init(&hub->tei_pool, 1, 0xffffffff);
2371
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002372 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002373 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002374 for_each_side_and_plane(side_idx, plane_idx) {
2375 gtphub_bind_init(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002376 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002377
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002378 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].label = "SGSN Ctrl";
2379 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].label = "GGSN Ctrl";
2380 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].label = "SGSN User";
2381 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002382}
2383
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002384/* For the test suite, this is kept separate from gtphub_stop(), which also
2385 * closes sockets. The test suite avoids using sockets and would cause
2386 * segfaults when trying to close uninitialized ofds. */
2387void gtphub_free(struct gtphub *hub)
2388{
2389 /* By expiring all mappings, a garbage collection should free
2390 * everything else. A gtphub_bind_free() will assert that everything is
2391 * indeed empty. */
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002392 expiry_clear(&hub->expire_quickly);
2393 expiry_clear(&hub->expire_slowly);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002394
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002395 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002396 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002397 for_each_side_and_plane(side_idx, plane_idx) {
2398 gtphub_gc_bind(&hub->to_gsns[side_idx][plane_idx]);
2399 gtphub_bind_free(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002400 }
2401}
2402
2403void gtphub_stop(struct gtphub *hub)
2404{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002405 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002406 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002407 for_each_side_and_plane(side_idx, plane_idx) {
2408 gtphub_bind_stop(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002409 }
2410 gtphub_free(hub);
2411}
2412
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002413static int gtphub_make_proxy(struct gtphub *hub,
2414 struct gtphub_peer_port **pp,
2415 struct gtphub_bind *bind,
2416 const struct gtphub_cfg_addr *addr)
2417{
2418 if (!addr->addr_str)
2419 return 0;
2420
2421 struct gsn_addr gsna;
2422 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
2423 return -1;
2424
2425 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
2426
2427 /* This is *the* proxy. Make sure it is never expired. */
2428 gtphub_port_ref_count_inc(*pp);
2429 return 0;
2430}
2431
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002432int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
2433 uint8_t restart_counter)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002434{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002435 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002436
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002437 hub->restart_counter = restart_counter;
Neels Hofmeyrca2361c2015-12-03 14:12:44 +01002438 hub->sgsn_use_sender = cfg->sgsn_use_sender? 1 : 0;
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002439
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002440 /* If a Ctrl plane proxy is configured, ares will never be used. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002441 if (!cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str) {
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002442 if (gtphub_ares_init(hub) != 0) {
2443 LOG(LOGL_FATAL, "Failed to initialize ares\n");
2444 return -1;
2445 }
2446 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002447
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002448 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002449 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002450 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01002451 int rc;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002452 rc = gtphub_bind_start(&hub->to_gsns[side_idx][plane_idx],
2453 &cfg->to_gsns[side_idx][plane_idx],
2454 (side_idx == GTPH_SIDE_SGSN)
2455 ? from_sgsns_read_cb
2456 : from_ggsns_read_cb,
2457 hub, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002458 if (rc) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002459 LOG(LOGL_FATAL, "Failed to bind for %ss (%s)\n",
2460 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002461 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002462 return rc;
2463 }
2464 }
2465
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002466 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002467 if (gtphub_make_proxy(hub,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002468 &hub->proxy[side_idx][plane_idx],
2469 &hub->to_gsns[side_idx][plane_idx],
2470 &cfg->proxy[side_idx][plane_idx])
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002471 != 0) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002472 LOG(LOGL_FATAL, "Cannot configure %s proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002473 " %s port %d.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002474 gtphub_side_idx_names[side_idx],
2475 cfg->proxy[side_idx][plane_idx].addr_str,
2476 (int)cfg->proxy[side_idx][plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002477 return -1;
2478 }
2479 }
2480
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002481 for_each_side_and_plane(side_idx, plane_idx) {
2482 if (hub->proxy[side_idx][plane_idx])
2483 LOG(LOGL_NOTICE, "Using %s %s proxy %s\n",
2484 gtphub_side_idx_names[side_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002485 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002486 gtphub_port_str(hub->proxy[side_idx][plane_idx]));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002487 }
2488
2489 gtphub_gc_start(hub);
2490 return 0;
2491}
2492
2493static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
2494 const struct gsn_addr *addr)
2495{
2496 struct gtphub_peer_addr *a;
2497 llist_for_each_entry(a, &peer->addresses, entry) {
2498 if (gsn_addr_same(&a->addr, addr))
2499 return a;
2500 }
2501 return NULL;
2502}
2503
2504static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2505 uint16_t port)
2506{
2507 OSMO_ASSERT(port);
2508 struct gtphub_peer_port *pp;
2509 llist_for_each_entry(pp, &a->ports, entry) {
2510 if (pp->port == port)
2511 return pp;
2512 }
2513 return NULL;
2514}
2515
2516static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2517 const struct gsn_addr *addr)
2518{
2519 struct gtphub_peer *peer;
2520 llist_for_each_entry(peer, &bind->peers, entry) {
2521 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2522 if (a)
2523 return a;
2524 }
2525 return NULL;
2526}
2527
2528static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2529 const struct gsn_addr *addr,
2530 uint16_t port)
2531{
2532 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2533 if (!a)
2534 return NULL;
2535 return gtphub_addr_find_port(a, port);
2536}
2537
2538struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2539 const struct osmo_sockaddr *addr)
2540{
2541 struct gsn_addr gsna;
2542 uint16_t port;
2543 gsn_addr_from_sockaddr(&gsna, &port, addr);
2544 return gtphub_port_find(bind, &gsna, port);
2545}
2546
2547static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2548 struct gtphub_bind *bind)
2549{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002550 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2551 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002552 OSMO_ASSERT(peer);
2553
2554 INIT_LLIST_HEAD(&peer->addresses);
2555
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002556 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002557 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002558
2559 /* TODO use something random to pick the initial sequence nr.
2560 0x6d31 produces the ASCII character sequence 'm1', currently used in
2561 gtphub_nc_test.sh. */
2562 peer->seq_pool.last_nr = 0x6d31 - 1;
2563
2564 llist_add(&peer->entry, &bind->peers);
2565 return peer;
2566}
2567
2568static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2569 const struct gsn_addr *addr)
2570{
2571 struct gtphub_peer_addr *a;
2572 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2573 OSMO_ASSERT(a);
2574 a->peer = peer;
2575 gsn_addr_copy(&a->addr, addr);
2576 INIT_LLIST_HEAD(&a->ports);
2577 llist_add(&a->entry, &peer->addresses);
2578
2579 return a;
2580}
2581
2582static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2583 struct gtphub_bind *bind,
2584 const struct gsn_addr *addr)
2585{
2586 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2587 if (a)
2588 return a;
2589
2590 /* If we haven't found an address, that means we need to create an
2591 * entirely new peer for the new address. More addresses may be added
2592 * to this peer later, but not via this function. */
2593 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002594
2595 a = gtphub_peer_add_addr(peer, addr);
2596
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002597 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002598 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002599 gsn_addr_to_str(&a->addr));
2600
2601 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002602}
2603
2604static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2605 uint16_t port)
2606{
2607 struct gtphub_peer_port *pp;
2608
2609 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2610 OSMO_ASSERT(pp);
2611 pp->peer_addr = a;
2612 pp->port = port;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002613 pp->last_restart_count = -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002614
2615 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2616 talloc_free(pp);
2617 return NULL;
2618 }
2619
2620 llist_add(&pp->entry, &a->ports);
2621
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002622 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002623 gsn_addr_to_str(&a->addr),
2624 (int)port);
2625
2626 return pp;
2627}
2628
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002629struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2630 struct gtphub_bind *bind,
2631 const struct gsn_addr *addr,
2632 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002633{
2634 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2635
2636 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2637 if (pp)
2638 return pp;
2639
2640 return gtphub_addr_add_port(a, port);
2641}
2642
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002643/* Find a GGSN peer with a matching address. If the address is known but the
2644 * port not, create a new port for that peer address. */
2645struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2646 const struct osmo_sockaddr *addr)
2647{
2648 struct gtphub_peer_addr *pa;
2649 struct gtphub_peer_port *pp;
2650
2651 struct gsn_addr gsna;
2652 uint16_t port;
2653 gsn_addr_from_sockaddr(&gsna, &port, addr);
2654
2655 pa = gtphub_addr_find(bind, &gsna);
2656 if (!pa)
2657 return NULL;
2658
2659 pp = gtphub_addr_find_port(pa, port);
2660
2661 if (!pp)
2662 pp = gtphub_addr_add_port(pa, port);
2663
2664 return pp;
2665}
2666
2667
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002668/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2669 * resolution should be possible but failed, and 1 if resolution was
2670 * successful. *pp will be set to NULL if <1 is returned. */
2671static int gtphub_resolve_ggsn(struct gtphub *hub,
2672 struct gtp_packet_desc *p,
2673 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002674{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002675 *pp = NULL;
2676
2677 /* TODO determine from message type whether IEs should be present? */
2678
2679 int rc;
2680 const char *imsi_str;
2681 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2682 if (rc < 1)
2683 return rc;
2684 OSMO_ASSERT(imsi_str);
2685
2686 const char *apn_str;
2687 rc = get_ie_apn_str(p->ie, &apn_str);
2688 if (rc < 1)
2689 return rc;
2690 OSMO_ASSERT(apn_str);
2691
2692 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2693 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002694}
2695
2696
2697/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002698/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002699/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2700 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002701static int _osmo_getaddrinfo(struct addrinfo **result,
2702 uint16_t family, uint16_t type, uint8_t proto,
2703 const char *host, uint16_t port)
2704{
2705 struct addrinfo hints;
2706 char portbuf[16];
2707
2708 sprintf(portbuf, "%u", port);
2709 memset(&hints, '\0', sizeof(struct addrinfo));
2710 hints.ai_family = family;
2711 if (type == SOCK_RAW) {
2712 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2713 * SOCK_RAW and IPPROTO_GRE is used.
2714 */
2715 hints.ai_socktype = SOCK_DGRAM;
2716 hints.ai_protocol = IPPROTO_UDP;
2717 } else {
2718 hints.ai_socktype = type;
2719 hints.ai_protocol = proto;
2720 }
2721
2722 return getaddrinfo(host, portbuf, &hints, result);
2723}
2724
2725/* TODO move to osmocom/core/socket.c ? */
2726int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2727 uint16_t family, uint16_t type, uint8_t proto,
2728 const char *host, uint16_t port)
2729{
2730 struct addrinfo *res;
2731 int rc;
2732 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2733
2734 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002735 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002736 return -EINVAL;
2737 }
2738
2739 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2740 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2741 addr->l = res->ai_addrlen;
2742 freeaddrinfo(res);
2743
2744 return 0;
2745}
2746
2747int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2748 char *port_str, size_t port_str_len,
2749 const struct osmo_sockaddr *addr,
2750 int flags)
2751{
2752 int rc;
2753
2754 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2755 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2756 return -1;
2757 }
2758
2759 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002760 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2761 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002762 return -1;
2763 }
2764
2765 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2766 addr_str, addr_str_len,
2767 port_str, port_str_len,
2768 flags);
2769
2770 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002771 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2772 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2773 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002774
2775 return rc;
2776}
2777
2778const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2779 char *buf, size_t buf_len)
2780{
2781 const int portbuf_len = 6;
2782 OSMO_ASSERT(buf_len > portbuf_len);
2783 char *portbuf = buf + buf_len - portbuf_len;
2784 buf_len -= portbuf_len;
2785 if (osmo_sockaddr_to_strs(buf, buf_len,
2786 portbuf, portbuf_len,
2787 addr,
2788 NI_NUMERICHOST | NI_NUMERICSERV))
2789 return NULL;
2790
2791 char *pos = buf + strnlen(buf, buf_len-1);
2792 size_t len = buf_len - (pos - buf);
2793
2794 snprintf(pos, len, " port %s", portbuf);
2795 buf[buf_len-1] = '\0';
2796
2797 return buf;
2798}
2799
2800const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2801{
2802 static char buf[256];
2803 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2804 if (! result)
2805 return "(invalid)";
2806 return result;
2807}
2808
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002809int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2810 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002811{
2812 if (a == b)
2813 return 0;
2814 if (!a)
2815 return -1;
2816 if (!b)
2817 return 1;
2818 if (a->l != b->l) {
2819 /* Lengths are not the same, but determine the order. Will
2820 * anyone ever sort a list by osmo_sockaddr though...? */
2821 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2822 if (cmp == 0) {
2823 if (a->l < b->l)
2824 return -1;
2825 else
2826 return 1;
2827 }
2828 return cmp;
2829 }
2830 return memcmp(&a->a, &b->a, a->l);
2831}
2832
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002833void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2834 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002835{
2836 OSMO_ASSERT(src->l <= sizeof(dst->a));
2837 memcpy(&dst->a, &src->a, src->l);
2838 dst->l = src->l;
2839}