blob: 27c70166e1300dc9845468e4e49b5097d1ea02b1 [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 Hofmeyr29d926b2015-11-24 13:27:13 +010094 time_t timestamp;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020095 union gtpie_member *ie[GTPIE_SIZE];
96};
97
Neels Hofmeyr10fc0242015-12-01 00:23:45 +010098struct pending_delete {
99 struct llist_head entry;
100 struct expiring_item expiry_entry;
101
102 struct gtphub_tunnel *tun;
103 uint8_t teardown_ind;
104 uint8_t nsapi;
105};
106
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100107
108/* counters */
109
110enum gtphub_counters_io {
111 GTPH_CTR_PKTS_IN = 0,
112 GTPH_CTR_PKTS_OUT,
113 GTPH_CTR_BYTES_IN,
114 GTPH_CTR_BYTES_OUT
115};
116
117static const struct rate_ctr_desc gtphub_counters_io_desc[] = {
118 { "packets.in", "Packets ( In)" },
119 { "packets.out", "Packets (Out)" },
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100120 { "bytes.in", "Bytes ( In)" },
121 { "bytes.out", "Bytes (Out)" },
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100122};
123
124static const struct rate_ctr_group_desc gtphub_ctrg_io_desc = {
125 .group_name_prefix = "gtphub.bind",
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100126 .group_description = "I/O Statistics",
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100127 .num_ctr = ARRAY_SIZE(gtphub_counters_io_desc),
128 .ctr_desc = gtphub_counters_io_desc,
129 .class_id = OSMO_STATS_CLASS_GLOBAL,
130};
131
132
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200133void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
134{
135 memcpy(gsna, src, sizeof(struct gsn_addr));
136}
137
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200138int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
139 const struct osmo_sockaddr *sa)
140{
141 char addr_str[256];
142 char port_str[6];
143
144 if (osmo_sockaddr_to_strs(addr_str, sizeof(addr_str),
145 port_str, sizeof(port_str),
146 sa, (NI_NUMERICHOST | NI_NUMERICSERV))
147 != 0) {
148 return -1;
149 }
150
151 if (port)
152 *port = atoi(port_str);
153
154 return gsn_addr_from_str(gsna, addr_str);
155}
156
157int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
158{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100159 if ((!gsna) || (!numeric_addr_str))
160 return -1;
161
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200162 int af = AF_INET;
163 gsna->len = 4;
164 const char *pos = numeric_addr_str;
165 for (; *pos; pos++) {
166 if (*pos == ':') {
167 af = AF_INET6;
168 gsna->len = 16;
169 break;
170 }
171 }
172
173 int rc = inet_pton(af, numeric_addr_str, gsna->buf);
174 if (rc != 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100175 LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n",
176 numeric_addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200177 return -1;
178 }
179 return 0;
180}
181
182const char *gsn_addr_to_str(const struct gsn_addr *gsna)
183{
184 static char buf[INET6_ADDRSTRLEN + 1];
185 return gsn_addr_to_strb(gsna, buf, sizeof(buf));
186}
187
188const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
189 char *strbuf,
190 int strbuf_len)
191{
192 int af;
193 switch (gsna->len) {
194 case 4:
195 af = AF_INET;
196 break;
197 case 16:
198 af = AF_INET6;
199 break;
200 default:
201 return NULL;
202 }
203
204 const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
205 if (!r) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100206 LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:"
207 " %s: len=%d, buf=%s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100208 strerror(errno),
209 (int)gsna->len,
210 osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200211 }
212 return r;
213}
214
215int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
216{
217 if (a == b)
218 return 1;
219 if ((!a) || (!b))
220 return 0;
221 if (a->len != b->len)
222 return 0;
223 return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
224}
225
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100226static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p,
227 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200228{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100229 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200230 return -1;
231
232 unsigned int len;
233 /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
234 if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
235 &len, gsna->buf, sizeof(gsna->buf))
236 != 0)
237 return -1;
238 gsna->len = len;
239 return 0;
240}
241
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100242static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p,
243 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200244{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100245 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200246 return -1;
247
248 int ie_idx;
249 ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx);
250
251 if (ie_idx < 0)
252 return -1;
253
254 struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
255 int ie_l = ntoh16(ie->l);
256 if (ie_l != gsna->len) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100257 LOG(LOGL_ERROR, "Not implemented:"
258 " replace an IE address of different size:"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200259 " replace %d with %d\n", (int)ie_l, (int)gsna->len);
260 return -1;
261 }
262
263 memcpy(ie->v, gsna->buf, (int)ie_l);
264 return 0;
265}
266
267/* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there.
268 */
269void validate_gtp0_header(struct gtp_packet_desc *p)
270{
271 const struct gtp0_header *pheader = &(p->data->gtp0.h);
272 p->rc = GTP_RC_UNKNOWN;
273 p->header_len = 0;
274
275 OSMO_ASSERT(p->data_len >= 1);
276 OSMO_ASSERT(p->version == 0);
277
278 if (p->data_len < GTP0_HEADER_SIZE) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100279 LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200280 p->rc = GTP_RC_TOOSHORT;
281 return;
282 }
283
284 p->type = ntoh8(pheader->type);
285 p->seq = ntoh16(pheader->seq);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100286 p->header_tei_rx = 0; /* TODO */
287 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200288
289 if (p->data_len == GTP0_HEADER_SIZE) {
290 p->rc = GTP_RC_TINY;
291 p->header_len = GTP0_HEADER_SIZE;
292 return;
293 }
294
295 /* Check packet length field versus length of packet */
296 if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100297 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
298 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100299 GTP0_HEADER_SIZE, (int)ntoh16(pheader->length),
300 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200301 p->rc = GTP_RC_TOOSHORT;
302 return;
303 }
304
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100305 LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200306 p->header_len = GTP0_HEADER_SIZE;
Neels Hofmeyre921e322015-11-11 00:45:50 +0100307 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200308}
309
310/* Validate GTP version 1 data, and update p->rc with the result, as well as
311 * p->header_len in case of a valid header. */
312void validate_gtp1_header(struct gtp_packet_desc *p)
313{
314 const struct gtp1_header_long *pheader = &(p->data->gtp1l.h);
315 p->rc = GTP_RC_UNKNOWN;
316 p->header_len = 0;
317
318 OSMO_ASSERT(p->data_len >= 1);
319 OSMO_ASSERT(p->version == 1);
320
321 if ((p->data_len < GTP1_HEADER_SIZE_LONG)
322 && (p->data_len != GTP1_HEADER_SIZE_SHORT)){
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100323 LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200324 p->rc = GTP_RC_TOOSHORT;
325 return;
326 }
327
328 p->type = ntoh8(pheader->type);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100329 p->header_tei_rx = ntoh32(pheader->tei);
330 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200331 p->seq = ntoh16(pheader->seq);
332
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100333 LOG(LOGL_DEBUG, "GTPv1\n"
334 "| type = %" PRIu8 " 0x%02" PRIx8 "\n"
335 "| length = %" PRIu16 " 0x%04" PRIx16 "\n"
336 "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n"
337 "| seq = %" PRIu16 " 0x%04" PRIx16 "\n"
338 "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n"
339 "| next = %" PRIu8 " 0x%02" PRIx8 "\n",
340 p->type, p->type,
341 ntoh16(pheader->length), ntoh16(pheader->length),
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100342 p->header_tei_rx, p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100343 p->seq, p->seq,
344 pheader->npdu, pheader->npdu,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200345 pheader->next, pheader->next);
346
347 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
348 p->rc = GTP_RC_TINY;
349 p->header_len = GTP1_HEADER_SIZE_SHORT;
350 return;
351 }
352
353 /* Check packet length field versus length of packet */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100354 int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT;
355 if (p->data_len != announced_len) {
356 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
357 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100358 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
359 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200360 p->rc = GTP_RC_TOOSHORT;
361 return;
362 }
363
Neels Hofmeyre921e322015-11-11 00:45:50 +0100364 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200365 p->header_len = GTP1_HEADER_SIZE_LONG;
366}
367
368/* Examine whether p->data of size p->data_len has a valid GTP header. Set
369 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
370 * gtp_rc). p->data must point at a buffer with p->data_len set. */
371void validate_gtp_header(struct gtp_packet_desc *p)
372{
373 p->rc = GTP_RC_UNKNOWN;
374
375 /* Need at least 1 byte in order to check version */
376 if (p->data_len < 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100377 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n",
378 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200379 p->rc = GTP_RC_TOOSHORT;
380 return;
381 }
382
383 p->version = p->data->flags >> 5;
384
385 switch (p->version) {
386 case 0:
387 validate_gtp0_header(p);
388 break;
389 case 1:
390 validate_gtp1_header(p);
391 break;
392 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100393 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200394 p->rc = GTP_RC_UNSUPPORTED_VERSION;
395 break;
396 }
397}
398
399
400/* Return the value of the i'th IMSI IEI by copying to *imsi.
401 * The first IEI is reached by passing i = 0.
402 * imsi must point at allocated space of (at least) 8 bytes.
403 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100404static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200405{
406 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
407}
408
409/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100410static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200411{
412 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
413}
414
415static char imsi_digit_to_char(uint8_t nibble)
416{
417 nibble &= 0x0f;
418 if (nibble > 9)
419 return (nibble == 0x0f) ? '\0' : '?';
420 return '0' + nibble;
421}
422
423/* Return a human readable IMSI string, in a static buffer.
424 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100425static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200426{
427 static char str[17];
428 int i;
429
430 for (i = 0; i < 8; i++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +0100431 char c;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100432 c = imsi_digit_to_char(imsi[i]);
433 if (c == '?')
434 return -1;
435 str[2*i] = c;
436
437 c = imsi_digit_to_char(imsi[i] >> 4);
438 if (c == '?')
439 return -1;
440 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200441 }
442 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100443 *imsi_str = str;
444 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200445}
446
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100447/* Return 0 if not present, 1 if present and decoded successfully, -1 if
448 * present but cannot be decoded. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100449static int get_ie_imsi_str(union gtpie_member *ie[], int i,
450 const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100451{
452 uint8_t imsi_buf[8];
453 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100454 return 0;
455 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100456}
457
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100458/* Return 0 if not present, 1 if present and decoded successfully, -1 if
459 * present but cannot be decoded. */
460static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100461{
462 static char apn_buf[GSM_APN_LENGTH];
463 unsigned int len;
464 if (gtpie_gettlv(ie, GTPIE_APN, 0,
465 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100466 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100467
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100468 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100469 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
470 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100471 return -1;
472 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100473
474 if (len > (sizeof(apn_buf) - 1))
475 len = sizeof(apn_buf) - 1;
476 apn_buf[len] = '\0';
477
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100478 *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
479 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100480 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
481 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100482 return -1;
483 }
484 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100485}
486
487
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200488/* Validate header, and index information elements. Write decoded packet
489 * information to *res. res->data will point at the given data buffer. On
490 * error, p->rc is set <= 0 (see enum gtp_rc). */
491static void gtp_decode(const uint8_t *data, int data_len,
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100492 unsigned int from_side_idx,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200493 unsigned int from_plane_idx,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100494 struct gtp_packet_desc *res,
495 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200496{
497 ZERO_STRUCT(res);
498 res->data = (union gtp_packet*)data;
499 res->data_len = data_len;
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100500 res->side_idx = from_side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200501 res->plane_idx = from_plane_idx;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100502 res->timestamp = now;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200503
504 validate_gtp_header(res);
505
506 if (res->rc <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100507 LOG(LOGL_ERROR, "INVALID: dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200508 return;
509 }
510
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100511 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200512
Neels Hofmeyre921e322015-11-11 00:45:50 +0100513 if (from_plane_idx == GTPH_PLANE_USER) {
514 res->rc = GTP_RC_PDU_U;
515 return;
516 }
517
518 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100519 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200520 return;
521 }
522
523 if (gtpie_decaps(res->ie, res->version,
524 (void*)(data + res->header_len),
525 res->data_len - res->header_len) != 0) {
526 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100527 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
528 " Dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200529 return;
530 }
531
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100532#if 1
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100533 /* TODO if (<loglevel is debug>) { ...
534 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200535 int i;
536
537 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100538 const char *imsi;
539 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200540 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100541 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200542 }
543
544 for (i = 0; i < 10; i++) {
545 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100546 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200547 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100548 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200549 }
550
551 for (i = 0; i < 2; i++) {
552 struct gsn_addr addr;
553 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100554 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200555 }
556
557 for (i = 0; i < 10; i++) {
558 uint32_t tei;
559 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
560 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100561 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200562 tei, tei);
563 }
564
565 for (i = 0; i < 10; i++) {
566 uint32_t tei;
567 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
568 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100569 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200570 tei, tei);
571 }
572#endif
573}
574
575
576/* expiry */
577
578void expiry_init(struct expiry *exq, int expiry_in_seconds)
579{
580 ZERO_STRUCT(exq);
581 exq->expiry_in_seconds = expiry_in_seconds;
582 INIT_LLIST_HEAD(&exq->items);
583}
584
585void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
586{
587 item->expiry = now + exq->expiry_in_seconds;
588
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +0100589 OSMO_ASSERT(llist_empty(&exq->items)
590 || (item->expiry
591 >= llist_last(&exq->items, struct expiring_item, entry)->expiry));
592
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200593 /* Add/move to the tail to always sort by expiry, ascending. */
594 llist_del(&item->entry);
595 llist_add_tail(&item->entry, &exq->items);
596}
597
598int expiry_tick(struct expiry *exq, time_t now)
599{
600 int expired = 0;
601 struct expiring_item *m, *n;
602 llist_for_each_entry_safe(m, n, &exq->items, entry) {
603 if (m->expiry <= now) {
604 expiring_item_del(m);
605 expired ++;
606 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200607 /* The items are added sorted by expiry. So when we hit
608 * an unexpired entry, only more unexpired ones will
609 * follow. */
610 break;
611 }
612 }
613 return expired;
614}
615
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100616void expiry_clear(struct expiry *exq)
617{
618 struct expiring_item *m, *n;
619 llist_for_each_entry_safe(m, n, &exq->items, entry) {
620 expiring_item_del(m);
621 }
622}
623
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200624void expiring_item_init(struct expiring_item *item)
625{
626 ZERO_STRUCT(item);
627 INIT_LLIST_HEAD(&item->entry);
628}
629
630void expiring_item_del(struct expiring_item *item)
631{
632 OSMO_ASSERT(item);
633 llist_del(&item->entry);
634 INIT_LLIST_HEAD(&item->entry);
635 if (item->del_cb) {
636 /* avoid loops */
637 del_cb_t del_cb = item->del_cb;
638 item->del_cb = 0;
639 (del_cb)(item);
640 }
641}
642
643
644/* nr_map, nr_pool */
645
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100646void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200647{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100648 *pool = (struct nr_pool){
649 .nr_min = nr_min,
650 .nr_max = nr_max,
651 .last_nr = nr_max
652 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200653}
654
655nr_t nr_pool_next(struct nr_pool *pool)
656{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100657 if (pool->last_nr >= pool->nr_max)
658 pool->last_nr = pool->nr_min;
659 else
660 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200661
662 return pool->last_nr;
663}
664
665void nr_map_init(struct nr_map *map, struct nr_pool *pool,
666 struct expiry *exq)
667{
668 ZERO_STRUCT(map);
669 map->pool = pool;
670 map->add_items_to_expiry = exq;
671 INIT_LLIST_HEAD(&map->mappings);
672}
673
674void nr_mapping_init(struct nr_mapping *m)
675{
676 ZERO_STRUCT(m);
677 INIT_LLIST_HEAD(&m->entry);
678 expiring_item_init(&m->expiry_entry);
679}
680
681void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
682{
683 /* Generate a mapped number */
684 mapping->repl = nr_pool_next(map->pool);
685
686 /* Add to the tail to always yield a list sorted by expiry, in
687 * ascending order. */
688 llist_add_tail(&mapping->entry, &map->mappings);
Neels Hofmeyr508514c2015-11-24 13:30:38 +0100689 nr_map_refresh(map, mapping, now);
690}
691
692void nr_map_refresh(struct nr_map *map, struct nr_mapping *mapping, time_t now)
693{
694 if (!map->add_items_to_expiry)
695 return;
696 expiry_add(map->add_items_to_expiry,
697 &mapping->expiry_entry,
698 now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200699}
700
701void nr_map_clear(struct nr_map *map)
702{
703 struct nr_mapping *m;
704 struct nr_mapping *n;
705 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
706 nr_mapping_del(m);
707 }
708}
709
710int nr_map_empty(const struct nr_map *map)
711{
712 return llist_empty(&map->mappings);
713}
714
715struct nr_mapping *nr_map_get(const struct nr_map *map,
716 void *origin, nr_t nr_orig)
717{
718 struct nr_mapping *mapping;
719 llist_for_each_entry(mapping, &map->mappings, entry) {
720 if ((mapping->origin == origin)
721 && (mapping->orig == nr_orig))
722 return mapping;
723 }
724 /* Not found. */
725 return NULL;
726}
727
728struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
729{
730 struct nr_mapping *mapping;
731 llist_for_each_entry(mapping, &map->mappings, entry) {
732 if (mapping->repl == nr_repl) {
733 return mapping;
734 }
735 }
736 /* Not found. */
737 return NULL;
738}
739
740void nr_mapping_del(struct nr_mapping *mapping)
741{
742 OSMO_ASSERT(mapping);
743 llist_del(&mapping->entry);
744 INIT_LLIST_HEAD(&mapping->entry);
745 expiring_item_del(&mapping->expiry_entry);
746}
747
748
749/* gtphub */
750
751const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
752 "CTRL",
753 "USER",
754};
755
756const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
757 2123,
758 2152,
759};
760
Neels Hofmeyra9905a52015-11-29 23:49:48 +0100761const char* const gtphub_side_idx_names[GTPH_SIDE_N] = {
762 "SGSN",
763 "GGSN",
764};
765
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200766time_t gtphub_now(void)
767{
768 struct timespec now_tp;
769 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
770 return now_tp.tv_sec;
771}
772
773/* Remove a gtphub_peer from its list and free it. */
774static void gtphub_peer_del(struct gtphub_peer *peer)
775{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100776 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200777 nr_map_clear(&peer->seq_map);
778 llist_del(&peer->entry);
779 talloc_free(peer);
780}
781
782static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
783{
784 OSMO_ASSERT(llist_empty(&pa->ports));
785 llist_del(&pa->entry);
786 talloc_free(pa);
787}
788
789static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
790{
791 OSMO_ASSERT(pp->ref_count == 0);
792 llist_del(&pp->entry);
793 talloc_free(pp);
794}
795
796/* From the information in the gtp_packet_desc, return the address of a GGSN.
797 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100798static int gtphub_resolve_ggsn(struct gtphub *hub,
799 struct gtp_packet_desc *p,
800 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200801
802/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100803struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
804 const char *imsi_str,
805 const char *apn_ni_str);
806int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200807
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200808static void gtphub_zero(struct gtphub *hub)
809{
810 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100811 INIT_LLIST_HEAD(&hub->ggsn_lookups);
812 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200813}
814
815static int gtphub_sock_init(struct osmo_fd *ofd,
816 const struct gtphub_cfg_addr *addr,
817 osmo_fd_cb_t cb,
818 void *data,
819 int ofd_id)
820{
821 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100822 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200823 return -1;
824 }
825 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100826 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200827 return -1;
828 }
829
830 ofd->when = BSC_FD_READ;
831 ofd->cb = cb;
832 ofd->data = data;
833 ofd->priv_nr = ofd_id;
834
835 int rc;
836 rc = osmo_sock_init_ofd(ofd,
837 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
838 addr->addr_str, addr->port,
839 OSMO_SOCK_F_BIND);
840 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100841 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
842 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200843 return -1;
844 }
845
846 return 0;
847}
848
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100849static void gtphub_sock_close(struct osmo_fd *ofd)
850{
851 close(ofd->fd);
852 osmo_fd_unregister(ofd);
853 ofd->cb = NULL;
854}
855
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200856static void gtphub_bind_init(struct gtphub_bind *b)
857{
858 ZERO_STRUCT(b);
859
860 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100861
862 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
863 &gtphub_ctrg_io_desc, 0);
864 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200865}
866
867static int gtphub_bind_start(struct gtphub_bind *b,
868 const struct gtphub_cfg_bind *cfg,
869 osmo_fd_cb_t cb, void *cb_data,
870 unsigned int ofd_id)
871{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100872 LOG(LOGL_DEBUG, "Starting bind %s\n", b->label);
873 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0) {
874 LOG(LOGL_FATAL, "Invalid bind address for %s: %s\n",
875 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200876 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100877 }
878 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0) {
879 LOG(LOGL_FATAL, "Cannot bind for %s: %s\n",
880 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200881 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100882 }
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100883 b->local_port = cfg->bind.port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200884 return 0;
885}
886
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100887static void gtphub_bind_free(struct gtphub_bind *b)
888{
889 OSMO_ASSERT(llist_empty(&b->peers));
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100890 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100891}
892
893static void gtphub_bind_stop(struct gtphub_bind *b) {
894 gtphub_sock_close(&b->ofd);
895 gtphub_bind_free(b);
896}
897
Neels Hofmeyr40348972015-11-17 12:22:28 +0100898/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200899 * Return the number of bytes read, zero on error. */
900static int gtphub_read(const struct osmo_fd *from,
901 struct osmo_sockaddr *from_addr,
902 uint8_t *buf, size_t buf_len)
903{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100904 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200905
Neels Hofmeyr40348972015-11-17 12:22:28 +0100906 /* recvfrom requires the available length set in *from_addr_len. */
907 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200908 errno = 0;
909 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100910 (struct sockaddr*)&from_addr->a,
911 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200912 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
913 * is not truncated. Then maybe reduce buf's size. */
914
915 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100916 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
917 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200918 return 0;
919 }
920
Neels Hofmeyr40348972015-11-17 12:22:28 +0100921 LOG(LOGL_DEBUG, "Received %d bytes from %s\n%s\n",
922 (int)received, osmo_sockaddr_to_str(from_addr),
923 osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200924
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200925 return received;
926}
927
928inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
929{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100930 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200931 OSMO_ASSERT(pp->ref_count < UINT_MAX);
932 pp->ref_count++;
933}
934
935inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
936{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100937 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200938 OSMO_ASSERT(pp->ref_count > 0);
939 pp->ref_count--;
940}
941
942inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
943{
944 OSMO_ASSERT(p->version == 1);
945 p->data->gtp1l.h.seq = hton16(seq);
946 p->seq = seq;
947}
948
949inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
950{
951 OSMO_ASSERT(p->version == 1);
952 p->data->gtp1l.h.tei = hton32(tei);
953 p->header_tei = tei;
954}
955
956static void gtphub_mapping_del_cb(struct expiring_item *expi);
957
958static struct nr_mapping *gtphub_mapping_new()
959{
960 struct nr_mapping *nrm;
961 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
962 OSMO_ASSERT(nrm);
963
964 nr_mapping_init(nrm);
965 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
966 return nrm;
967}
968
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100969
970#define APPEND(args...) \
971 l = snprintf(pos, left, args); \
972 pos += l; \
973 left -= l
974
975static const char *gtphub_tunnel_side_str(struct gtphub_tunnel *tun,
976 int side_idx)
977{
978 static char buf[256];
979 char *pos = buf;
980 int left = sizeof(buf);
981 int l;
982
983 struct gtphub_tunnel_endpoint *c, *u;
984 c = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
985 u = &tun->endpoint[side_idx][GTPH_PLANE_USER];
986
987 /* print both only if they differ. */
988 if (!c->peer) {
989 APPEND("(uninitialized)");
990 } else {
991 APPEND("%s", gsn_addr_to_str(&c->peer->peer_addr->addr));
992 }
993
994 if (!u->peer) {
995 if (c->peer) {
996 APPEND(" / (uninitialized)");
997 }
998 } else if ((!c->peer)
999 || (!gsn_addr_same(&u->peer->peer_addr->addr,
1000 &c->peer->peer_addr->addr))) {
1001 APPEND(" / %s", gsn_addr_to_str(&u->peer->peer_addr->addr));
1002 }
1003
1004 APPEND(" (TEI C %x=%x / U %x=%x)",
1005 c->tei_orig, c->tei_repl,
1006 u->tei_orig, u->tei_repl);
1007 return buf;
1008}
1009
1010const char *gtphub_tunnel_str(struct gtphub_tunnel *tun)
1011{
1012 static char buf[512];
1013 char *pos = buf;
1014 int left = sizeof(buf);
1015 int l;
1016
1017 APPEND("%s", gtphub_tunnel_side_str(tun, GTPH_SIDE_SGSN));
1018 APPEND(" <-> %s", gtphub_tunnel_side_str(tun, GTPH_SIDE_GGSN));
1019
1020 return buf;
1021}
1022
1023#undef APPEND
1024
1025void gtphub_tunnel_endpoint_set_peer(struct gtphub_tunnel_endpoint *te,
1026 struct gtphub_peer_port *pp)
1027{
1028 if (te->peer)
1029 gtphub_port_ref_count_dec(te->peer);
1030 te->peer = pp;
1031 if (te->peer)
1032 gtphub_port_ref_count_inc(te->peer);
1033}
1034
1035int gtphub_tunnel_complete(struct gtphub_tunnel *tun)
1036{
1037 if (!tun)
1038 return 0;
1039 int side_idx;
1040 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001041 for_each_side_and_plane(side_idx, plane_idx) {
1042 struct gtphub_tunnel_endpoint *te =
1043 &tun->endpoint[side_idx][plane_idx];
1044 if (!(te->peer && te->tei_orig && te->tei_repl))
1045 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001046 }
1047 return 1;
1048}
1049
1050static void gtphub_tunnel_del_cb(struct expiring_item *expi)
1051{
1052 struct gtphub_tunnel *tun = container_of(expi,
1053 struct gtphub_tunnel,
1054 expiry_entry);
1055 LOG(LOGL_DEBUG, "expired: %s\n", gtphub_tunnel_str(tun));
1056
1057 llist_del(&tun->entry);
1058 INIT_LLIST_HEAD(&tun->entry); /* mark unused */
1059
1060 expi->del_cb = 0; /* avoid recursion loops */
1061 expiring_item_del(&tun->expiry_entry); /* usually already done, but make sure. */
1062
1063 int side_idx;
1064 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001065 for_each_side_and_plane(side_idx, plane_idx) {
1066 /* clear ref count */
1067 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1068 NULL);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001069 }
1070
1071 talloc_free(tun);
1072}
1073
1074static struct gtphub_tunnel *gtphub_tunnel_new()
1075{
1076 struct gtphub_tunnel *tun;
1077 tun = talloc_zero(osmo_gtphub_ctx, struct gtphub_tunnel);
1078 OSMO_ASSERT(tun);
1079
1080 INIT_LLIST_HEAD(&tun->entry);
1081 expiring_item_init(&tun->expiry_entry);
1082
1083 tun->expiry_entry.del_cb = gtphub_tunnel_del_cb;
1084 return tun;
1085}
1086
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001087static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
1088 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001089{
1090 if (llist_empty(&peer->addresses))
1091 return "(addressless)";
1092
1093 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
1094 struct gtphub_peer_addr,
1095 entry);
1096 return gsn_addr_to_strb(&a->addr, buf, buflen);
1097}
1098
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001099static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
1100 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001101{
1102 if (!port)
1103 return "(null port)";
1104
1105 snprintf(buf, buflen, "%s port %d",
1106 gsn_addr_to_str(&port->peer_addr->addr),
1107 (int)port->port);
1108 return buf;
1109}
1110
1111const char *gtphub_peer_str(struct gtphub_peer *peer)
1112{
1113 static char buf[256];
1114 return gtphub_peer_strb(peer, buf, sizeof(buf));
1115}
1116
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001117const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001118{
1119 static char buf[256];
1120 return gtphub_port_strb(port, buf, sizeof(buf));
1121}
1122
1123static const char *gtphub_port_str2(struct gtphub_peer_port *port)
1124{
1125 static char buf[256];
1126 return gtphub_port_strb(port, buf, sizeof(buf));
1127}
1128
1129static void gtphub_mapping_del_cb(struct expiring_item *expi)
1130{
1131 expi->del_cb = 0; /* avoid recursion loops */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001132 expiring_item_del(expi); /* usually already done, but make sure. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001133
1134 struct nr_mapping *nrm = container_of(expi,
1135 struct nr_mapping,
1136 expiry_entry);
1137 llist_del(&nrm->entry);
1138 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
1139
1140 /* Just for log */
1141 struct gtphub_peer_port *from = nrm->origin;
1142 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001143 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001144 (int)nrm->expiry_entry.expiry,
1145 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001146 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001147
1148 gtphub_port_ref_count_dec(from);
1149
1150 talloc_free(nrm);
1151}
1152
1153static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
1154 struct gtphub_peer_port *from,
1155 nr_t orig_nr,
1156 time_t now)
1157{
1158 struct nr_mapping *nrm;
1159
1160 nrm = nr_map_get(map, from, orig_nr);
1161
1162 if (!nrm) {
1163 nrm = gtphub_mapping_new();
1164 nrm->orig = orig_nr;
1165 nrm->origin = from;
1166 nr_map_add(map, nrm, now);
1167 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001168 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001169 gtphub_port_str(from),
1170 (int)(nrm->orig), (int)(nrm->repl));
1171 } else {
Neels Hofmeyr508514c2015-11-24 13:30:38 +01001172 nr_map_refresh(map, nrm, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001173 }
1174
1175 OSMO_ASSERT(nrm);
1176 return nrm;
1177}
1178
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001179static void gtphub_map_seq(struct gtp_packet_desc *p,
1180 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001181 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001182{
1183 /* Store a mapping in to_peer's map, so when we later receive a GTP
1184 * packet back from to_peer, the seq nr can be unmapped back to its
1185 * origin (from_peer here). */
1186 struct nr_mapping *nrm;
1187 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001188 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001189
1190 /* Change the GTP packet to yield the new, mapped seq nr */
1191 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001192}
1193
1194static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1195 struct gtphub_peer_port *responding_port)
1196{
1197 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001198 struct nr_mapping *nrm =
1199 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1200 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001201 if (!nrm)
1202 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001203 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1204 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001205 set_seq(p, nrm->orig);
1206 return nrm->origin;
1207}
1208
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001209static struct gtphub_tunnel *gtphub_tun_find(struct gtphub *hub,
1210 int side_idx,
1211 int plane_idx,
1212 struct gtphub_peer_port *from,
1213 uint32_t tei_orig,
1214 uint32_t tei_repl)
1215{
1216 OSMO_ASSERT(from);
1217
1218 struct gtphub_tunnel *tun;
1219 /* TODO: optimize: don't iterate *all* tunnels. */
1220 llist_for_each_entry(tun, &hub->tunnels, entry) {
1221 struct gtphub_tunnel_endpoint *te =
1222 &tun->endpoint[side_idx][plane_idx];
1223 if (((!tei_orig) || (te->tei_orig == tei_orig))
1224 && ((!tei_repl) || (te->tei_repl == tei_repl))
1225 && gsn_addr_same(&te->peer->peer_addr->addr, &from->peer_addr->addr))
1226 return tun;
1227 }
1228 return NULL;
1229}
1230
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001231static int gtphub_check_mapped_tei(struct gtphub_tunnel_endpoint *new_te,
1232 struct gtphub_tunnel_endpoint *iterated_te,
1233 uint32_t *tei_min,
1234 uint32_t *tei_max)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001235{
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001236 if (new_te->tei_repl != iterated_te->tei_repl)
1237 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001238
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001239 /* new_te->tei_repl is already taken. Try to find one out of the known
1240 * range. */
1241
1242 if ((*tei_max) < 0xffffffff) {
1243 (*tei_max)++;
1244 new_te->tei_repl = *tei_max;
1245 return 1;
1246 } else if ((*tei_min) > 1) {
1247 (*tei_min)--;
1248 new_te->tei_repl = *tei_min;
1249 return 1;
1250 }
1251
1252 /* None seems to be available. */
1253 return 0;
1254}
1255
1256static int gtphub_check_reused_teis(struct gtphub *hub,
1257 struct gtphub_tunnel *new_tun)
1258{
1259 uint32_t tei_min = 0xffffffff;
1260 uint32_t tei_max = 0;
1261 int side_idx;
1262 int plane_idx;
1263 int side_idx2;
1264 int plane_idx2;
1265 struct gtphub_tunnel_endpoint *te;
1266 struct gtphub_tunnel_endpoint *te2;
1267
1268 struct gtphub_tunnel *tun, *ntun;
1269
1270 llist_for_each_entry_safe(tun, ntun, &hub->tunnels, entry) {
1271 if (tun == new_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001272 continue;
1273
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001274 /* Check whether the GSN sent a TEI that it is reusing from a
1275 * previous tunnel. */
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001276 int tun_continue = 0;
1277 for_each_side(side_idx) {
1278 for_each_plane(plane_idx) {
1279 te = &tun->endpoint[side_idx][plane_idx];
1280 te2 = &new_tun->endpoint[side_idx][plane_idx];
1281 if ((te->tei_orig != te2->tei_orig)
1282 || (!te->peer)
1283 || (!te2->peer)
1284 || !gsn_addr_same(&te->peer->peer_addr->addr,
1285 &te2->peer->peer_addr->addr))
1286 continue;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001287
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001288 /* The peer is reusing a TEI that I believe to
1289 * be part of another tunnel. The other tunnel
1290 * must be stale, then. */
1291 LOG(LOGL_NOTICE,
1292 "Expiring tunnel due to reused TEI:"
1293 " peer %s sent %s TEI %x,"
1294 " previously used by tunnel %s...\n",
1295 gtphub_port_str(te->peer),
1296 gtphub_plane_idx_names[plane_idx],
1297 te->tei_orig,
1298 gtphub_tunnel_str(tun));
1299 LOG(LOGL_NOTICE, "...while establishing tunnel %s\n",
1300 gtphub_tunnel_str(new_tun));
Neels Hofmeyr52c0bd32015-12-02 14:14:32 +01001301
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001302 expiring_item_del(&tun->expiry_entry);
1303 /* continue to find more matches. There shouldn't be
1304 * any, but let's make sure. However, tun is deleted,
1305 * so we need to skip to the next tunnel. */
1306 tun_continue = 1;
1307 break;
1308 }
1309 if (tun_continue)
1310 break;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001311 }
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001312 if (tun_continue)
1313 continue;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001314
1315 /* Check whether the mapped TEIs assigned to the endpoints are
1316 * used anywhere else. */
1317 for_each_side_and_plane(side_idx, plane_idx) {
1318 te = &tun->endpoint[side_idx][plane_idx];
1319 tei_min = (tei_min < te->tei_repl)? tei_min : te->tei_repl;
1320 tei_max = (tei_max > te->tei_repl)? tei_max : te->tei_repl;
1321
1322 for_each_side_and_plane(side_idx2, plane_idx2) {
1323 te2 = &new_tun->endpoint[side_idx2][plane_idx2];
1324 if (!gtphub_check_mapped_tei(te2, te, &tei_min, &tei_max)) {
1325 LOG(LOGL_ERROR,
1326 "No mapped TEI is readily available."
1327 " Searching for holes between occupied"
1328 " TEIs not implemented.");
1329 return 0;
1330 }
1331 }
1332 }
1333
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001334 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001335
1336 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001337}
1338
1339static void gtphub_tunnel_refresh(struct gtphub *hub,
1340 struct gtphub_tunnel *tun,
1341 time_t now)
1342{
1343 expiry_add(&hub->expire_slowly,
1344 &tun->expiry_entry,
1345 now);
1346}
1347
1348static struct gtphub_tunnel_endpoint *gtphub_unmap_tei(struct gtphub *hub,
1349 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001350 struct gtphub_peer_port *from,
1351 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001352{
1353 OSMO_ASSERT(from);
1354 int other_side = other_side_idx(p->side_idx);
1355
1356 struct gtphub_tunnel *tun;
1357 llist_for_each_entry(tun, &hub->tunnels, entry) {
1358 struct gtphub_tunnel_endpoint *te_from =
1359 &tun->endpoint[p->side_idx][p->plane_idx];
1360 struct gtphub_tunnel_endpoint *te_to =
1361 &tun->endpoint[other_side][p->plane_idx];
1362 if ((te_to->tei_repl == p->header_tei_rx)
Neels Hofmeyrfc1be3a2015-12-02 00:31:31 +01001363 && te_from->peer
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001364 && gsn_addr_same(&te_from->peer->peer_addr->addr,
1365 &from->peer_addr->addr)) {
1366 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001367 if (unmapped_from_tun)
1368 *unmapped_from_tun = tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001369 return te_to;
1370 }
1371 }
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001372
1373 if (unmapped_from_tun)
1374 *unmapped_from_tun = NULL;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001375 return NULL;
1376}
1377
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001378static void gtphub_map_restart_counter(struct gtphub *hub,
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001379 struct gtp_packet_desc *p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001380{
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001381 if (p->rc != GTP_RC_PDU_C)
1382 return;
1383
1384 int ie_idx;
1385 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1386 if (ie_idx < 0)
1387 return;
1388
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001389 /* Always send gtphub's own restart counter */
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001390 p->ie[ie_idx]->tv1.v = hton8(hub->restart_counter);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001391}
1392
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001393static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001394 struct gtphub_tunnel **unmapped_from_tun,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001395 struct gtphub *hub,
1396 struct gtp_packet_desc *p,
1397 struct gtphub_peer_port *from_port)
1398{
1399 OSMO_ASSERT(p->version == 1);
1400 *to_port_p = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001401 if (unmapped_from_tun)
1402 *unmapped_from_tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001403
1404 /* If the header's TEI is zero, no PDP context has been established
1405 * yet. If nonzero, a mapping should actually already exist for this
1406 * TEI, since it must have been announced in a PDP context creation. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001407 if (!p->header_tei_rx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001408 return 0;
1409
1410 /* to_peer has previously announced a TEI, which was stored and
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001411 * mapped in a tunnel struct. */
1412 struct gtphub_tunnel_endpoint *to;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001413 to = gtphub_unmap_tei(hub, p, from_port, unmapped_from_tun);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001414 if (!to) {
1415 LOG(LOGL_ERROR, "Received unknown TEI %" PRIx32 " from %s\n",
1416 p->header_tei_rx, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001417 return -1;
1418 }
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001419 OSMO_ASSERT(*unmapped_from_tun);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001420
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001421 uint32_t unmapped_tei = to->tei_orig;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001422 set_tei(p, unmapped_tei);
1423
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001424 LOG(LOGL_DEBUG, "Unmapped TEI coming from: %s\n",
1425 gtphub_tunnel_str(*unmapped_from_tun));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001426
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001427 /* May be NULL for an invalidated tunnel. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001428 *to_port_p = to->peer;
1429
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001430 return 0;
1431}
1432
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001433static int gtphub_handle_create_pdp_ctx(struct gtphub *hub,
1434 struct gtp_packet_desc *p,
1435 struct gtphub_peer_port *from_ctrl,
1436 struct gtphub_peer_port *to_ctrl)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001437{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001438 int plane_idx;
1439
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001440 /* TODO enforce a Request only from SGSN, a Response only from GGSN? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001441 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1442 plane_nrs_match_GSN_addr_IE_indices);
1443
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001444 struct gtphub_tunnel *tun = NULL;
1445
1446 if (p->type == GTP_CREATE_PDP_REQ) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001447 if (p->side_idx != GTPH_SIDE_SGSN) {
1448 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
1449 " Request from the GGSN side");
1450 return -1;
1451 }
1452
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001453 /* A new tunnel. */
1454 tun = gtphub_tunnel_new();
1455 llist_add(&tun->entry, &hub->tunnels);
1456 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001457 /* The endpoint peers on this side (SGSN) will be set from IEs
1458 * below. Also set the GGSN Ctrl endpoint, for logging. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001459 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001460 to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001461 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001462 if (p->side_idx != GTPH_SIDE_GGSN) {
1463 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
1464 " Response from the SGSN side");
1465 return -1;
1466 }
1467
1468 /* Find the tunnel created during request. This is coming from
1469 * the GGSN side, so to_ctrl corresponds to the SGSN. */
1470 OSMO_ASSERT(to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001471 tun = gtphub_tun_find(hub, other_side_idx(p->side_idx),
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001472 p->plane_idx, to_ctrl, 0, p->header_tei_rx);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001473
1474 if (!tun) {
1475 LOG(LOGL_ERROR, "Create PDP Context Response: cannot"
1476 " find matching request from SGSN %s, mapped TEI %x.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001477 gtphub_port_str(to_ctrl), p->header_tei_rx);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001478 return -1;
1479 }
1480 }
1481
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001482 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1483 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001484 unsigned int side_idx = p->side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001485
1486 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01001487 int rc;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001488 struct gsn_addr addr_from_ie;
1489 uint32_t tei_from_ie;
1490 int ie_idx;
1491
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001492 /* Fetch GSN Address and TEI from IEs. As ensured by above
1493 * static asserts, plane_idx corresponds to the GSN Address IE
1494 * index (the first one = 0 = ctrl, second one = 1 = user). */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001495 rc = gsn_addr_get(&addr_from_ie, p, plane_idx);
1496 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001497 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1498 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001499 return -1;
1500 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001501 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001502 gtphub_plane_idx_names[plane_idx],
1503 gsn_addr_to_str(&addr_from_ie),
1504 addr_from_ie.len);
1505
1506 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1507 if (ie_idx < 0) {
1508 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001509 LOG(LOGL_ERROR,
1510 "Create PDP Context message invalid:"
1511 " missing IE %d\n",
1512 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001513 return -1;
1514 }
1515 tei_from_ie = 0;
1516 }
1517 else
1518 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1519
1520 /* Make sure an entry for this peer address with default port
1521 * exists */
1522 struct gtphub_peer_port *peer_from_ie =
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001523 gtphub_port_have(hub, &hub->to_gsns[side_idx][plane_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001524 &addr_from_ie,
1525 gtphub_plane_idx_default_port[plane_idx]);
1526
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001527 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1528 peer_from_ie);
1529
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001530 if (tei_from_ie) {
1531 /* Create TEI mapping and replace in GTP packet IE */
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01001532 uint32_t mapped_tei = nr_pool_next(&hub->tei_pool);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001533
1534 tun->endpoint[side_idx][plane_idx].tei_orig = tei_from_ie;
1535 tun->endpoint[side_idx][plane_idx].tei_repl = mapped_tei;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001536 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001537
Neels Hofmeyrcd865d62015-11-30 12:58:48 +01001538 if (!gtphub_check_reused_teis(hub, tun)) {
1539 /* It's highly unlikely that all TEIs are
1540 * taken. But the code looking for an unused
1541 * TEI is, at the time of writing this comment,
1542 * not able to find gaps in the TEI space. To
1543 * explicitly alert the user of this problem,
1544 * rather abort than carry on. */
1545 LOG(LOGL_FATAL, "TEI range exhausted. Cannot create TEI mapping, aborting.\n");
1546 abort();
1547 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001548 }
1549
1550 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001551 rc = gsn_addr_put(&hub->to_gsns[other_side_idx(side_idx)][plane_idx].local_addr,
1552 p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001553 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001554 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1555 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001556 return -1;
1557 }
1558 }
1559
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001560 if (p->type == GTP_CREATE_PDP_REQ) {
1561 LOG(LOGL_DEBUG, "New tunnel, first half: %s\n",
1562 gtphub_tunnel_str(tun));
Neels Hofmeyr005f1752015-11-30 12:19:11 +01001563 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001564 LOG(LOGL_DEBUG, "New tunnel: %s\n",
1565 gtphub_tunnel_str(tun));
1566 }
1567
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001568 return 0;
1569}
1570
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001571static void pending_delete_del_cb(struct expiring_item *expi)
1572{
1573 struct pending_delete *pd;
1574 pd = container_of(expi, struct pending_delete, expiry_entry);
1575
1576 llist_del(&pd->entry);
1577 INIT_LLIST_HEAD(&pd->entry);
1578
1579 pd->expiry_entry.del_cb = 0;
1580 expiring_item_del(&pd->expiry_entry);
1581
1582 talloc_free(pd);
1583}
1584
1585static struct pending_delete *pending_delete_new(void)
1586{
1587 struct pending_delete *pd = talloc_zero(osmo_gtphub_ctx, struct pending_delete);
1588 INIT_LLIST_HEAD(&pd->entry);
1589 expiring_item_init(&pd->expiry_entry);
1590 pd->expiry_entry.del_cb = pending_delete_del_cb;
1591 return pd;
1592}
1593
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001594static int gtphub_handle_delete_pdp_ctx(struct gtphub *hub,
1595 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001596 struct gtphub_tunnel *known_tun,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001597 struct gtphub_peer_port *from_ctrl,
1598 struct gtphub_peer_port *to_ctrl)
1599{
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001600 if (p->type == GTP_DELETE_PDP_REQ) {
1601 if (!known_tun) {
1602 LOG(LOGL_ERROR, "Cannot find tunnel for Delete PDP Context Request.\n");
1603 return -1;
1604 }
1605
1606 /* Store the Delete Request until a successful Response is seen. */
1607 uint8_t teardown_ind;
1608 uint8_t nsapi;
1609
1610 if (gtpie_gettv1(p->ie, GTPIE_TEARDOWN, 0, &teardown_ind) != 0) {
1611 LOG(LOGL_ERROR, "Missing Teardown Ind IE in Delete PDP Context Request.\n");
1612 return -1;
1613 }
1614
1615 if (gtpie_gettv1(p->ie, GTPIE_NSAPI, 0, &nsapi) != 0) {
1616 LOG(LOGL_ERROR, "Missing NSAPI IE in Delete PDP Context Request.\n");
1617 return -1;
1618 }
1619
1620 struct pending_delete *pd = NULL;
1621
1622 struct pending_delete *pdi = NULL;
1623 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1624 if ((pdi->tun == known_tun)
1625 && (pdi->teardown_ind == teardown_ind)
1626 && (pdi->nsapi == nsapi)) {
1627 pd = pdi;
1628 break;
1629 }
1630 }
1631
1632 if (!pd) {
1633 pd = pending_delete_new();
1634 pd->tun = known_tun;
1635 pd->teardown_ind = teardown_ind;
1636 pd->nsapi = nsapi;
1637
1638 LOG(LOGL_DEBUG, "Tunnel delete pending: %s\n",
1639 gtphub_tunnel_str(known_tun));
1640 llist_add(&pd->entry, &hub->pending_deletes);
1641 }
1642
1643 /* Add or refresh timeout. */
1644 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1645
1646 /* If a pending_delete should expire before the response to
1647 * indicate success comes in, the responding peer will have the
1648 * tunnel deactivated, while the requesting peer gets no reply
1649 * and keeps the tunnel. The hope is that the requesting peer
1650 * will try again and get a useful response. */
1651 } else if (p->type == GTP_DELETE_PDP_RSP) {
1652 /* Find the Delete Request for this Response. */
1653 struct pending_delete *pd = NULL;
1654
1655 struct pending_delete *pdi;
1656 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1657 if (known_tun == pdi->tun) {
1658 pd = pdi;
1659 break;
1660 }
1661 }
1662
1663 if (!pd) {
1664 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1665 " Cannot find matching request.");
1666 /* If we delete the tunnel now, anyone can send a
1667 * Delete response to kill tunnels at will. */
1668 return -1;
1669 }
1670
1671 /* TODO handle teardown_ind and nsapi */
1672
1673 expiring_item_del(&pd->expiry_entry);
1674
1675 uint8_t cause;
1676 if (gtpie_gettv1(p->ie, GTPIE_CAUSE, 0, &cause) != 0) {
1677 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1678 " Missing Cause IE.");
1679 /* If we delete the tunnel now, at least one of the
1680 * peers may still think it is active. */
1681 return -1;
1682 }
1683
1684 if (cause != GTPCAUSE_ACC_REQ) {
1685 LOG(LOGL_NOTICE,
1686 "Delete PDP Context Response indicates failure;"
1687 "for %s\n",
1688 gtphub_tunnel_str(known_tun));
1689 return -1;
1690 }
1691
1692 LOG(LOGL_DEBUG, "Delete PDP Context: removing tunnel %s\n",
1693 gtphub_tunnel_str(known_tun));
1694 expiring_item_del(&known_tun->expiry_entry);
1695 }
1696
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001697 return 0;
1698}
1699
1700static int gtphub_handle_update_pdp_ctx(struct gtphub *hub,
1701 struct gtp_packet_desc *p,
1702 struct gtphub_peer_port *from_ctrl,
1703 struct gtphub_peer_port *to_ctrl)
1704{
1705 /* TODO */
1706 return 0;
1707}
1708
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001709/* Read GSN address IEs from p, and make sure these peer addresses exist in
1710 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1711 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1712 * the packet p. */
1713static int gtphub_handle_pdp_ctx(struct gtphub *hub,
1714 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001715 struct gtphub_tunnel *known_tun,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001716 struct gtphub_peer_port *from_ctrl,
1717 struct gtphub_peer_port *to_ctrl)
1718{
1719 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1720
1721 switch (p->type) {
1722 case GTP_CREATE_PDP_REQ:
1723 case GTP_CREATE_PDP_RSP:
1724 return gtphub_handle_create_pdp_ctx(hub, p,
1725 from_ctrl, to_ctrl);
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001726
1727 case GTP_DELETE_PDP_REQ:
1728 case GTP_DELETE_PDP_RSP:
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001729 return gtphub_handle_delete_pdp_ctx(hub, p, known_tun,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001730 from_ctrl, to_ctrl);
1731
1732 case GTP_UPDATE_PDP_REQ:
1733 case GTP_UPDATE_PDP_RSP:
1734 return gtphub_handle_update_pdp_ctx(hub, p,
1735 from_ctrl, to_ctrl);
1736
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001737 default:
1738 /* Nothing to do for this message type. */
1739 return 0;
1740 }
1741
1742}
1743
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001744static int gtphub_write(const struct osmo_fd *to,
1745 const struct osmo_sockaddr *to_addr,
1746 const uint8_t *buf, size_t buf_len)
1747{
1748 errno = 0;
1749 ssize_t sent = sendto(to->fd, buf, buf_len, 0,
1750 (struct sockaddr*)&to_addr->a, to_addr->l);
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01001751 LOG(LOGL_DEBUG, "to %s\n", osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001752
1753 if (sent == -1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001754 LOG(LOGL_ERROR, "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001755 return -EINVAL;
1756 }
1757
1758 if (sent != buf_len)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001759 LOG(LOGL_ERROR, "sent(%d) != data_len(%d)\n",
1760 (int)sent, (int)buf_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001761 else
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001762 LOG(LOGL_DEBUG, "Sent %d\n%s\n",
1763 (int)sent, osmo_hexdump(buf, sent));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001764
1765 return 0;
1766}
1767
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001768static int gtphub_send_del_pdp_ctx(struct gtphub *hub,
1769 struct gtphub_tunnel *tun,
1770 int to_side)
1771{
1772 static uint8_t del_ctx_msg[16] = {
1773 0x32, /* GTP v1 flags */
1774 GTP_DELETE_PDP_REQ,
1775 0x00, 0x08, /* Length in network byte order */
1776 0x00, 0x00, 0x00, 0x00, /* TEI to be replaced */
1777 0, 0, /* Seq, to be replaced */
1778 0, 0, /* no extensions */
1779 0x13, 0xff, /* 19: Teardown ind = 1 */
1780 0x14, 0 /* 20: NSAPI = 0 */
1781 };
1782
1783 uint32_t *tei = (uint32_t*)&del_ctx_msg[4];
1784 uint16_t *seq = (uint16_t*)&del_ctx_msg[8];
1785
1786 struct gtphub_tunnel_endpoint *te =
1787 &tun->endpoint[to_side][GTPH_PLANE_CTRL];
1788
1789 if (! te->peer)
1790 return 0;
1791
1792 *tei = hton32(te->tei_orig);
1793 *seq = hton16(nr_pool_next(&te->peer->peer_addr->peer->seq_pool));
1794
1795 struct gtphub_bind *to_bind = &hub->to_gsns[to_side][GTPH_PLANE_CTRL];
1796 int rc = gtphub_write(&to_bind->ofd, &te->peer->sa,
1797 del_ctx_msg, sizeof(del_ctx_msg));
1798 if (rc != 0) {
1799 LOG(LOGL_ERROR,
1800 "Failed to send out-of-band Delete PDP Context Request to %s\n",
1801 gtphub_port_str(te->peer));
1802 }
1803 return rc;
1804}
1805
1806/* Tell all peers on the other end of tunnels that PDP contexts are void. */
1807static void gtphub_restarted(struct gtphub *hub,
1808 struct gtp_packet_desc *p,
1809 struct gtphub_peer_port *pp)
1810{
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001811 LOG(LOGL_DEBUG, "Peer has restarted: %s\n",
1812 gtphub_port_str(pp));
1813
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001814 struct gtphub_tunnel *tun;
1815 llist_for_each_entry(tun, &hub->tunnels, entry) {
1816 int side_idx;
1817 for_each_side(side_idx) {
1818 if (pp != tun->endpoint[side_idx][GTPH_PLANE_CTRL].peer)
1819 continue;
1820
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001821 LOG(LOGL_DEBUG, "Deleting tunnel due to peer restart: %s\n",
1822 gtphub_tunnel_str(tun));
1823
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001824 /* Send a Delete PDP Context Request to the
1825 * peer on the other side, remember the pending
1826 * delete and wait for the response to delete
1827 * the tunnel. Clear this side of the tunnel to
1828 * make sure it isn't used.
1829 *
1830 * Should the delete message send fail, or if no
1831 * response is received, this tunnel will expire. If
1832 * its TEIs come up in a new PDP Context Request, it
1833 * will be removed. If messages for this tunnel should
1834 * come in (from the not restarted side), they will be
1835 * dropped because the tunnel is rendered unusable. */
1836 gtphub_send_del_pdp_ctx(hub, tun, other_side_idx(side_idx));
1837
1838 struct pending_delete *pd;
1839 pd = pending_delete_new();
1840 pd->tun = tun;
1841 pd->teardown_ind = 0xff;
1842 pd->nsapi = 0;
1843 llist_add(&pd->entry, &hub->pending_deletes);
1844 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1845
1846 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_CTRL],
1847 NULL);
1848 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_USER],
1849 NULL);
1850 }
1851 }
1852}
1853
1854static int get_restart_count(struct gtp_packet_desc *p)
1855{
1856 int ie_idx;
1857 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1858 if (ie_idx < 0)
1859 return -1;
1860 return ntoh8(p->ie[ie_idx]->tv1.v);
1861}
1862
1863static void gtphub_check_restart_counter(struct gtphub *hub,
1864 struct gtp_packet_desc *p,
1865 struct gtphub_peer_port *from)
1866{
1867 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1868 * that doesn't match the peer's previously sent restart counter, clear
1869 * that peer and cancel PDP contexts. */
1870
1871 int restart = get_restart_count(p);
1872
1873 if ((restart < 0) || (restart > 255))
1874 return;
1875
1876 if ((from->last_restart_count >= 0) && (from->last_restart_count <= 255)) {
1877 if (from->last_restart_count != restart) {
1878 gtphub_restarted(hub, p, from);
1879 }
1880 }
1881
1882 from->last_restart_count = restart;
1883}
1884
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001885static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1886{
1887 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1888 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
1889 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1890 gtphub_plane_idx_names[plane_idx]);
1891
1892 if (!(what & BSC_FD_READ))
1893 return 0;
1894
1895 struct gtphub *hub = from_sgsns_ofd->data;
1896
1897 static uint8_t buf[4096];
1898 struct osmo_sockaddr from_addr;
1899 struct osmo_sockaddr to_addr;
1900 struct osmo_fd *to_ofd;
1901 int len;
1902 uint8_t *reply_buf;
1903
1904 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1905 if (len < 1)
1906 return 0;
1907
1908 len = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, &from_addr,
1909 buf, len, gtphub_now(),
1910 &reply_buf, &to_ofd, &to_addr);
1911 if (len < 1)
1912 return 0;
1913
1914 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
1915}
1916
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001917static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1918{
1919 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1920 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001921 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1922 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001923 if (!(what & BSC_FD_READ))
1924 return 0;
1925
1926 struct gtphub *hub = from_ggsns_ofd->data;
1927
1928 static uint8_t buf[4096];
1929 struct osmo_sockaddr from_addr;
1930 struct osmo_sockaddr to_addr;
1931 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001932 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001933 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001934
1935 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1936 if (len < 1)
1937 return 0;
1938
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001939 len = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, &from_addr,
1940 buf, len, gtphub_now(),
1941 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001942 if (len < 1)
1943 return 0;
1944
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001945 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001946}
1947
1948static int gtphub_unmap(struct gtphub *hub,
1949 struct gtp_packet_desc *p,
1950 struct gtphub_peer_port *from,
1951 struct gtphub_peer_port *to_proxy,
1952 struct gtphub_peer_port **final_unmapped,
1953 struct gtphub_peer_port **unmapped_from_seq,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001954 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001955{
1956 /* Always (try to) unmap sequence and TEI numbers, which need to be
1957 * replaced in the packet. Either way, give precedence to the proxy, if
1958 * configured. */
1959
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001960 if (unmapped_from_seq)
1961 *unmapped_from_seq = NULL;
1962 if (unmapped_from_tun)
1963 *unmapped_from_tun = NULL;
1964 if (final_unmapped)
1965 *final_unmapped = NULL;
1966
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001967 struct gtphub_peer_port *from_seq = NULL;
1968 struct gtphub_peer_port *from_tei = NULL;
1969 struct gtphub_peer_port *unmapped = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001970 struct gtphub_tunnel *tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001971
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001972 from_seq = gtphub_unmap_seq(p, from);
1973
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001974 if (gtphub_unmap_header_tei(&from_tei, &tun, hub, p, from) < 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001975 return -1;
1976
1977 struct gtphub_peer *from_peer = from->peer_addr->peer;
1978 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001979 LOG(LOGL_DEBUG,
1980 "Seq unmap and TEI unmap yield two different peers."
1981 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001982 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1983 gtphub_plane_idx_names[p->plane_idx],
1984 gtphub_peer_str(from_peer),
1985 (int)p->seq,
1986 gtphub_port_str(from_seq),
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001987 (unsigned int)p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001988 gtphub_port_str2(from_tei)
1989 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001990 }
1991 unmapped = (from_seq? from_seq : from_tei);
1992
1993 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001994 LOG(LOGL_NOTICE,
1995 "Unmap yields a different peer than the configured proxy."
1996 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001997 " unmapped: %s proxy: %s\n",
1998 gtphub_port_str(unmapped),
1999 gtphub_port_str2(to_proxy)
2000 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002001 }
2002 unmapped = (to_proxy? to_proxy : unmapped);
2003
2004 if (!unmapped) {
2005 /* Return no error, but returned pointers are all NULL. */
2006 return 0;
2007 }
2008
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002009 if (unmapped_from_seq)
2010 *unmapped_from_seq = from_seq;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002011 if (unmapped_from_tun)
2012 *unmapped_from_tun = tun;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002013 if (final_unmapped)
2014 *final_unmapped = unmapped;
2015 return 0;
2016}
2017
2018static int gsn_addr_to_sockaddr(struct gsn_addr *src,
2019 uint16_t port,
2020 struct osmo_sockaddr *dst)
2021{
2022 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
2023}
2024
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002025/* If p is an Echo request, replace p's data with the matching response and
2026 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
2027 * detected. */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002028static int gtphub_handle_echo_req(struct gtphub *hub, struct gtp_packet_desc *p,
2029 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002030{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002031 if (p->type != GTP_ECHO_REQ)
2032 return 0;
2033
2034 static uint8_t echo_response_data[14] = {
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002035 0x32, /* GTP v1 flags */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002036 GTP_ECHO_RSP,
2037 0x00, 14 - 8, /* Length in network byte order */
2038 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
2039 0, 0, /* Seq, to be replaced */
2040 0, 0, /* no extensions */
2041 0x0e, /* Recovery IE */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002042 0 /* Restart counter, to be replaced */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002043 };
2044 uint16_t *seq = (uint16_t*)&echo_response_data[8];
2045 uint8_t *recovery = &echo_response_data[13];
2046
2047 *seq = hton16(p->seq);
2048 *recovery = hub->restart_counter;
2049
2050 *reply_buf = echo_response_data;
2051
2052 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002053}
2054
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002055struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2056 const struct osmo_sockaddr *addr);
2057
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002058/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
2059 * address to forward to. Return a pointer to the osmo_fd, but copy the
2060 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
2061 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
2062 * bytes to forward, 0 or less on failure. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002063int gtphub_handle_buf(struct gtphub *hub,
2064 unsigned int side_idx,
2065 unsigned int plane_idx,
2066 const struct osmo_sockaddr *from_addr,
2067 uint8_t *buf,
2068 size_t received,
2069 time_t now,
2070 uint8_t **reply_buf,
2071 struct osmo_fd **to_ofd,
2072 struct osmo_sockaddr *to_addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002073{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002074 /*
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002075 struct gtphub_bind *from_bind_arr = hub->to_ggsns;
2076 struct gtphub_bind *to_bind_arr = hub->to_sgsns;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002077 */
2078 struct gtphub_bind *from_bind = &hub->to_gsns[side_idx][plane_idx];
2079 struct gtphub_bind *to_bind = &hub->to_gsns[other_side_idx(side_idx)][plane_idx];
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002080
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002081 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
2082 received);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002083 LOG(LOGL_DEBUG, "%s rx %s from %s %s\n",
2084 (side_idx == GTPH_SIDE_GGSN)? "<-" : "->",
Neels Hofmeyre921e322015-11-11 00:45:50 +01002085 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002086 gtphub_side_idx_names[side_idx],
Neels Hofmeyre921e322015-11-11 00:45:50 +01002087 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002088
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002089 static struct gtp_packet_desc p;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002090 gtp_decode(buf, received, side_idx, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002091
2092 if (p.rc <= 0)
2093 return -1;
2094
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002095 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2096
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002097 int reply_len;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002098 reply_len = gtphub_handle_echo_req(hub, &p, reply_buf);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002099 if (reply_len > 0) {
2100 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002101 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002102 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002103
2104 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2105 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2106 reply_len);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002107 LOG(LOGL_DEBUG, "%s Echo response to %s: %d bytes to %s\n",
2108 (side_idx == GTPH_SIDE_GGSN)? "-->" : "<--",
2109 gtphub_side_idx_names[side_idx],
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002110 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002111 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002112 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002113 if (reply_len < 0)
2114 return -1;
2115
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002116 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002117
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002118 /* If a proxy is configured, check that it's indeed that proxy talking
2119 * to us. A proxy is a forced 1:1 connection, e.g. to another gtphub,
2120 * so no-one else is allowed to talk to us from that side. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002121 struct gtphub_peer_port *from_peer = hub->proxy[side_idx][plane_idx];
2122 if (from_peer) {
2123 if (osmo_sockaddr_cmp(&from_peer->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002124 LOG(LOGL_ERROR,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002125 "Rejecting: %s proxy configured, but GTP packet"
2126 " received on %s bind is from another sender:"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002127 " proxy: %s sender: %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002128 gtphub_side_idx_names[side_idx],
2129 gtphub_side_idx_names[side_idx],
2130 gtphub_port_str(from_peer),
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002131 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002132 return -1;
2133 }
2134 }
2135
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002136 if (!from_peer) {
2137 /* Find or create a peer with a matching address. The sender's
2138 * port may in fact differ. */
2139 from_peer = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002140 }
2141
2142 /* If any PDP context has been created, we already have an entry for
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002143 * this GSN. If we don't have an entry, a GGSN has nothing to tell us
2144 * about, while an SGSN may initiate a PDP context. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002145 if (!from_peer) {
2146 if (side_idx == GTPH_SIDE_GGSN) {
2147 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
2148 osmo_sockaddr_to_str(from_addr));
2149 return -1;
2150 } else {
2151 /* SGSN */
2152 /* A new peer. If this is on the Ctrl plane, an SGSN
2153 * may make first contact without being known yet, so
2154 * create the peer struct for the current sender. */
2155 if (plane_idx != GTPH_PLANE_CTRL) {
2156 LOG(LOGL_ERROR,
2157 "Dropping packet: User plane peer was not"
2158 "announced by PDP Context: %s\n",
2159 osmo_sockaddr_to_str(from_addr));
2160 return -1;
2161 }
2162
2163 struct gsn_addr from_gsna;
2164 uint16_t from_port;
2165 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
2166 return -1;
2167
2168 from_peer = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
2169 }
2170 }
2171
2172 if (!from_peer) {
2173 /* This could theoretically happen for invalid address data or
2174 * somesuch. */
2175 LOG(LOGL_ERROR, "Dropping packet: invalid %s peer: %s\n",
2176 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002177 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002178 return -1;
2179 }
2180
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002181 LOG(LOGL_DEBUG, "from %s peer: %s\n", gtphub_side_idx_names[side_idx],
2182 gtphub_port_str(from_peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002183
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002184 struct gtphub_peer_port *to_peer_from_seq;
2185 struct gtphub_peer_port *to_peer;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002186 struct gtphub_tunnel *tun;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002187 if (gtphub_unmap(hub, &p, from_peer,
2188 hub->proxy[other_side_idx(side_idx)][plane_idx],
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002189 &to_peer, &to_peer_from_seq, &tun)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002190 != 0) {
2191 return -1;
2192 }
2193
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002194 if ((!to_peer) && (side_idx == GTPH_SIDE_SGSN)) {
2195 if (gtphub_resolve_ggsn(hub, &p, &to_peer) < 0)
2196 return -1;
2197 }
2198
2199 if (!to_peer) {
2200 LOG(LOGL_ERROR, "No %s to send to. Dropping packet.\n",
2201 gtphub_side_idx_names[other_side_idx(side_idx)]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002202 return -1;
2203 }
2204
2205 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002206 /* This may be a Create PDP Context response. If it is, there
2207 * are other addresses in the GTP message to set up apart from
2208 * the sender. */
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002209 if (gtphub_handle_pdp_ctx(hub, &p, tun, from_peer, to_peer)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002210 != 0)
2211 return -1;
2212 }
2213
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002214 gtphub_check_restart_counter(hub, &p, from_peer);
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002215 gtphub_map_restart_counter(hub, &p);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002216
2217 /* If the GGSN is replying to an SGSN request, the sequence nr has
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002218 * already been unmapped above (to_peer_from_seq != NULL), and we need not
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002219 * create a new mapping. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002220 if (!to_peer_from_seq)
2221 gtphub_map_seq(&p, from_peer, to_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002222
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002223 osmo_sockaddr_copy(to_addr, &to_peer->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002224
2225 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01002226
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002227 if (received) {
2228 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2229 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2230 received);
2231 }
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002232 LOG(LOGL_DEBUG, "%s Forward to %s: %d bytes to %s\n",
2233 (side_idx == GTPH_SIDE_SGSN)? "-->" : "<--",
2234 gtphub_side_idx_names[other_side_idx(side_idx)],
Neels Hofmeyre921e322015-11-11 00:45:50 +01002235 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002236 return received;
2237}
2238
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002239static void resolved_gssn_del_cb(struct expiring_item *expi)
2240{
2241 struct gtphub_resolved_ggsn *ggsn;
2242 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
2243
2244 gtphub_port_ref_count_dec(ggsn->peer);
2245 llist_del(&ggsn->entry);
2246
2247 ggsn->expiry_entry.del_cb = 0;
2248 expiring_item_del(&ggsn->expiry_entry);
2249
2250 talloc_free(ggsn);
2251}
2252
2253void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
2254 struct gsn_addr *resolved_addr,
2255 time_t now)
2256{
2257 struct gtphub_peer_port *pp;
2258 struct gtphub_resolved_ggsn *ggsn;
2259
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002260 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002261 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
2262 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01002263
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002264 pp = gtphub_port_have(hub, &hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002265 resolved_addr, 2123);
2266 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002267 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
2268 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002269 return;
2270 }
2271
2272 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
2273 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01002274 INIT_LLIST_HEAD(&ggsn->entry);
2275 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002276
2277 ggsn->peer = pp;
2278 gtphub_port_ref_count_inc(pp);
2279
2280 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01002281 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002282
2283 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002284 expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002285
2286 llist_add(&ggsn->entry, &hub->resolved_ggsns);
2287}
2288
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002289static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
2290{
2291 return pp->ref_count == 0;
2292}
2293
2294static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
2295{
2296 struct gtphub_peer_port *pp, *npp;
2297 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
2298 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002299 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002300 gtphub_port_str(pp));
2301 gtphub_peer_port_del(pp);
2302 }
2303 }
2304 return llist_empty(&pa->ports);
2305}
2306
2307static int gtphub_gc_peer(struct gtphub_peer *p)
2308{
2309 struct gtphub_peer_addr *pa, *npa;
2310 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
2311 if (gtphub_gc_peer_addr(pa)) {
2312 gtphub_peer_addr_del(pa);
2313 }
2314 }
2315
2316 /* Note that there's a ref_count in each gtphub_peer_port instance
2317 * listed within p->addresses, referenced by TEI mappings from
2318 * hub->tei_map. As long as those don't expire, this peer will stay. */
2319
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002320 return llist_empty(&p->addresses)
2321 && nr_map_empty(&p->seq_map);
2322}
2323
2324static void gtphub_gc_bind(struct gtphub_bind *b)
2325{
2326 struct gtphub_peer *p, *n;
2327 llist_for_each_entry_safe(p, n, &b->peers, entry) {
2328 if (gtphub_gc_peer(p)) {
2329 gtphub_peer_del(p);
2330 }
2331 }
2332}
2333
2334void gtphub_gc(struct gtphub *hub, time_t now)
2335{
2336 int expired;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002337 expired = expiry_tick(&hub->expire_quickly, now);
2338 expired += expiry_tick(&hub->expire_slowly, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002339
2340 /* ... */
2341
2342 if (expired) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002343 int s, p;
2344 for_each_side_and_plane(s, p) {
2345 gtphub_gc_bind(&hub->to_gsns[s][p]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002346 }
2347 }
2348}
2349
2350static void gtphub_gc_cb(void *data)
2351{
2352 struct gtphub *hub = data;
2353 gtphub_gc(hub, gtphub_now());
2354 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2355}
2356
2357static void gtphub_gc_start(struct gtphub *hub)
2358{
2359 hub->gc_timer.cb = gtphub_gc_cb;
2360 hub->gc_timer.data = hub;
2361
2362 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2363}
2364
2365/* called by unit tests */
2366void gtphub_init(struct gtphub *hub)
2367{
2368 gtphub_zero(hub);
2369
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002370 INIT_LLIST_HEAD(&hub->tunnels);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002371 INIT_LLIST_HEAD(&hub->pending_deletes);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002372
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002373 expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS);
2374 expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002375
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01002376 nr_pool_init(&hub->tei_pool, 1, 0xffffffff);
2377
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002378 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002379 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002380 for_each_side_and_plane(side_idx, plane_idx) {
2381 gtphub_bind_init(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002382 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002383
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002384 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].label = "SGSN Ctrl";
2385 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].label = "GGSN Ctrl";
2386 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].label = "SGSN User";
2387 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002388}
2389
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002390/* For the test suite, this is kept separate from gtphub_stop(), which also
2391 * closes sockets. The test suite avoids using sockets and would cause
2392 * segfaults when trying to close uninitialized ofds. */
2393void gtphub_free(struct gtphub *hub)
2394{
2395 /* By expiring all mappings, a garbage collection should free
2396 * everything else. A gtphub_bind_free() will assert that everything is
2397 * indeed empty. */
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002398 expiry_clear(&hub->expire_quickly);
2399 expiry_clear(&hub->expire_slowly);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002400
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002401 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002402 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002403 for_each_side_and_plane(side_idx, plane_idx) {
2404 gtphub_gc_bind(&hub->to_gsns[side_idx][plane_idx]);
2405 gtphub_bind_free(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002406 }
2407}
2408
2409void gtphub_stop(struct gtphub *hub)
2410{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002411 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002412 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002413 for_each_side_and_plane(side_idx, plane_idx) {
2414 gtphub_bind_stop(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002415 }
2416 gtphub_free(hub);
2417}
2418
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002419static int gtphub_make_proxy(struct gtphub *hub,
2420 struct gtphub_peer_port **pp,
2421 struct gtphub_bind *bind,
2422 const struct gtphub_cfg_addr *addr)
2423{
2424 if (!addr->addr_str)
2425 return 0;
2426
2427 struct gsn_addr gsna;
2428 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
2429 return -1;
2430
2431 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
2432
2433 /* This is *the* proxy. Make sure it is never expired. */
2434 gtphub_port_ref_count_inc(*pp);
2435 return 0;
2436}
2437
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002438int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
2439 uint8_t restart_counter)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002440{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002441 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002442
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002443 hub->restart_counter = restart_counter;
2444
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002445 /* If a Ctrl plane proxy is configured, ares will never be used. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002446 if (!cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str) {
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002447 if (gtphub_ares_init(hub) != 0) {
2448 LOG(LOGL_FATAL, "Failed to initialize ares\n");
2449 return -1;
2450 }
2451 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002452
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002453 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002454 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002455 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01002456 int rc;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002457 rc = gtphub_bind_start(&hub->to_gsns[side_idx][plane_idx],
2458 &cfg->to_gsns[side_idx][plane_idx],
2459 (side_idx == GTPH_SIDE_SGSN)
2460 ? from_sgsns_read_cb
2461 : from_ggsns_read_cb,
2462 hub, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002463 if (rc) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002464 LOG(LOGL_FATAL, "Failed to bind for %ss (%s)\n",
2465 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002466 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002467 return rc;
2468 }
2469 }
2470
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002471 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002472 if (gtphub_make_proxy(hub,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002473 &hub->proxy[side_idx][plane_idx],
2474 &hub->to_gsns[side_idx][plane_idx],
2475 &cfg->proxy[side_idx][plane_idx])
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002476 != 0) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002477 LOG(LOGL_FATAL, "Cannot configure %s proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002478 " %s port %d.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002479 gtphub_side_idx_names[side_idx],
2480 cfg->proxy[side_idx][plane_idx].addr_str,
2481 (int)cfg->proxy[side_idx][plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002482 return -1;
2483 }
2484 }
2485
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002486 for_each_side_and_plane(side_idx, plane_idx) {
2487 if (hub->proxy[side_idx][plane_idx])
2488 LOG(LOGL_NOTICE, "Using %s %s proxy %s\n",
2489 gtphub_side_idx_names[side_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002490 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002491 gtphub_port_str(hub->proxy[side_idx][plane_idx]));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002492 }
2493
2494 gtphub_gc_start(hub);
2495 return 0;
2496}
2497
2498static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
2499 const struct gsn_addr *addr)
2500{
2501 struct gtphub_peer_addr *a;
2502 llist_for_each_entry(a, &peer->addresses, entry) {
2503 if (gsn_addr_same(&a->addr, addr))
2504 return a;
2505 }
2506 return NULL;
2507}
2508
2509static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2510 uint16_t port)
2511{
2512 OSMO_ASSERT(port);
2513 struct gtphub_peer_port *pp;
2514 llist_for_each_entry(pp, &a->ports, entry) {
2515 if (pp->port == port)
2516 return pp;
2517 }
2518 return NULL;
2519}
2520
2521static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2522 const struct gsn_addr *addr)
2523{
2524 struct gtphub_peer *peer;
2525 llist_for_each_entry(peer, &bind->peers, entry) {
2526 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2527 if (a)
2528 return a;
2529 }
2530 return NULL;
2531}
2532
2533static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2534 const struct gsn_addr *addr,
2535 uint16_t port)
2536{
2537 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2538 if (!a)
2539 return NULL;
2540 return gtphub_addr_find_port(a, port);
2541}
2542
2543struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2544 const struct osmo_sockaddr *addr)
2545{
2546 struct gsn_addr gsna;
2547 uint16_t port;
2548 gsn_addr_from_sockaddr(&gsna, &port, addr);
2549 return gtphub_port_find(bind, &gsna, port);
2550}
2551
2552static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2553 struct gtphub_bind *bind)
2554{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002555 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2556 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002557 OSMO_ASSERT(peer);
2558
2559 INIT_LLIST_HEAD(&peer->addresses);
2560
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002561 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002562 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002563
2564 /* TODO use something random to pick the initial sequence nr.
2565 0x6d31 produces the ASCII character sequence 'm1', currently used in
2566 gtphub_nc_test.sh. */
2567 peer->seq_pool.last_nr = 0x6d31 - 1;
2568
2569 llist_add(&peer->entry, &bind->peers);
2570 return peer;
2571}
2572
2573static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2574 const struct gsn_addr *addr)
2575{
2576 struct gtphub_peer_addr *a;
2577 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2578 OSMO_ASSERT(a);
2579 a->peer = peer;
2580 gsn_addr_copy(&a->addr, addr);
2581 INIT_LLIST_HEAD(&a->ports);
2582 llist_add(&a->entry, &peer->addresses);
2583
2584 return a;
2585}
2586
2587static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2588 struct gtphub_bind *bind,
2589 const struct gsn_addr *addr)
2590{
2591 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2592 if (a)
2593 return a;
2594
2595 /* If we haven't found an address, that means we need to create an
2596 * entirely new peer for the new address. More addresses may be added
2597 * to this peer later, but not via this function. */
2598 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002599
2600 a = gtphub_peer_add_addr(peer, addr);
2601
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002602 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002603 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002604 gsn_addr_to_str(&a->addr));
2605
2606 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002607}
2608
2609static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2610 uint16_t port)
2611{
2612 struct gtphub_peer_port *pp;
2613
2614 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2615 OSMO_ASSERT(pp);
2616 pp->peer_addr = a;
2617 pp->port = port;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002618 pp->last_restart_count = -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002619
2620 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2621 talloc_free(pp);
2622 return NULL;
2623 }
2624
2625 llist_add(&pp->entry, &a->ports);
2626
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002627 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002628 gsn_addr_to_str(&a->addr),
2629 (int)port);
2630
2631 return pp;
2632}
2633
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002634struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2635 struct gtphub_bind *bind,
2636 const struct gsn_addr *addr,
2637 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002638{
2639 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2640
2641 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2642 if (pp)
2643 return pp;
2644
2645 return gtphub_addr_add_port(a, port);
2646}
2647
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002648/* Find a GGSN peer with a matching address. If the address is known but the
2649 * port not, create a new port for that peer address. */
2650struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2651 const struct osmo_sockaddr *addr)
2652{
2653 struct gtphub_peer_addr *pa;
2654 struct gtphub_peer_port *pp;
2655
2656 struct gsn_addr gsna;
2657 uint16_t port;
2658 gsn_addr_from_sockaddr(&gsna, &port, addr);
2659
2660 pa = gtphub_addr_find(bind, &gsna);
2661 if (!pa)
2662 return NULL;
2663
2664 pp = gtphub_addr_find_port(pa, port);
2665
2666 if (!pp)
2667 pp = gtphub_addr_add_port(pa, port);
2668
2669 return pp;
2670}
2671
2672
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002673/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2674 * resolution should be possible but failed, and 1 if resolution was
2675 * successful. *pp will be set to NULL if <1 is returned. */
2676static int gtphub_resolve_ggsn(struct gtphub *hub,
2677 struct gtp_packet_desc *p,
2678 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002679{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002680 *pp = NULL;
2681
2682 /* TODO determine from message type whether IEs should be present? */
2683
2684 int rc;
2685 const char *imsi_str;
2686 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2687 if (rc < 1)
2688 return rc;
2689 OSMO_ASSERT(imsi_str);
2690
2691 const char *apn_str;
2692 rc = get_ie_apn_str(p->ie, &apn_str);
2693 if (rc < 1)
2694 return rc;
2695 OSMO_ASSERT(apn_str);
2696
2697 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2698 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002699}
2700
2701
2702/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002703/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002704/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2705 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002706static int _osmo_getaddrinfo(struct addrinfo **result,
2707 uint16_t family, uint16_t type, uint8_t proto,
2708 const char *host, uint16_t port)
2709{
2710 struct addrinfo hints;
2711 char portbuf[16];
2712
2713 sprintf(portbuf, "%u", port);
2714 memset(&hints, '\0', sizeof(struct addrinfo));
2715 hints.ai_family = family;
2716 if (type == SOCK_RAW) {
2717 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2718 * SOCK_RAW and IPPROTO_GRE is used.
2719 */
2720 hints.ai_socktype = SOCK_DGRAM;
2721 hints.ai_protocol = IPPROTO_UDP;
2722 } else {
2723 hints.ai_socktype = type;
2724 hints.ai_protocol = proto;
2725 }
2726
2727 return getaddrinfo(host, portbuf, &hints, result);
2728}
2729
2730/* TODO move to osmocom/core/socket.c ? */
2731int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2732 uint16_t family, uint16_t type, uint8_t proto,
2733 const char *host, uint16_t port)
2734{
2735 struct addrinfo *res;
2736 int rc;
2737 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2738
2739 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002740 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002741 return -EINVAL;
2742 }
2743
2744 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2745 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2746 addr->l = res->ai_addrlen;
2747 freeaddrinfo(res);
2748
2749 return 0;
2750}
2751
2752int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2753 char *port_str, size_t port_str_len,
2754 const struct osmo_sockaddr *addr,
2755 int flags)
2756{
2757 int rc;
2758
2759 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2760 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2761 return -1;
2762 }
2763
2764 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002765 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2766 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002767 return -1;
2768 }
2769
2770 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2771 addr_str, addr_str_len,
2772 port_str, port_str_len,
2773 flags);
2774
2775 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002776 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2777 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2778 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002779
2780 return rc;
2781}
2782
2783const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2784 char *buf, size_t buf_len)
2785{
2786 const int portbuf_len = 6;
2787 OSMO_ASSERT(buf_len > portbuf_len);
2788 char *portbuf = buf + buf_len - portbuf_len;
2789 buf_len -= portbuf_len;
2790 if (osmo_sockaddr_to_strs(buf, buf_len,
2791 portbuf, portbuf_len,
2792 addr,
2793 NI_NUMERICHOST | NI_NUMERICSERV))
2794 return NULL;
2795
2796 char *pos = buf + strnlen(buf, buf_len-1);
2797 size_t len = buf_len - (pos - buf);
2798
2799 snprintf(pos, len, " port %s", portbuf);
2800 buf[buf_len-1] = '\0';
2801
2802 return buf;
2803}
2804
2805const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2806{
2807 static char buf[256];
2808 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2809 if (! result)
2810 return "(invalid)";
2811 return result;
2812}
2813
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002814int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2815 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002816{
2817 if (a == b)
2818 return 0;
2819 if (!a)
2820 return -1;
2821 if (!b)
2822 return 1;
2823 if (a->l != b->l) {
2824 /* Lengths are not the same, but determine the order. Will
2825 * anyone ever sort a list by osmo_sockaddr though...? */
2826 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2827 if (cmp == 0) {
2828 if (a->l < b->l)
2829 return -1;
2830 else
2831 return 1;
2832 }
2833 return cmp;
2834 }
2835 return memcmp(&a->a, &b->a, a->l);
2836}
2837
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002838void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2839 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002840{
2841 OSMO_ASSERT(src->l <= sizeof(dst->a));
2842 memcpy(&dst->a, &src->a, src->l);
2843 dst->l = src->l;
2844}