blob: 96068bf5a16797ec811001322fd91413688d4787 [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{
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +0100135 *gsna = *src;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200136}
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
Neels Hofmeyr87c83d02015-12-03 11:25:27 +0100506 if (res->rc <= 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200507 return;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200508
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100509 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200510
Neels Hofmeyre921e322015-11-11 00:45:50 +0100511 if (from_plane_idx == GTPH_PLANE_USER) {
512 res->rc = GTP_RC_PDU_U;
513 return;
514 }
515
516 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100517 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200518 return;
519 }
520
521 if (gtpie_decaps(res->ie, res->version,
522 (void*)(data + res->header_len),
523 res->data_len - res->header_len) != 0) {
524 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100525 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
526 " Dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200527 return;
528 }
529
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100530#if 1
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100531 /* TODO if (<loglevel is debug>) { ...
532 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200533 int i;
534
535 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100536 const char *imsi;
537 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200538 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100539 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200540 }
541
542 for (i = 0; i < 10; i++) {
543 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100544 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200545 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100546 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200547 }
548
549 for (i = 0; i < 2; i++) {
550 struct gsn_addr addr;
551 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100552 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200553 }
554
555 for (i = 0; i < 10; i++) {
556 uint32_t tei;
557 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
558 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100559 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200560 tei, tei);
561 }
562
563 for (i = 0; i < 10; i++) {
564 uint32_t tei;
565 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
566 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100567 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200568 tei, tei);
569 }
570#endif
571}
572
573
574/* expiry */
575
576void expiry_init(struct expiry *exq, int expiry_in_seconds)
577{
578 ZERO_STRUCT(exq);
579 exq->expiry_in_seconds = expiry_in_seconds;
580 INIT_LLIST_HEAD(&exq->items);
581}
582
583void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
584{
585 item->expiry = now + exq->expiry_in_seconds;
586
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +0100587 OSMO_ASSERT(llist_empty(&exq->items)
588 || (item->expiry
589 >= llist_last(&exq->items, struct expiring_item, entry)->expiry));
590
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200591 /* Add/move to the tail to always sort by expiry, ascending. */
592 llist_del(&item->entry);
593 llist_add_tail(&item->entry, &exq->items);
594}
595
596int expiry_tick(struct expiry *exq, time_t now)
597{
598 int expired = 0;
599 struct expiring_item *m, *n;
600 llist_for_each_entry_safe(m, n, &exq->items, entry) {
601 if (m->expiry <= now) {
602 expiring_item_del(m);
603 expired ++;
604 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200605 /* The items are added sorted by expiry. So when we hit
606 * an unexpired entry, only more unexpired ones will
607 * follow. */
608 break;
609 }
610 }
611 return expired;
612}
613
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100614void expiry_clear(struct expiry *exq)
615{
616 struct expiring_item *m, *n;
617 llist_for_each_entry_safe(m, n, &exq->items, entry) {
618 expiring_item_del(m);
619 }
620}
621
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200622void expiring_item_init(struct expiring_item *item)
623{
624 ZERO_STRUCT(item);
625 INIT_LLIST_HEAD(&item->entry);
626}
627
628void expiring_item_del(struct expiring_item *item)
629{
630 OSMO_ASSERT(item);
631 llist_del(&item->entry);
632 INIT_LLIST_HEAD(&item->entry);
633 if (item->del_cb) {
634 /* avoid loops */
635 del_cb_t del_cb = item->del_cb;
636 item->del_cb = 0;
637 (del_cb)(item);
638 }
639}
640
641
642/* nr_map, nr_pool */
643
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100644void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200645{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100646 *pool = (struct nr_pool){
647 .nr_min = nr_min,
648 .nr_max = nr_max,
649 .last_nr = nr_max
650 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200651}
652
653nr_t nr_pool_next(struct nr_pool *pool)
654{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100655 if (pool->last_nr >= pool->nr_max)
656 pool->last_nr = pool->nr_min;
657 else
658 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200659
660 return pool->last_nr;
661}
662
663void nr_map_init(struct nr_map *map, struct nr_pool *pool,
664 struct expiry *exq)
665{
666 ZERO_STRUCT(map);
667 map->pool = pool;
668 map->add_items_to_expiry = exq;
669 INIT_LLIST_HEAD(&map->mappings);
670}
671
672void nr_mapping_init(struct nr_mapping *m)
673{
674 ZERO_STRUCT(m);
675 INIT_LLIST_HEAD(&m->entry);
676 expiring_item_init(&m->expiry_entry);
677}
678
679void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
680{
681 /* Generate a mapped number */
682 mapping->repl = nr_pool_next(map->pool);
683
684 /* Add to the tail to always yield a list sorted by expiry, in
685 * ascending order. */
686 llist_add_tail(&mapping->entry, &map->mappings);
Neels Hofmeyr508514c2015-11-24 13:30:38 +0100687 nr_map_refresh(map, mapping, now);
688}
689
690void nr_map_refresh(struct nr_map *map, struct nr_mapping *mapping, time_t now)
691{
692 if (!map->add_items_to_expiry)
693 return;
694 expiry_add(map->add_items_to_expiry,
695 &mapping->expiry_entry,
696 now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200697}
698
699void nr_map_clear(struct nr_map *map)
700{
701 struct nr_mapping *m;
702 struct nr_mapping *n;
703 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
704 nr_mapping_del(m);
705 }
706}
707
708int nr_map_empty(const struct nr_map *map)
709{
710 return llist_empty(&map->mappings);
711}
712
713struct nr_mapping *nr_map_get(const struct nr_map *map,
714 void *origin, nr_t nr_orig)
715{
716 struct nr_mapping *mapping;
717 llist_for_each_entry(mapping, &map->mappings, entry) {
718 if ((mapping->origin == origin)
719 && (mapping->orig == nr_orig))
720 return mapping;
721 }
722 /* Not found. */
723 return NULL;
724}
725
726struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
727{
728 struct nr_mapping *mapping;
729 llist_for_each_entry(mapping, &map->mappings, entry) {
730 if (mapping->repl == nr_repl) {
731 return mapping;
732 }
733 }
734 /* Not found. */
735 return NULL;
736}
737
738void nr_mapping_del(struct nr_mapping *mapping)
739{
740 OSMO_ASSERT(mapping);
741 llist_del(&mapping->entry);
742 INIT_LLIST_HEAD(&mapping->entry);
743 expiring_item_del(&mapping->expiry_entry);
744}
745
746
747/* gtphub */
748
749const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
750 "CTRL",
751 "USER",
752};
753
754const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
755 2123,
756 2152,
757};
758
Neels Hofmeyra9905a52015-11-29 23:49:48 +0100759const char* const gtphub_side_idx_names[GTPH_SIDE_N] = {
760 "SGSN",
761 "GGSN",
762};
763
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200764time_t gtphub_now(void)
765{
766 struct timespec now_tp;
767 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
768 return now_tp.tv_sec;
769}
770
771/* Remove a gtphub_peer from its list and free it. */
772static void gtphub_peer_del(struct gtphub_peer *peer)
773{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100774 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200775 nr_map_clear(&peer->seq_map);
776 llist_del(&peer->entry);
777 talloc_free(peer);
778}
779
780static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
781{
782 OSMO_ASSERT(llist_empty(&pa->ports));
783 llist_del(&pa->entry);
784 talloc_free(pa);
785}
786
787static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
788{
789 OSMO_ASSERT(pp->ref_count == 0);
790 llist_del(&pp->entry);
791 talloc_free(pp);
792}
793
794/* From the information in the gtp_packet_desc, return the address of a GGSN.
795 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100796static int gtphub_resolve_ggsn(struct gtphub *hub,
797 struct gtp_packet_desc *p,
798 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200799
800/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100801struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
802 const char *imsi_str,
803 const char *apn_ni_str);
804int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200805
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200806static void gtphub_zero(struct gtphub *hub)
807{
808 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100809 INIT_LLIST_HEAD(&hub->ggsn_lookups);
810 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200811}
812
813static int gtphub_sock_init(struct osmo_fd *ofd,
814 const struct gtphub_cfg_addr *addr,
815 osmo_fd_cb_t cb,
816 void *data,
817 int ofd_id)
818{
819 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100820 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200821 return -1;
822 }
823 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100824 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200825 return -1;
826 }
827
828 ofd->when = BSC_FD_READ;
829 ofd->cb = cb;
830 ofd->data = data;
831 ofd->priv_nr = ofd_id;
832
833 int rc;
834 rc = osmo_sock_init_ofd(ofd,
835 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
836 addr->addr_str, addr->port,
837 OSMO_SOCK_F_BIND);
838 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100839 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
840 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200841 return -1;
842 }
843
844 return 0;
845}
846
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100847static void gtphub_sock_close(struct osmo_fd *ofd)
848{
849 close(ofd->fd);
850 osmo_fd_unregister(ofd);
851 ofd->cb = NULL;
852}
853
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200854static void gtphub_bind_init(struct gtphub_bind *b)
855{
856 ZERO_STRUCT(b);
857
858 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100859
860 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
861 &gtphub_ctrg_io_desc, 0);
862 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200863}
864
865static int gtphub_bind_start(struct gtphub_bind *b,
866 const struct gtphub_cfg_bind *cfg,
867 osmo_fd_cb_t cb, void *cb_data,
868 unsigned int ofd_id)
869{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100870 LOG(LOGL_DEBUG, "Starting bind %s\n", b->label);
871 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0) {
872 LOG(LOGL_FATAL, "Invalid bind address for %s: %s\n",
873 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200874 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100875 }
876 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0) {
877 LOG(LOGL_FATAL, "Cannot bind for %s: %s\n",
878 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200879 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100880 }
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100881 b->local_port = cfg->bind.port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200882 return 0;
883}
884
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100885static void gtphub_bind_free(struct gtphub_bind *b)
886{
887 OSMO_ASSERT(llist_empty(&b->peers));
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100888 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100889}
890
891static void gtphub_bind_stop(struct gtphub_bind *b) {
892 gtphub_sock_close(&b->ofd);
893 gtphub_bind_free(b);
894}
895
Neels Hofmeyr40348972015-11-17 12:22:28 +0100896/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200897 * Return the number of bytes read, zero on error. */
898static int gtphub_read(const struct osmo_fd *from,
899 struct osmo_sockaddr *from_addr,
900 uint8_t *buf, size_t buf_len)
901{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100902 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200903
Neels Hofmeyr40348972015-11-17 12:22:28 +0100904 /* recvfrom requires the available length set in *from_addr_len. */
905 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200906 errno = 0;
907 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100908 (struct sockaddr*)&from_addr->a,
909 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200910 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
911 * is not truncated. Then maybe reduce buf's size. */
912
913 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100914 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
915 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200916 return 0;
917 }
918
Neels Hofmeyr40348972015-11-17 12:22:28 +0100919 LOG(LOGL_DEBUG, "Received %d bytes from %s\n%s\n",
920 (int)received, osmo_sockaddr_to_str(from_addr),
921 osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200922
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200923 return received;
924}
925
926inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
927{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100928 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200929 OSMO_ASSERT(pp->ref_count < UINT_MAX);
930 pp->ref_count++;
931}
932
933inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
934{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100935 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200936 OSMO_ASSERT(pp->ref_count > 0);
937 pp->ref_count--;
938}
939
940inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
941{
942 OSMO_ASSERT(p->version == 1);
943 p->data->gtp1l.h.seq = hton16(seq);
944 p->seq = seq;
945}
946
947inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
948{
949 OSMO_ASSERT(p->version == 1);
950 p->data->gtp1l.h.tei = hton32(tei);
951 p->header_tei = tei;
952}
953
954static void gtphub_mapping_del_cb(struct expiring_item *expi);
955
956static struct nr_mapping *gtphub_mapping_new()
957{
958 struct nr_mapping *nrm;
959 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
960 OSMO_ASSERT(nrm);
961
962 nr_mapping_init(nrm);
963 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
964 return nrm;
965}
966
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100967
968#define APPEND(args...) \
969 l = snprintf(pos, left, args); \
970 pos += l; \
971 left -= l
972
973static const char *gtphub_tunnel_side_str(struct gtphub_tunnel *tun,
974 int side_idx)
975{
976 static char buf[256];
977 char *pos = buf;
978 int left = sizeof(buf);
979 int l;
980
981 struct gtphub_tunnel_endpoint *c, *u;
982 c = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
983 u = &tun->endpoint[side_idx][GTPH_PLANE_USER];
984
985 /* print both only if they differ. */
986 if (!c->peer) {
987 APPEND("(uninitialized)");
988 } else {
989 APPEND("%s", gsn_addr_to_str(&c->peer->peer_addr->addr));
990 }
991
992 if (!u->peer) {
993 if (c->peer) {
994 APPEND(" / (uninitialized)");
995 }
996 } else if ((!c->peer)
997 || (!gsn_addr_same(&u->peer->peer_addr->addr,
998 &c->peer->peer_addr->addr))) {
999 APPEND(" / %s", gsn_addr_to_str(&u->peer->peer_addr->addr));
1000 }
1001
1002 APPEND(" (TEI C %x=%x / U %x=%x)",
1003 c->tei_orig, c->tei_repl,
1004 u->tei_orig, u->tei_repl);
1005 return buf;
1006}
1007
1008const char *gtphub_tunnel_str(struct gtphub_tunnel *tun)
1009{
1010 static char buf[512];
1011 char *pos = buf;
1012 int left = sizeof(buf);
1013 int l;
1014
1015 APPEND("%s", gtphub_tunnel_side_str(tun, GTPH_SIDE_SGSN));
1016 APPEND(" <-> %s", gtphub_tunnel_side_str(tun, GTPH_SIDE_GGSN));
1017
1018 return buf;
1019}
1020
1021#undef APPEND
1022
1023void gtphub_tunnel_endpoint_set_peer(struct gtphub_tunnel_endpoint *te,
1024 struct gtphub_peer_port *pp)
1025{
1026 if (te->peer)
1027 gtphub_port_ref_count_dec(te->peer);
1028 te->peer = pp;
1029 if (te->peer)
1030 gtphub_port_ref_count_inc(te->peer);
1031}
1032
1033int gtphub_tunnel_complete(struct gtphub_tunnel *tun)
1034{
1035 if (!tun)
1036 return 0;
1037 int side_idx;
1038 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001039 for_each_side_and_plane(side_idx, plane_idx) {
1040 struct gtphub_tunnel_endpoint *te =
1041 &tun->endpoint[side_idx][plane_idx];
1042 if (!(te->peer && te->tei_orig && te->tei_repl))
1043 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001044 }
1045 return 1;
1046}
1047
1048static void gtphub_tunnel_del_cb(struct expiring_item *expi)
1049{
1050 struct gtphub_tunnel *tun = container_of(expi,
1051 struct gtphub_tunnel,
1052 expiry_entry);
1053 LOG(LOGL_DEBUG, "expired: %s\n", gtphub_tunnel_str(tun));
1054
1055 llist_del(&tun->entry);
1056 INIT_LLIST_HEAD(&tun->entry); /* mark unused */
1057
1058 expi->del_cb = 0; /* avoid recursion loops */
1059 expiring_item_del(&tun->expiry_entry); /* usually already done, but make sure. */
1060
1061 int side_idx;
1062 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001063 for_each_side_and_plane(side_idx, plane_idx) {
1064 /* clear ref count */
1065 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1066 NULL);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001067 }
1068
1069 talloc_free(tun);
1070}
1071
1072static struct gtphub_tunnel *gtphub_tunnel_new()
1073{
1074 struct gtphub_tunnel *tun;
1075 tun = talloc_zero(osmo_gtphub_ctx, struct gtphub_tunnel);
1076 OSMO_ASSERT(tun);
1077
1078 INIT_LLIST_HEAD(&tun->entry);
1079 expiring_item_init(&tun->expiry_entry);
1080
1081 tun->expiry_entry.del_cb = gtphub_tunnel_del_cb;
1082 return tun;
1083}
1084
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001085static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
1086 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001087{
1088 if (llist_empty(&peer->addresses))
1089 return "(addressless)";
1090
1091 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
1092 struct gtphub_peer_addr,
1093 entry);
1094 return gsn_addr_to_strb(&a->addr, buf, buflen);
1095}
1096
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001097static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
1098 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001099{
1100 if (!port)
1101 return "(null port)";
1102
1103 snprintf(buf, buflen, "%s port %d",
1104 gsn_addr_to_str(&port->peer_addr->addr),
1105 (int)port->port);
1106 return buf;
1107}
1108
1109const char *gtphub_peer_str(struct gtphub_peer *peer)
1110{
1111 static char buf[256];
1112 return gtphub_peer_strb(peer, buf, sizeof(buf));
1113}
1114
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001115const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001116{
1117 static char buf[256];
1118 return gtphub_port_strb(port, buf, sizeof(buf));
1119}
1120
1121static const char *gtphub_port_str2(struct gtphub_peer_port *port)
1122{
1123 static char buf[256];
1124 return gtphub_port_strb(port, buf, sizeof(buf));
1125}
1126
1127static void gtphub_mapping_del_cb(struct expiring_item *expi)
1128{
1129 expi->del_cb = 0; /* avoid recursion loops */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001130 expiring_item_del(expi); /* usually already done, but make sure. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001131
1132 struct nr_mapping *nrm = container_of(expi,
1133 struct nr_mapping,
1134 expiry_entry);
1135 llist_del(&nrm->entry);
1136 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
1137
1138 /* Just for log */
1139 struct gtphub_peer_port *from = nrm->origin;
1140 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001141 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001142 (int)nrm->expiry_entry.expiry,
1143 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001144 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001145
1146 gtphub_port_ref_count_dec(from);
1147
1148 talloc_free(nrm);
1149}
1150
1151static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
1152 struct gtphub_peer_port *from,
1153 nr_t orig_nr,
1154 time_t now)
1155{
1156 struct nr_mapping *nrm;
1157
1158 nrm = nr_map_get(map, from, orig_nr);
1159
1160 if (!nrm) {
1161 nrm = gtphub_mapping_new();
1162 nrm->orig = orig_nr;
1163 nrm->origin = from;
1164 nr_map_add(map, nrm, now);
1165 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001166 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001167 gtphub_port_str(from),
1168 (int)(nrm->orig), (int)(nrm->repl));
1169 } else {
Neels Hofmeyr508514c2015-11-24 13:30:38 +01001170 nr_map_refresh(map, nrm, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001171 }
1172
1173 OSMO_ASSERT(nrm);
1174 return nrm;
1175}
1176
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001177static void gtphub_map_seq(struct gtp_packet_desc *p,
1178 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001179 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001180{
1181 /* Store a mapping in to_peer's map, so when we later receive a GTP
1182 * packet back from to_peer, the seq nr can be unmapped back to its
1183 * origin (from_peer here). */
1184 struct nr_mapping *nrm;
1185 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001186 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001187
1188 /* Change the GTP packet to yield the new, mapped seq nr */
1189 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001190}
1191
1192static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1193 struct gtphub_peer_port *responding_port)
1194{
1195 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001196 struct nr_mapping *nrm =
1197 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1198 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001199 if (!nrm)
1200 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001201 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1202 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001203 set_seq(p, nrm->orig);
1204 return nrm->origin;
1205}
1206
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001207static struct gtphub_tunnel *gtphub_tun_find(struct gtphub *hub,
1208 int side_idx,
1209 int plane_idx,
1210 struct gtphub_peer_port *from,
1211 uint32_t tei_orig,
1212 uint32_t tei_repl)
1213{
1214 OSMO_ASSERT(from);
1215
1216 struct gtphub_tunnel *tun;
1217 /* TODO: optimize: don't iterate *all* tunnels. */
1218 llist_for_each_entry(tun, &hub->tunnels, entry) {
1219 struct gtphub_tunnel_endpoint *te =
1220 &tun->endpoint[side_idx][plane_idx];
1221 if (((!tei_orig) || (te->tei_orig == tei_orig))
1222 && ((!tei_repl) || (te->tei_repl == tei_repl))
1223 && gsn_addr_same(&te->peer->peer_addr->addr, &from->peer_addr->addr))
1224 return tun;
1225 }
1226 return NULL;
1227}
1228
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001229static int gtphub_check_mapped_tei(struct gtphub_tunnel_endpoint *new_te,
1230 struct gtphub_tunnel_endpoint *iterated_te,
1231 uint32_t *tei_min,
1232 uint32_t *tei_max)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001233{
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001234 if (new_te->tei_repl != iterated_te->tei_repl)
1235 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001236
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001237 /* new_te->tei_repl is already taken. Try to find one out of the known
1238 * range. */
1239
1240 if ((*tei_max) < 0xffffffff) {
1241 (*tei_max)++;
1242 new_te->tei_repl = *tei_max;
1243 return 1;
1244 } else if ((*tei_min) > 1) {
1245 (*tei_min)--;
1246 new_te->tei_repl = *tei_min;
1247 return 1;
1248 }
1249
1250 /* None seems to be available. */
1251 return 0;
1252}
1253
1254static int gtphub_check_reused_teis(struct gtphub *hub,
1255 struct gtphub_tunnel *new_tun)
1256{
1257 uint32_t tei_min = 0xffffffff;
1258 uint32_t tei_max = 0;
1259 int side_idx;
1260 int plane_idx;
1261 int side_idx2;
1262 int plane_idx2;
1263 struct gtphub_tunnel_endpoint *te;
1264 struct gtphub_tunnel_endpoint *te2;
1265
1266 struct gtphub_tunnel *tun, *ntun;
1267
1268 llist_for_each_entry_safe(tun, ntun, &hub->tunnels, entry) {
1269 if (tun == new_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001270 continue;
1271
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001272 /* Check whether the GSN sent a TEI that it is reusing from a
1273 * previous tunnel. */
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001274 int tun_continue = 0;
1275 for_each_side(side_idx) {
1276 for_each_plane(plane_idx) {
1277 te = &tun->endpoint[side_idx][plane_idx];
1278 te2 = &new_tun->endpoint[side_idx][plane_idx];
1279 if ((te->tei_orig != te2->tei_orig)
1280 || (!te->peer)
1281 || (!te2->peer)
1282 || !gsn_addr_same(&te->peer->peer_addr->addr,
1283 &te2->peer->peer_addr->addr))
1284 continue;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001285
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001286 /* The peer is reusing a TEI that I believe to
1287 * be part of another tunnel. The other tunnel
1288 * must be stale, then. */
1289 LOG(LOGL_NOTICE,
1290 "Expiring tunnel due to reused TEI:"
1291 " peer %s sent %s TEI %x,"
1292 " previously used by tunnel %s...\n",
1293 gtphub_port_str(te->peer),
1294 gtphub_plane_idx_names[plane_idx],
1295 te->tei_orig,
1296 gtphub_tunnel_str(tun));
1297 LOG(LOGL_NOTICE, "...while establishing tunnel %s\n",
1298 gtphub_tunnel_str(new_tun));
Neels Hofmeyr52c0bd32015-12-02 14:14:32 +01001299
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001300 expiring_item_del(&tun->expiry_entry);
1301 /* continue to find more matches. There shouldn't be
1302 * any, but let's make sure. However, tun is deleted,
1303 * so we need to skip to the next tunnel. */
1304 tun_continue = 1;
1305 break;
1306 }
1307 if (tun_continue)
1308 break;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001309 }
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001310 if (tun_continue)
1311 continue;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001312
1313 /* Check whether the mapped TEIs assigned to the endpoints are
1314 * used anywhere else. */
1315 for_each_side_and_plane(side_idx, plane_idx) {
1316 te = &tun->endpoint[side_idx][plane_idx];
1317 tei_min = (tei_min < te->tei_repl)? tei_min : te->tei_repl;
1318 tei_max = (tei_max > te->tei_repl)? tei_max : te->tei_repl;
1319
1320 for_each_side_and_plane(side_idx2, plane_idx2) {
1321 te2 = &new_tun->endpoint[side_idx2][plane_idx2];
1322 if (!gtphub_check_mapped_tei(te2, te, &tei_min, &tei_max)) {
1323 LOG(LOGL_ERROR,
1324 "No mapped TEI is readily available."
1325 " Searching for holes between occupied"
1326 " TEIs not implemented.");
1327 return 0;
1328 }
1329 }
1330 }
1331
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001332 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001333
1334 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001335}
1336
1337static void gtphub_tunnel_refresh(struct gtphub *hub,
1338 struct gtphub_tunnel *tun,
1339 time_t now)
1340{
1341 expiry_add(&hub->expire_slowly,
1342 &tun->expiry_entry,
1343 now);
1344}
1345
1346static struct gtphub_tunnel_endpoint *gtphub_unmap_tei(struct gtphub *hub,
1347 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001348 struct gtphub_peer_port *from,
1349 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001350{
1351 OSMO_ASSERT(from);
1352 int other_side = other_side_idx(p->side_idx);
1353
1354 struct gtphub_tunnel *tun;
1355 llist_for_each_entry(tun, &hub->tunnels, entry) {
1356 struct gtphub_tunnel_endpoint *te_from =
1357 &tun->endpoint[p->side_idx][p->plane_idx];
1358 struct gtphub_tunnel_endpoint *te_to =
1359 &tun->endpoint[other_side][p->plane_idx];
1360 if ((te_to->tei_repl == p->header_tei_rx)
Neels Hofmeyrfc1be3a2015-12-02 00:31:31 +01001361 && te_from->peer
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001362 && gsn_addr_same(&te_from->peer->peer_addr->addr,
1363 &from->peer_addr->addr)) {
1364 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001365 if (unmapped_from_tun)
1366 *unmapped_from_tun = tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001367 return te_to;
1368 }
1369 }
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001370
1371 if (unmapped_from_tun)
1372 *unmapped_from_tun = NULL;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001373 return NULL;
1374}
1375
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001376static void gtphub_map_restart_counter(struct gtphub *hub,
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001377 struct gtp_packet_desc *p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001378{
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001379 if (p->rc != GTP_RC_PDU_C)
1380 return;
1381
1382 int ie_idx;
1383 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1384 if (ie_idx < 0)
1385 return;
1386
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001387 /* Always send gtphub's own restart counter */
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001388 p->ie[ie_idx]->tv1.v = hton8(hub->restart_counter);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001389}
1390
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001391static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001392 struct gtphub_tunnel **unmapped_from_tun,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001393 struct gtphub *hub,
1394 struct gtp_packet_desc *p,
1395 struct gtphub_peer_port *from_port)
1396{
1397 OSMO_ASSERT(p->version == 1);
1398 *to_port_p = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001399 if (unmapped_from_tun)
1400 *unmapped_from_tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001401
1402 /* If the header's TEI is zero, no PDP context has been established
1403 * yet. If nonzero, a mapping should actually already exist for this
1404 * TEI, since it must have been announced in a PDP context creation. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001405 if (!p->header_tei_rx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001406 return 0;
1407
1408 /* to_peer has previously announced a TEI, which was stored and
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001409 * mapped in a tunnel struct. */
1410 struct gtphub_tunnel_endpoint *to;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001411 to = gtphub_unmap_tei(hub, p, from_port, unmapped_from_tun);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001412 if (!to) {
1413 LOG(LOGL_ERROR, "Received unknown TEI %" PRIx32 " from %s\n",
1414 p->header_tei_rx, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001415 return -1;
1416 }
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001417 OSMO_ASSERT(*unmapped_from_tun);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001418
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001419 uint32_t unmapped_tei = to->tei_orig;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001420 set_tei(p, unmapped_tei);
1421
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001422 LOG(LOGL_DEBUG, "Unmapped TEI coming from: %s\n",
1423 gtphub_tunnel_str(*unmapped_from_tun));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001424
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001425 /* May be NULL for an invalidated tunnel. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001426 *to_port_p = to->peer;
1427
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001428 return 0;
1429}
1430
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001431static int gtphub_handle_create_pdp_ctx(struct gtphub *hub,
1432 struct gtp_packet_desc *p,
1433 struct gtphub_peer_port *from_ctrl,
1434 struct gtphub_peer_port *to_ctrl)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001435{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001436 int plane_idx;
1437
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001438 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1439 plane_nrs_match_GSN_addr_IE_indices);
1440
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001441 struct gtphub_tunnel *tun = NULL;
1442
1443 if (p->type == GTP_CREATE_PDP_REQ) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001444 if (p->side_idx != GTPH_SIDE_SGSN) {
1445 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
1446 " Request from the GGSN side");
1447 return -1;
1448 }
1449
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001450 /* A new tunnel. */
1451 tun = gtphub_tunnel_new();
1452 llist_add(&tun->entry, &hub->tunnels);
1453 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001454 /* The endpoint peers on this side (SGSN) will be set from IEs
1455 * below. Also set the GGSN Ctrl endpoint, for logging. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001456 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001457 to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001458 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001459 if (p->side_idx != GTPH_SIDE_GGSN) {
1460 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
1461 " Response from the SGSN side");
1462 return -1;
1463 }
1464
1465 /* Find the tunnel created during request. This is coming from
1466 * the GGSN side, so to_ctrl corresponds to the SGSN. */
1467 OSMO_ASSERT(to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001468 tun = gtphub_tun_find(hub, other_side_idx(p->side_idx),
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001469 p->plane_idx, to_ctrl, 0, p->header_tei_rx);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001470
1471 if (!tun) {
1472 LOG(LOGL_ERROR, "Create PDP Context Response: cannot"
1473 " find matching request from SGSN %s, mapped TEI %x.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001474 gtphub_port_str(to_ctrl), p->header_tei_rx);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001475 return -1;
1476 }
1477 }
1478
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001479 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1480 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001481 unsigned int side_idx = p->side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001482
1483 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01001484 int rc;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001485 struct gsn_addr addr_from_ie;
1486 uint32_t tei_from_ie;
1487 int ie_idx;
1488
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001489 /* Fetch GSN Address and TEI from IEs. As ensured by above
1490 * static asserts, plane_idx corresponds to the GSN Address IE
1491 * index (the first one = 0 = ctrl, second one = 1 = user). */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001492 rc = gsn_addr_get(&addr_from_ie, p, plane_idx);
1493 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001494 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1495 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001496 return -1;
1497 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001498 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001499 gtphub_plane_idx_names[plane_idx],
1500 gsn_addr_to_str(&addr_from_ie),
1501 addr_from_ie.len);
1502
1503 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1504 if (ie_idx < 0) {
1505 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001506 LOG(LOGL_ERROR,
1507 "Create PDP Context message invalid:"
1508 " missing IE %d\n",
1509 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001510 return -1;
1511 }
1512 tei_from_ie = 0;
1513 }
1514 else
1515 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1516
1517 /* Make sure an entry for this peer address with default port
1518 * exists */
1519 struct gtphub_peer_port *peer_from_ie =
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001520 gtphub_port_have(hub, &hub->to_gsns[side_idx][plane_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001521 &addr_from_ie,
1522 gtphub_plane_idx_default_port[plane_idx]);
1523
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001524 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1525 peer_from_ie);
1526
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001527 if (tei_from_ie) {
1528 /* Create TEI mapping and replace in GTP packet IE */
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01001529 uint32_t mapped_tei = nr_pool_next(&hub->tei_pool);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001530
1531 tun->endpoint[side_idx][plane_idx].tei_orig = tei_from_ie;
1532 tun->endpoint[side_idx][plane_idx].tei_repl = mapped_tei;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001533 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001534
Neels Hofmeyrcd865d62015-11-30 12:58:48 +01001535 if (!gtphub_check_reused_teis(hub, tun)) {
1536 /* It's highly unlikely that all TEIs are
1537 * taken. But the code looking for an unused
1538 * TEI is, at the time of writing this comment,
1539 * not able to find gaps in the TEI space. To
1540 * explicitly alert the user of this problem,
1541 * rather abort than carry on. */
1542 LOG(LOGL_FATAL, "TEI range exhausted. Cannot create TEI mapping, aborting.\n");
1543 abort();
1544 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001545 }
1546
1547 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001548 rc = gsn_addr_put(&hub->to_gsns[other_side_idx(side_idx)][plane_idx].local_addr,
1549 p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001550 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001551 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1552 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001553 return -1;
1554 }
1555 }
1556
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001557 if (p->type == GTP_CREATE_PDP_REQ) {
1558 LOG(LOGL_DEBUG, "New tunnel, first half: %s\n",
1559 gtphub_tunnel_str(tun));
Neels Hofmeyr005f1752015-11-30 12:19:11 +01001560 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001561 LOG(LOGL_DEBUG, "New tunnel: %s\n",
1562 gtphub_tunnel_str(tun));
1563 }
1564
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001565 return 0;
1566}
1567
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001568static void pending_delete_del_cb(struct expiring_item *expi)
1569{
1570 struct pending_delete *pd;
1571 pd = container_of(expi, struct pending_delete, expiry_entry);
1572
1573 llist_del(&pd->entry);
1574 INIT_LLIST_HEAD(&pd->entry);
1575
1576 pd->expiry_entry.del_cb = 0;
1577 expiring_item_del(&pd->expiry_entry);
1578
1579 talloc_free(pd);
1580}
1581
1582static struct pending_delete *pending_delete_new(void)
1583{
1584 struct pending_delete *pd = talloc_zero(osmo_gtphub_ctx, struct pending_delete);
1585 INIT_LLIST_HEAD(&pd->entry);
1586 expiring_item_init(&pd->expiry_entry);
1587 pd->expiry_entry.del_cb = pending_delete_del_cb;
1588 return pd;
1589}
1590
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001591static int gtphub_handle_delete_pdp_ctx(struct gtphub *hub,
1592 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001593 struct gtphub_tunnel *known_tun,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001594 struct gtphub_peer_port *from_ctrl,
1595 struct gtphub_peer_port *to_ctrl)
1596{
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001597 if (p->type == GTP_DELETE_PDP_REQ) {
1598 if (!known_tun) {
1599 LOG(LOGL_ERROR, "Cannot find tunnel for Delete PDP Context Request.\n");
1600 return -1;
1601 }
1602
1603 /* Store the Delete Request until a successful Response is seen. */
1604 uint8_t teardown_ind;
1605 uint8_t nsapi;
1606
1607 if (gtpie_gettv1(p->ie, GTPIE_TEARDOWN, 0, &teardown_ind) != 0) {
1608 LOG(LOGL_ERROR, "Missing Teardown Ind IE in Delete PDP Context Request.\n");
1609 return -1;
1610 }
1611
1612 if (gtpie_gettv1(p->ie, GTPIE_NSAPI, 0, &nsapi) != 0) {
1613 LOG(LOGL_ERROR, "Missing NSAPI IE in Delete PDP Context Request.\n");
1614 return -1;
1615 }
1616
1617 struct pending_delete *pd = NULL;
1618
1619 struct pending_delete *pdi = NULL;
1620 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1621 if ((pdi->tun == known_tun)
1622 && (pdi->teardown_ind == teardown_ind)
1623 && (pdi->nsapi == nsapi)) {
1624 pd = pdi;
1625 break;
1626 }
1627 }
1628
1629 if (!pd) {
1630 pd = pending_delete_new();
1631 pd->tun = known_tun;
1632 pd->teardown_ind = teardown_ind;
1633 pd->nsapi = nsapi;
1634
1635 LOG(LOGL_DEBUG, "Tunnel delete pending: %s\n",
1636 gtphub_tunnel_str(known_tun));
1637 llist_add(&pd->entry, &hub->pending_deletes);
1638 }
1639
1640 /* Add or refresh timeout. */
1641 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1642
1643 /* If a pending_delete should expire before the response to
1644 * indicate success comes in, the responding peer will have the
1645 * tunnel deactivated, while the requesting peer gets no reply
1646 * and keeps the tunnel. The hope is that the requesting peer
1647 * will try again and get a useful response. */
1648 } else if (p->type == GTP_DELETE_PDP_RSP) {
1649 /* Find the Delete Request for this Response. */
1650 struct pending_delete *pd = NULL;
1651
1652 struct pending_delete *pdi;
1653 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1654 if (known_tun == pdi->tun) {
1655 pd = pdi;
1656 break;
1657 }
1658 }
1659
1660 if (!pd) {
1661 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1662 " Cannot find matching request.");
1663 /* If we delete the tunnel now, anyone can send a
1664 * Delete response to kill tunnels at will. */
1665 return -1;
1666 }
1667
1668 /* TODO handle teardown_ind and nsapi */
1669
1670 expiring_item_del(&pd->expiry_entry);
1671
1672 uint8_t cause;
1673 if (gtpie_gettv1(p->ie, GTPIE_CAUSE, 0, &cause) != 0) {
1674 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1675 " Missing Cause IE.");
1676 /* If we delete the tunnel now, at least one of the
1677 * peers may still think it is active. */
1678 return -1;
1679 }
1680
1681 if (cause != GTPCAUSE_ACC_REQ) {
1682 LOG(LOGL_NOTICE,
1683 "Delete PDP Context Response indicates failure;"
1684 "for %s\n",
1685 gtphub_tunnel_str(known_tun));
1686 return -1;
1687 }
1688
1689 LOG(LOGL_DEBUG, "Delete PDP Context: removing tunnel %s\n",
1690 gtphub_tunnel_str(known_tun));
1691 expiring_item_del(&known_tun->expiry_entry);
1692 }
1693
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001694 return 0;
1695}
1696
1697static int gtphub_handle_update_pdp_ctx(struct gtphub *hub,
1698 struct gtp_packet_desc *p,
1699 struct gtphub_peer_port *from_ctrl,
1700 struct gtphub_peer_port *to_ctrl)
1701{
1702 /* TODO */
1703 return 0;
1704}
1705
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001706/* Read GSN address IEs from p, and make sure these peer addresses exist in
1707 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1708 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1709 * the packet p. */
1710static int gtphub_handle_pdp_ctx(struct gtphub *hub,
1711 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001712 struct gtphub_tunnel *known_tun,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001713 struct gtphub_peer_port *from_ctrl,
1714 struct gtphub_peer_port *to_ctrl)
1715{
1716 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1717
1718 switch (p->type) {
1719 case GTP_CREATE_PDP_REQ:
1720 case GTP_CREATE_PDP_RSP:
1721 return gtphub_handle_create_pdp_ctx(hub, p,
1722 from_ctrl, to_ctrl);
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001723
1724 case GTP_DELETE_PDP_REQ:
1725 case GTP_DELETE_PDP_RSP:
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001726 return gtphub_handle_delete_pdp_ctx(hub, p, known_tun,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001727 from_ctrl, to_ctrl);
1728
1729 case GTP_UPDATE_PDP_REQ:
1730 case GTP_UPDATE_PDP_RSP:
1731 return gtphub_handle_update_pdp_ctx(hub, p,
1732 from_ctrl, to_ctrl);
1733
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001734 default:
1735 /* Nothing to do for this message type. */
1736 return 0;
1737 }
1738
1739}
1740
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001741static int gtphub_send_del_pdp_ctx(struct gtphub *hub,
1742 struct gtphub_tunnel *tun,
1743 int to_side)
1744{
1745 static uint8_t del_ctx_msg[16] = {
1746 0x32, /* GTP v1 flags */
1747 GTP_DELETE_PDP_REQ,
1748 0x00, 0x08, /* Length in network byte order */
1749 0x00, 0x00, 0x00, 0x00, /* TEI to be replaced */
1750 0, 0, /* Seq, to be replaced */
1751 0, 0, /* no extensions */
1752 0x13, 0xff, /* 19: Teardown ind = 1 */
1753 0x14, 0 /* 20: NSAPI = 0 */
1754 };
1755
1756 uint32_t *tei = (uint32_t*)&del_ctx_msg[4];
1757 uint16_t *seq = (uint16_t*)&del_ctx_msg[8];
1758
1759 struct gtphub_tunnel_endpoint *te =
1760 &tun->endpoint[to_side][GTPH_PLANE_CTRL];
1761
1762 if (! te->peer)
1763 return 0;
1764
1765 *tei = hton32(te->tei_orig);
1766 *seq = hton16(nr_pool_next(&te->peer->peer_addr->peer->seq_pool));
1767
1768 struct gtphub_bind *to_bind = &hub->to_gsns[to_side][GTPH_PLANE_CTRL];
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01001769 int rc = gtphub_write(&to_bind->ofd, &te->peer->sa,
1770 del_ctx_msg, sizeof(del_ctx_msg));
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001771 if (rc != 0) {
1772 LOG(LOGL_ERROR,
1773 "Failed to send out-of-band Delete PDP Context Request to %s\n",
1774 gtphub_port_str(te->peer));
1775 }
1776 return rc;
1777}
1778
1779/* Tell all peers on the other end of tunnels that PDP contexts are void. */
1780static void gtphub_restarted(struct gtphub *hub,
1781 struct gtp_packet_desc *p,
1782 struct gtphub_peer_port *pp)
1783{
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001784 LOG(LOGL_DEBUG, "Peer has restarted: %s\n",
1785 gtphub_port_str(pp));
1786
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001787 struct gtphub_tunnel *tun;
1788 llist_for_each_entry(tun, &hub->tunnels, entry) {
1789 int side_idx;
1790 for_each_side(side_idx) {
Neels Hofmeyrc6d51f52015-12-02 15:44:34 +01001791 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
1792 struct gtphub_tunnel_endpoint *te2 = &tun->endpoint[other_side_idx(side_idx)][GTPH_PLANE_CTRL];
1793 if ((!te->peer)
1794 || (!te2->tei_orig)
1795 || (pp->peer_addr->peer != te->peer->peer_addr->peer))
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001796 continue;
1797
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001798 LOG(LOGL_DEBUG, "Deleting tunnel due to peer restart: %s\n",
1799 gtphub_tunnel_str(tun));
1800
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001801 /* Send a Delete PDP Context Request to the
1802 * peer on the other side, remember the pending
1803 * delete and wait for the response to delete
1804 * the tunnel. Clear this side of the tunnel to
1805 * make sure it isn't used.
1806 *
1807 * Should the delete message send fail, or if no
1808 * response is received, this tunnel will expire. If
1809 * its TEIs come up in a new PDP Context Request, it
1810 * will be removed. If messages for this tunnel should
1811 * come in (from the not restarted side), they will be
1812 * dropped because the tunnel is rendered unusable. */
1813 gtphub_send_del_pdp_ctx(hub, tun, other_side_idx(side_idx));
1814
1815 struct pending_delete *pd;
1816 pd = pending_delete_new();
1817 pd->tun = tun;
1818 pd->teardown_ind = 0xff;
1819 pd->nsapi = 0;
1820 llist_add(&pd->entry, &hub->pending_deletes);
1821 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1822
1823 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_CTRL],
1824 NULL);
1825 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_USER],
1826 NULL);
1827 }
1828 }
1829}
1830
1831static int get_restart_count(struct gtp_packet_desc *p)
1832{
1833 int ie_idx;
1834 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1835 if (ie_idx < 0)
1836 return -1;
1837 return ntoh8(p->ie[ie_idx]->tv1.v);
1838}
1839
1840static void gtphub_check_restart_counter(struct gtphub *hub,
1841 struct gtp_packet_desc *p,
1842 struct gtphub_peer_port *from)
1843{
1844 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1845 * that doesn't match the peer's previously sent restart counter, clear
1846 * that peer and cancel PDP contexts. */
1847
1848 int restart = get_restart_count(p);
1849
1850 if ((restart < 0) || (restart > 255))
1851 return;
1852
1853 if ((from->last_restart_count >= 0) && (from->last_restart_count <= 255)) {
1854 if (from->last_restart_count != restart) {
1855 gtphub_restarted(hub, p, from);
1856 }
1857 }
1858
1859 from->last_restart_count = restart;
1860}
1861
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001862static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1863{
1864 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1865 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
1866 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1867 gtphub_plane_idx_names[plane_idx]);
1868
1869 if (!(what & BSC_FD_READ))
1870 return 0;
1871
1872 struct gtphub *hub = from_sgsns_ofd->data;
1873
1874 static uint8_t buf[4096];
1875 struct osmo_sockaddr from_addr;
1876 struct osmo_sockaddr to_addr;
1877 struct osmo_fd *to_ofd;
1878 int len;
1879 uint8_t *reply_buf;
1880
1881 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1882 if (len < 1)
1883 return 0;
1884
1885 len = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, &from_addr,
1886 buf, len, gtphub_now(),
1887 &reply_buf, &to_ofd, &to_addr);
1888 if (len < 1)
1889 return 0;
1890
1891 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
1892}
1893
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001894static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1895{
1896 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1897 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001898 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1899 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001900 if (!(what & BSC_FD_READ))
1901 return 0;
1902
1903 struct gtphub *hub = from_ggsns_ofd->data;
1904
1905 static uint8_t buf[4096];
1906 struct osmo_sockaddr from_addr;
1907 struct osmo_sockaddr to_addr;
1908 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001909 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001910 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001911
1912 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1913 if (len < 1)
1914 return 0;
1915
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001916 len = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, &from_addr,
1917 buf, len, gtphub_now(),
1918 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001919 if (len < 1)
1920 return 0;
1921
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001922 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001923}
1924
1925static int gtphub_unmap(struct gtphub *hub,
1926 struct gtp_packet_desc *p,
1927 struct gtphub_peer_port *from,
1928 struct gtphub_peer_port *to_proxy,
1929 struct gtphub_peer_port **final_unmapped,
1930 struct gtphub_peer_port **unmapped_from_seq,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001931 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001932{
1933 /* Always (try to) unmap sequence and TEI numbers, which need to be
1934 * replaced in the packet. Either way, give precedence to the proxy, if
1935 * configured. */
1936
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001937 if (unmapped_from_seq)
1938 *unmapped_from_seq = NULL;
1939 if (unmapped_from_tun)
1940 *unmapped_from_tun = NULL;
1941 if (final_unmapped)
1942 *final_unmapped = NULL;
1943
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001944 struct gtphub_peer_port *from_seq = NULL;
1945 struct gtphub_peer_port *from_tei = NULL;
1946 struct gtphub_peer_port *unmapped = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001947 struct gtphub_tunnel *tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001948
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001949 from_seq = gtphub_unmap_seq(p, from);
1950
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001951 if (gtphub_unmap_header_tei(&from_tei, &tun, hub, p, from) < 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001952 return -1;
1953
1954 struct gtphub_peer *from_peer = from->peer_addr->peer;
1955 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001956 LOG(LOGL_DEBUG,
1957 "Seq unmap and TEI unmap yield two different peers."
1958 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001959 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1960 gtphub_plane_idx_names[p->plane_idx],
1961 gtphub_peer_str(from_peer),
1962 (int)p->seq,
1963 gtphub_port_str(from_seq),
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001964 (unsigned int)p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001965 gtphub_port_str2(from_tei)
1966 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001967 }
1968 unmapped = (from_seq? from_seq : from_tei);
1969
1970 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001971 LOG(LOGL_NOTICE,
1972 "Unmap yields a different peer than the configured proxy."
1973 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001974 " unmapped: %s proxy: %s\n",
1975 gtphub_port_str(unmapped),
1976 gtphub_port_str2(to_proxy)
1977 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001978 }
1979 unmapped = (to_proxy? to_proxy : unmapped);
1980
1981 if (!unmapped) {
1982 /* Return no error, but returned pointers are all NULL. */
1983 return 0;
1984 }
1985
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001986 if (unmapped_from_seq)
1987 *unmapped_from_seq = from_seq;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001988 if (unmapped_from_tun)
1989 *unmapped_from_tun = tun;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001990 if (final_unmapped)
1991 *final_unmapped = unmapped;
1992 return 0;
1993}
1994
1995static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1996 uint16_t port,
1997 struct osmo_sockaddr *dst)
1998{
1999 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
2000}
2001
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002002/* If p is an Echo request, replace p's data with the matching response and
2003 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
2004 * detected. */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002005static int gtphub_handle_echo_req(struct gtphub *hub, struct gtp_packet_desc *p,
2006 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002007{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002008 if (p->type != GTP_ECHO_REQ)
2009 return 0;
2010
2011 static uint8_t echo_response_data[14] = {
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002012 0x32, /* GTP v1 flags */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002013 GTP_ECHO_RSP,
2014 0x00, 14 - 8, /* Length in network byte order */
2015 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
2016 0, 0, /* Seq, to be replaced */
2017 0, 0, /* no extensions */
2018 0x0e, /* Recovery IE */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002019 0 /* Restart counter, to be replaced */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002020 };
2021 uint16_t *seq = (uint16_t*)&echo_response_data[8];
2022 uint8_t *recovery = &echo_response_data[13];
2023
2024 *seq = hton16(p->seq);
2025 *recovery = hub->restart_counter;
2026
2027 *reply_buf = echo_response_data;
2028
2029 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002030}
2031
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002032struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2033 const struct osmo_sockaddr *addr);
2034
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002035/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
2036 * address to forward to. Return a pointer to the osmo_fd, but copy the
2037 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
2038 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
2039 * bytes to forward, 0 or less on failure. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002040int gtphub_handle_buf(struct gtphub *hub,
2041 unsigned int side_idx,
2042 unsigned int plane_idx,
2043 const struct osmo_sockaddr *from_addr,
2044 uint8_t *buf,
2045 size_t received,
2046 time_t now,
2047 uint8_t **reply_buf,
2048 struct osmo_fd **to_ofd,
2049 struct osmo_sockaddr *to_addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002050{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002051 struct gtphub_bind *from_bind = &hub->to_gsns[side_idx][plane_idx];
2052 struct gtphub_bind *to_bind = &hub->to_gsns[other_side_idx(side_idx)][plane_idx];
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002053
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002054 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
2055 received);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002056 LOG(LOGL_DEBUG, "%s rx %s from %s %s\n",
2057 (side_idx == GTPH_SIDE_GGSN)? "<-" : "->",
Neels Hofmeyre921e322015-11-11 00:45:50 +01002058 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002059 gtphub_side_idx_names[side_idx],
Neels Hofmeyre921e322015-11-11 00:45:50 +01002060 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002061
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01002062 struct gtp_packet_desc p;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002063 gtp_decode(buf, received, side_idx, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002064
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002065 if (p.rc <= 0) {
2066 LOG(LOGL_ERROR, "INVALID: dropping GTP packet from %s %s %s\n",
2067 gtphub_side_idx_names[side_idx],
2068 gtphub_plane_idx_names[plane_idx],
2069 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002070 return -1;
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002071 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002072
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002073 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2074
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002075 int reply_len;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002076 reply_len = gtphub_handle_echo_req(hub, &p, reply_buf);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002077 if (reply_len > 0) {
2078 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002079 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002080 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002081
2082 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2083 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2084 reply_len);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002085 LOG(LOGL_DEBUG, "%s Echo response to %s: %d bytes to %s\n",
2086 (side_idx == GTPH_SIDE_GGSN)? "-->" : "<--",
2087 gtphub_side_idx_names[side_idx],
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002088 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002089 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002090 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002091 if (reply_len < 0)
2092 return -1;
2093
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002094 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002095
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002096 /* If a proxy is configured, check that it's indeed that proxy talking
2097 * to us. A proxy is a forced 1:1 connection, e.g. to another gtphub,
2098 * so no-one else is allowed to talk to us from that side. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002099 struct gtphub_peer_port *from_peer = hub->proxy[side_idx][plane_idx];
2100 if (from_peer) {
2101 if (osmo_sockaddr_cmp(&from_peer->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002102 LOG(LOGL_ERROR,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002103 "Rejecting: %s proxy configured, but GTP packet"
2104 " received on %s bind is from another sender:"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002105 " proxy: %s sender: %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002106 gtphub_side_idx_names[side_idx],
2107 gtphub_side_idx_names[side_idx],
2108 gtphub_port_str(from_peer),
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002109 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002110 return -1;
2111 }
2112 }
2113
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002114 if (!from_peer) {
2115 /* Find or create a peer with a matching address. The sender's
2116 * port may in fact differ. */
2117 from_peer = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002118 }
2119
2120 /* If any PDP context has been created, we already have an entry for
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002121 * this GSN. If we don't have an entry, a GGSN has nothing to tell us
2122 * about, while an SGSN may initiate a PDP context. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002123 if (!from_peer) {
2124 if (side_idx == GTPH_SIDE_GGSN) {
2125 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
2126 osmo_sockaddr_to_str(from_addr));
2127 return -1;
2128 } else {
2129 /* SGSN */
2130 /* A new peer. If this is on the Ctrl plane, an SGSN
2131 * may make first contact without being known yet, so
2132 * create the peer struct for the current sender. */
2133 if (plane_idx != GTPH_PLANE_CTRL) {
2134 LOG(LOGL_ERROR,
2135 "Dropping packet: User plane peer was not"
2136 "announced by PDP Context: %s\n",
2137 osmo_sockaddr_to_str(from_addr));
2138 return -1;
2139 }
2140
2141 struct gsn_addr from_gsna;
2142 uint16_t from_port;
2143 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
2144 return -1;
2145
2146 from_peer = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
2147 }
2148 }
2149
2150 if (!from_peer) {
2151 /* This could theoretically happen for invalid address data or
2152 * somesuch. */
2153 LOG(LOGL_ERROR, "Dropping packet: invalid %s peer: %s\n",
2154 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002155 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002156 return -1;
2157 }
2158
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002159 LOG(LOGL_DEBUG, "from %s peer: %s\n", gtphub_side_idx_names[side_idx],
2160 gtphub_port_str(from_peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002161
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002162 struct gtphub_peer_port *to_peer_from_seq;
2163 struct gtphub_peer_port *to_peer;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002164 struct gtphub_tunnel *tun;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002165 if (gtphub_unmap(hub, &p, from_peer,
2166 hub->proxy[other_side_idx(side_idx)][plane_idx],
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002167 &to_peer, &to_peer_from_seq, &tun)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002168 != 0) {
2169 return -1;
2170 }
2171
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002172 if ((!to_peer) && (side_idx == GTPH_SIDE_SGSN)) {
2173 if (gtphub_resolve_ggsn(hub, &p, &to_peer) < 0)
2174 return -1;
2175 }
2176
2177 if (!to_peer) {
2178 LOG(LOGL_ERROR, "No %s to send to. Dropping packet.\n",
2179 gtphub_side_idx_names[other_side_idx(side_idx)]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002180 return -1;
2181 }
2182
2183 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002184 /* This may be a Create PDP Context response. If it is, there
2185 * are other addresses in the GTP message to set up apart from
2186 * the sender. */
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002187 if (gtphub_handle_pdp_ctx(hub, &p, tun, from_peer, to_peer)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002188 != 0)
2189 return -1;
2190 }
2191
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002192 gtphub_check_restart_counter(hub, &p, from_peer);
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002193 gtphub_map_restart_counter(hub, &p);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002194
2195 /* If the GGSN is replying to an SGSN request, the sequence nr has
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002196 * already been unmapped above (to_peer_from_seq != NULL), and we need not
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002197 * create a new mapping. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002198 if (!to_peer_from_seq)
2199 gtphub_map_seq(&p, from_peer, to_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002200
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002201 osmo_sockaddr_copy(to_addr, &to_peer->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002202
2203 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01002204
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002205 if (received) {
2206 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2207 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2208 received);
2209 }
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002210 LOG(LOGL_DEBUG, "%s Forward to %s: %d bytes to %s\n",
2211 (side_idx == GTPH_SIDE_SGSN)? "-->" : "<--",
2212 gtphub_side_idx_names[other_side_idx(side_idx)],
Neels Hofmeyre921e322015-11-11 00:45:50 +01002213 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002214 return received;
2215}
2216
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002217static void resolved_gssn_del_cb(struct expiring_item *expi)
2218{
2219 struct gtphub_resolved_ggsn *ggsn;
2220 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
2221
2222 gtphub_port_ref_count_dec(ggsn->peer);
2223 llist_del(&ggsn->entry);
2224
2225 ggsn->expiry_entry.del_cb = 0;
2226 expiring_item_del(&ggsn->expiry_entry);
2227
2228 talloc_free(ggsn);
2229}
2230
2231void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
2232 struct gsn_addr *resolved_addr,
2233 time_t now)
2234{
2235 struct gtphub_peer_port *pp;
2236 struct gtphub_resolved_ggsn *ggsn;
2237
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002238 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002239 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
2240 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01002241
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002242 pp = gtphub_port_have(hub, &hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002243 resolved_addr, 2123);
2244 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002245 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
2246 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002247 return;
2248 }
2249
2250 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
2251 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01002252 INIT_LLIST_HEAD(&ggsn->entry);
2253 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002254
2255 ggsn->peer = pp;
2256 gtphub_port_ref_count_inc(pp);
2257
2258 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01002259 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002260
2261 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002262 expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002263
2264 llist_add(&ggsn->entry, &hub->resolved_ggsns);
2265}
2266
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002267static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
2268{
2269 return pp->ref_count == 0;
2270}
2271
2272static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
2273{
2274 struct gtphub_peer_port *pp, *npp;
2275 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
2276 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002277 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002278 gtphub_port_str(pp));
2279 gtphub_peer_port_del(pp);
2280 }
2281 }
2282 return llist_empty(&pa->ports);
2283}
2284
2285static int gtphub_gc_peer(struct gtphub_peer *p)
2286{
2287 struct gtphub_peer_addr *pa, *npa;
2288 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
2289 if (gtphub_gc_peer_addr(pa)) {
2290 gtphub_peer_addr_del(pa);
2291 }
2292 }
2293
2294 /* Note that there's a ref_count in each gtphub_peer_port instance
2295 * listed within p->addresses, referenced by TEI mappings from
2296 * hub->tei_map. As long as those don't expire, this peer will stay. */
2297
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002298 return llist_empty(&p->addresses)
2299 && nr_map_empty(&p->seq_map);
2300}
2301
2302static void gtphub_gc_bind(struct gtphub_bind *b)
2303{
2304 struct gtphub_peer *p, *n;
2305 llist_for_each_entry_safe(p, n, &b->peers, entry) {
2306 if (gtphub_gc_peer(p)) {
2307 gtphub_peer_del(p);
2308 }
2309 }
2310}
2311
2312void gtphub_gc(struct gtphub *hub, time_t now)
2313{
2314 int expired;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002315 expired = expiry_tick(&hub->expire_quickly, now);
2316 expired += expiry_tick(&hub->expire_slowly, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002317
2318 /* ... */
2319
2320 if (expired) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002321 int s, p;
2322 for_each_side_and_plane(s, p) {
2323 gtphub_gc_bind(&hub->to_gsns[s][p]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002324 }
2325 }
2326}
2327
2328static void gtphub_gc_cb(void *data)
2329{
2330 struct gtphub *hub = data;
2331 gtphub_gc(hub, gtphub_now());
2332 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2333}
2334
2335static void gtphub_gc_start(struct gtphub *hub)
2336{
2337 hub->gc_timer.cb = gtphub_gc_cb;
2338 hub->gc_timer.data = hub;
2339
2340 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2341}
2342
2343/* called by unit tests */
2344void gtphub_init(struct gtphub *hub)
2345{
2346 gtphub_zero(hub);
2347
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002348 INIT_LLIST_HEAD(&hub->tunnels);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002349 INIT_LLIST_HEAD(&hub->pending_deletes);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002350
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002351 expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS);
2352 expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002353
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01002354 nr_pool_init(&hub->tei_pool, 1, 0xffffffff);
2355
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002356 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002357 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002358 for_each_side_and_plane(side_idx, plane_idx) {
2359 gtphub_bind_init(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002360 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002361
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002362 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].label = "SGSN Ctrl";
2363 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].label = "GGSN Ctrl";
2364 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].label = "SGSN User";
2365 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002366}
2367
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002368/* For the test suite, this is kept separate from gtphub_stop(), which also
2369 * closes sockets. The test suite avoids using sockets and would cause
2370 * segfaults when trying to close uninitialized ofds. */
2371void gtphub_free(struct gtphub *hub)
2372{
2373 /* By expiring all mappings, a garbage collection should free
2374 * everything else. A gtphub_bind_free() will assert that everything is
2375 * indeed empty. */
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002376 expiry_clear(&hub->expire_quickly);
2377 expiry_clear(&hub->expire_slowly);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002378
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002379 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002380 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002381 for_each_side_and_plane(side_idx, plane_idx) {
2382 gtphub_gc_bind(&hub->to_gsns[side_idx][plane_idx]);
2383 gtphub_bind_free(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002384 }
2385}
2386
2387void gtphub_stop(struct gtphub *hub)
2388{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002389 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002390 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002391 for_each_side_and_plane(side_idx, plane_idx) {
2392 gtphub_bind_stop(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002393 }
2394 gtphub_free(hub);
2395}
2396
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002397static int gtphub_make_proxy(struct gtphub *hub,
2398 struct gtphub_peer_port **pp,
2399 struct gtphub_bind *bind,
2400 const struct gtphub_cfg_addr *addr)
2401{
2402 if (!addr->addr_str)
2403 return 0;
2404
2405 struct gsn_addr gsna;
2406 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
2407 return -1;
2408
2409 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
2410
2411 /* This is *the* proxy. Make sure it is never expired. */
2412 gtphub_port_ref_count_inc(*pp);
2413 return 0;
2414}
2415
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002416int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
2417 uint8_t restart_counter)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002418{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002419 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002420
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002421 hub->restart_counter = restart_counter;
2422
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002423 /* If a Ctrl plane proxy is configured, ares will never be used. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002424 if (!cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str) {
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002425 if (gtphub_ares_init(hub) != 0) {
2426 LOG(LOGL_FATAL, "Failed to initialize ares\n");
2427 return -1;
2428 }
2429 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002430
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002431 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002432 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002433 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01002434 int rc;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002435 rc = gtphub_bind_start(&hub->to_gsns[side_idx][plane_idx],
2436 &cfg->to_gsns[side_idx][plane_idx],
2437 (side_idx == GTPH_SIDE_SGSN)
2438 ? from_sgsns_read_cb
2439 : from_ggsns_read_cb,
2440 hub, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002441 if (rc) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002442 LOG(LOGL_FATAL, "Failed to bind for %ss (%s)\n",
2443 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002444 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002445 return rc;
2446 }
2447 }
2448
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002449 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002450 if (gtphub_make_proxy(hub,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002451 &hub->proxy[side_idx][plane_idx],
2452 &hub->to_gsns[side_idx][plane_idx],
2453 &cfg->proxy[side_idx][plane_idx])
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002454 != 0) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002455 LOG(LOGL_FATAL, "Cannot configure %s proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002456 " %s port %d.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002457 gtphub_side_idx_names[side_idx],
2458 cfg->proxy[side_idx][plane_idx].addr_str,
2459 (int)cfg->proxy[side_idx][plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002460 return -1;
2461 }
2462 }
2463
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002464 for_each_side_and_plane(side_idx, plane_idx) {
2465 if (hub->proxy[side_idx][plane_idx])
2466 LOG(LOGL_NOTICE, "Using %s %s proxy %s\n",
2467 gtphub_side_idx_names[side_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002468 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002469 gtphub_port_str(hub->proxy[side_idx][plane_idx]));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002470 }
2471
2472 gtphub_gc_start(hub);
2473 return 0;
2474}
2475
2476static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
2477 const struct gsn_addr *addr)
2478{
2479 struct gtphub_peer_addr *a;
2480 llist_for_each_entry(a, &peer->addresses, entry) {
2481 if (gsn_addr_same(&a->addr, addr))
2482 return a;
2483 }
2484 return NULL;
2485}
2486
2487static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2488 uint16_t port)
2489{
2490 OSMO_ASSERT(port);
2491 struct gtphub_peer_port *pp;
2492 llist_for_each_entry(pp, &a->ports, entry) {
2493 if (pp->port == port)
2494 return pp;
2495 }
2496 return NULL;
2497}
2498
2499static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2500 const struct gsn_addr *addr)
2501{
2502 struct gtphub_peer *peer;
2503 llist_for_each_entry(peer, &bind->peers, entry) {
2504 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2505 if (a)
2506 return a;
2507 }
2508 return NULL;
2509}
2510
2511static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2512 const struct gsn_addr *addr,
2513 uint16_t port)
2514{
2515 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2516 if (!a)
2517 return NULL;
2518 return gtphub_addr_find_port(a, port);
2519}
2520
2521struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2522 const struct osmo_sockaddr *addr)
2523{
2524 struct gsn_addr gsna;
2525 uint16_t port;
2526 gsn_addr_from_sockaddr(&gsna, &port, addr);
2527 return gtphub_port_find(bind, &gsna, port);
2528}
2529
2530static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2531 struct gtphub_bind *bind)
2532{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002533 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2534 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002535 OSMO_ASSERT(peer);
2536
2537 INIT_LLIST_HEAD(&peer->addresses);
2538
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002539 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002540 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002541
2542 /* TODO use something random to pick the initial sequence nr.
2543 0x6d31 produces the ASCII character sequence 'm1', currently used in
2544 gtphub_nc_test.sh. */
2545 peer->seq_pool.last_nr = 0x6d31 - 1;
2546
2547 llist_add(&peer->entry, &bind->peers);
2548 return peer;
2549}
2550
2551static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2552 const struct gsn_addr *addr)
2553{
2554 struct gtphub_peer_addr *a;
2555 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2556 OSMO_ASSERT(a);
2557 a->peer = peer;
2558 gsn_addr_copy(&a->addr, addr);
2559 INIT_LLIST_HEAD(&a->ports);
2560 llist_add(&a->entry, &peer->addresses);
2561
2562 return a;
2563}
2564
2565static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2566 struct gtphub_bind *bind,
2567 const struct gsn_addr *addr)
2568{
2569 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2570 if (a)
2571 return a;
2572
2573 /* If we haven't found an address, that means we need to create an
2574 * entirely new peer for the new address. More addresses may be added
2575 * to this peer later, but not via this function. */
2576 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002577
2578 a = gtphub_peer_add_addr(peer, addr);
2579
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002580 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002581 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002582 gsn_addr_to_str(&a->addr));
2583
2584 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002585}
2586
2587static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2588 uint16_t port)
2589{
2590 struct gtphub_peer_port *pp;
2591
2592 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2593 OSMO_ASSERT(pp);
2594 pp->peer_addr = a;
2595 pp->port = port;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002596 pp->last_restart_count = -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002597
2598 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2599 talloc_free(pp);
2600 return NULL;
2601 }
2602
2603 llist_add(&pp->entry, &a->ports);
2604
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002605 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002606 gsn_addr_to_str(&a->addr),
2607 (int)port);
2608
2609 return pp;
2610}
2611
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002612struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2613 struct gtphub_bind *bind,
2614 const struct gsn_addr *addr,
2615 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002616{
2617 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2618
2619 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2620 if (pp)
2621 return pp;
2622
2623 return gtphub_addr_add_port(a, port);
2624}
2625
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002626/* Find a GGSN peer with a matching address. If the address is known but the
2627 * port not, create a new port for that peer address. */
2628struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2629 const struct osmo_sockaddr *addr)
2630{
2631 struct gtphub_peer_addr *pa;
2632 struct gtphub_peer_port *pp;
2633
2634 struct gsn_addr gsna;
2635 uint16_t port;
2636 gsn_addr_from_sockaddr(&gsna, &port, addr);
2637
2638 pa = gtphub_addr_find(bind, &gsna);
2639 if (!pa)
2640 return NULL;
2641
2642 pp = gtphub_addr_find_port(pa, port);
2643
2644 if (!pp)
2645 pp = gtphub_addr_add_port(pa, port);
2646
2647 return pp;
2648}
2649
2650
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002651/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2652 * resolution should be possible but failed, and 1 if resolution was
2653 * successful. *pp will be set to NULL if <1 is returned. */
2654static int gtphub_resolve_ggsn(struct gtphub *hub,
2655 struct gtp_packet_desc *p,
2656 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002657{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002658 *pp = NULL;
2659
2660 /* TODO determine from message type whether IEs should be present? */
2661
2662 int rc;
2663 const char *imsi_str;
2664 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2665 if (rc < 1)
2666 return rc;
2667 OSMO_ASSERT(imsi_str);
2668
2669 const char *apn_str;
2670 rc = get_ie_apn_str(p->ie, &apn_str);
2671 if (rc < 1)
2672 return rc;
2673 OSMO_ASSERT(apn_str);
2674
2675 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2676 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002677}
2678
2679
2680/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002681/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002682/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2683 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002684static int _osmo_getaddrinfo(struct addrinfo **result,
2685 uint16_t family, uint16_t type, uint8_t proto,
2686 const char *host, uint16_t port)
2687{
2688 struct addrinfo hints;
2689 char portbuf[16];
2690
2691 sprintf(portbuf, "%u", port);
2692 memset(&hints, '\0', sizeof(struct addrinfo));
2693 hints.ai_family = family;
2694 if (type == SOCK_RAW) {
2695 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2696 * SOCK_RAW and IPPROTO_GRE is used.
2697 */
2698 hints.ai_socktype = SOCK_DGRAM;
2699 hints.ai_protocol = IPPROTO_UDP;
2700 } else {
2701 hints.ai_socktype = type;
2702 hints.ai_protocol = proto;
2703 }
2704
2705 return getaddrinfo(host, portbuf, &hints, result);
2706}
2707
2708/* TODO move to osmocom/core/socket.c ? */
2709int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2710 uint16_t family, uint16_t type, uint8_t proto,
2711 const char *host, uint16_t port)
2712{
2713 struct addrinfo *res;
2714 int rc;
2715 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2716
2717 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002718 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002719 return -EINVAL;
2720 }
2721
2722 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2723 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2724 addr->l = res->ai_addrlen;
2725 freeaddrinfo(res);
2726
2727 return 0;
2728}
2729
2730int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2731 char *port_str, size_t port_str_len,
2732 const struct osmo_sockaddr *addr,
2733 int flags)
2734{
2735 int rc;
2736
2737 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2738 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2739 return -1;
2740 }
2741
2742 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002743 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2744 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002745 return -1;
2746 }
2747
2748 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2749 addr_str, addr_str_len,
2750 port_str, port_str_len,
2751 flags);
2752
2753 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002754 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2755 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2756 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002757
2758 return rc;
2759}
2760
2761const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2762 char *buf, size_t buf_len)
2763{
2764 const int portbuf_len = 6;
2765 OSMO_ASSERT(buf_len > portbuf_len);
2766 char *portbuf = buf + buf_len - portbuf_len;
2767 buf_len -= portbuf_len;
2768 if (osmo_sockaddr_to_strs(buf, buf_len,
2769 portbuf, portbuf_len,
2770 addr,
2771 NI_NUMERICHOST | NI_NUMERICSERV))
2772 return NULL;
2773
2774 char *pos = buf + strnlen(buf, buf_len-1);
2775 size_t len = buf_len - (pos - buf);
2776
2777 snprintf(pos, len, " port %s", portbuf);
2778 buf[buf_len-1] = '\0';
2779
2780 return buf;
2781}
2782
2783const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2784{
2785 static char buf[256];
2786 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2787 if (! result)
2788 return "(invalid)";
2789 return result;
2790}
2791
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002792int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2793 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002794{
2795 if (a == b)
2796 return 0;
2797 if (!a)
2798 return -1;
2799 if (!b)
2800 return 1;
2801 if (a->l != b->l) {
2802 /* Lengths are not the same, but determine the order. Will
2803 * anyone ever sort a list by osmo_sockaddr though...? */
2804 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2805 if (cmp == 0) {
2806 if (a->l < b->l)
2807 return -1;
2808 else
2809 return 1;
2810 }
2811 return cmp;
2812 }
2813 return memcmp(&a->a, &b->a, a->l);
2814}
2815
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002816void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2817 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002818{
2819 OSMO_ASSERT(src->l <= sizeof(dst->a));
2820 memcpy(&dst->a, &src->a, src->l);
2821 dst->l = src->l;
2822}