blob: 6428c60a156875a72ec8a340f418436f87270cd4 [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 Hofmeyr7174b162015-11-27 01:22:13 +01001207static int gtphub_check_mapped_tei(struct gtphub_tunnel_endpoint *new_te,
1208 struct gtphub_tunnel_endpoint *iterated_te,
1209 uint32_t *tei_min,
1210 uint32_t *tei_max)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001211{
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001212 if (new_te->tei_repl != iterated_te->tei_repl)
1213 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001214
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001215 /* new_te->tei_repl is already taken. Try to find one out of the known
1216 * range. */
1217
1218 if ((*tei_max) < 0xffffffff) {
1219 (*tei_max)++;
1220 new_te->tei_repl = *tei_max;
1221 return 1;
1222 } else if ((*tei_min) > 1) {
1223 (*tei_min)--;
1224 new_te->tei_repl = *tei_min;
1225 return 1;
1226 }
1227
1228 /* None seems to be available. */
1229 return 0;
1230}
1231
1232static int gtphub_check_reused_teis(struct gtphub *hub,
1233 struct gtphub_tunnel *new_tun)
1234{
1235 uint32_t tei_min = 0xffffffff;
1236 uint32_t tei_max = 0;
1237 int side_idx;
1238 int plane_idx;
1239 int side_idx2;
1240 int plane_idx2;
1241 struct gtphub_tunnel_endpoint *te;
1242 struct gtphub_tunnel_endpoint *te2;
1243
1244 struct gtphub_tunnel *tun, *ntun;
1245
1246 llist_for_each_entry_safe(tun, ntun, &hub->tunnels, entry) {
1247 if (tun == new_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001248 continue;
1249
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001250 /* Check whether the GSN sent a TEI that it is reusing from a
1251 * previous tunnel. */
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001252 int tun_continue = 0;
1253 for_each_side(side_idx) {
1254 for_each_plane(plane_idx) {
1255 te = &tun->endpoint[side_idx][plane_idx];
1256 te2 = &new_tun->endpoint[side_idx][plane_idx];
1257 if ((te->tei_orig != te2->tei_orig)
1258 || (!te->peer)
1259 || (!te2->peer)
1260 || !gsn_addr_same(&te->peer->peer_addr->addr,
1261 &te2->peer->peer_addr->addr))
1262 continue;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001263
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001264 /* The peer is reusing a TEI that I believe to
1265 * be part of another tunnel. The other tunnel
1266 * must be stale, then. */
1267 LOG(LOGL_NOTICE,
1268 "Expiring tunnel due to reused TEI:"
1269 " peer %s sent %s TEI %x,"
1270 " previously used by tunnel %s...\n",
1271 gtphub_port_str(te->peer),
1272 gtphub_plane_idx_names[plane_idx],
1273 te->tei_orig,
1274 gtphub_tunnel_str(tun));
1275 LOG(LOGL_NOTICE, "...while establishing tunnel %s\n",
1276 gtphub_tunnel_str(new_tun));
Neels Hofmeyr52c0bd32015-12-02 14:14:32 +01001277
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001278 expiring_item_del(&tun->expiry_entry);
1279 /* continue to find more matches. There shouldn't be
1280 * any, but let's make sure. However, tun is deleted,
1281 * so we need to skip to the next tunnel. */
1282 tun_continue = 1;
1283 break;
1284 }
1285 if (tun_continue)
1286 break;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001287 }
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001288 if (tun_continue)
1289 continue;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001290
1291 /* Check whether the mapped TEIs assigned to the endpoints are
1292 * used anywhere else. */
1293 for_each_side_and_plane(side_idx, plane_idx) {
1294 te = &tun->endpoint[side_idx][plane_idx];
1295 tei_min = (tei_min < te->tei_repl)? tei_min : te->tei_repl;
1296 tei_max = (tei_max > te->tei_repl)? tei_max : te->tei_repl;
1297
1298 for_each_side_and_plane(side_idx2, plane_idx2) {
1299 te2 = &new_tun->endpoint[side_idx2][plane_idx2];
1300 if (!gtphub_check_mapped_tei(te2, te, &tei_min, &tei_max)) {
1301 LOG(LOGL_ERROR,
1302 "No mapped TEI is readily available."
1303 " Searching for holes between occupied"
1304 " TEIs not implemented.");
1305 return 0;
1306 }
1307 }
1308 }
1309
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001310 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001311
1312 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001313}
1314
1315static void gtphub_tunnel_refresh(struct gtphub *hub,
1316 struct gtphub_tunnel *tun,
1317 time_t now)
1318{
1319 expiry_add(&hub->expire_slowly,
1320 &tun->expiry_entry,
1321 now);
1322}
1323
1324static struct gtphub_tunnel_endpoint *gtphub_unmap_tei(struct gtphub *hub,
1325 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001326 struct gtphub_peer_port *from,
1327 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001328{
1329 OSMO_ASSERT(from);
1330 int other_side = other_side_idx(p->side_idx);
1331
1332 struct gtphub_tunnel *tun;
1333 llist_for_each_entry(tun, &hub->tunnels, entry) {
1334 struct gtphub_tunnel_endpoint *te_from =
1335 &tun->endpoint[p->side_idx][p->plane_idx];
1336 struct gtphub_tunnel_endpoint *te_to =
1337 &tun->endpoint[other_side][p->plane_idx];
1338 if ((te_to->tei_repl == p->header_tei_rx)
Neels Hofmeyrfc1be3a2015-12-02 00:31:31 +01001339 && te_from->peer
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001340 && gsn_addr_same(&te_from->peer->peer_addr->addr,
1341 &from->peer_addr->addr)) {
1342 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001343 if (unmapped_from_tun)
1344 *unmapped_from_tun = tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001345 return te_to;
1346 }
1347 }
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001348
1349 if (unmapped_from_tun)
1350 *unmapped_from_tun = NULL;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001351 return NULL;
1352}
1353
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001354static void gtphub_map_restart_counter(struct gtphub *hub,
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001355 struct gtp_packet_desc *p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001356{
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001357 if (p->rc != GTP_RC_PDU_C)
1358 return;
1359
1360 int ie_idx;
1361 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1362 if (ie_idx < 0)
1363 return;
1364
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001365 /* Always send gtphub's own restart counter */
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001366 p->ie[ie_idx]->tv1.v = hton8(hub->restart_counter);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001367}
1368
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001369static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001370 struct gtphub_tunnel **unmapped_from_tun,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001371 struct gtphub *hub,
1372 struct gtp_packet_desc *p,
1373 struct gtphub_peer_port *from_port)
1374{
1375 OSMO_ASSERT(p->version == 1);
1376 *to_port_p = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001377 if (unmapped_from_tun)
1378 *unmapped_from_tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001379
1380 /* If the header's TEI is zero, no PDP context has been established
1381 * yet. If nonzero, a mapping should actually already exist for this
1382 * TEI, since it must have been announced in a PDP context creation. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001383 if (!p->header_tei_rx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001384 return 0;
1385
1386 /* to_peer has previously announced a TEI, which was stored and
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001387 * mapped in a tunnel struct. */
1388 struct gtphub_tunnel_endpoint *to;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001389 to = gtphub_unmap_tei(hub, p, from_port, unmapped_from_tun);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001390 if (!to) {
1391 LOG(LOGL_ERROR, "Received unknown TEI %" PRIx32 " from %s\n",
1392 p->header_tei_rx, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001393 return -1;
1394 }
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001395 OSMO_ASSERT(*unmapped_from_tun);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001396
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001397 uint32_t unmapped_tei = to->tei_orig;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001398 set_tei(p, unmapped_tei);
1399
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001400 LOG(LOGL_DEBUG, "Unmapped TEI coming from: %s\n",
1401 gtphub_tunnel_str(*unmapped_from_tun));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001402
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001403 /* May be NULL for an invalidated tunnel. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001404 *to_port_p = to->peer;
1405
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001406 return 0;
1407}
1408
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001409static int gtphub_handle_create_pdp_ctx(struct gtphub *hub,
1410 struct gtp_packet_desc *p,
1411 struct gtphub_peer_port *from_ctrl,
1412 struct gtphub_peer_port *to_ctrl)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001413{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001414 int plane_idx;
1415
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001416 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1417 plane_nrs_match_GSN_addr_IE_indices);
1418
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001419 struct gtphub_tunnel *tun = NULL;
1420
1421 if (p->type == GTP_CREATE_PDP_REQ) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001422 if (p->side_idx != GTPH_SIDE_SGSN) {
1423 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001424 " Request from the GGSN side: %s",
1425 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001426 return -1;
1427 }
1428
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001429 /* A new tunnel. */
1430 tun = gtphub_tunnel_new();
1431 llist_add(&tun->entry, &hub->tunnels);
1432 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001433 /* The endpoint peers on this side (SGSN) will be set from IEs
1434 * below. Also set the GGSN Ctrl endpoint, for logging. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001435 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001436 to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001437 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001438 if (p->side_idx != GTPH_SIDE_GGSN) {
1439 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001440 " Response from the SGSN side: %s",
1441 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001442 return -1;
1443 }
1444
Neels Hofmeyr8c5b0732015-12-03 13:45:15 +01001445 /* The tunnel should already have been resolved from the header
1446 * TEI and be available in tun (== p->tun). Just fill in the
1447 * GSN Addresses below.*/
1448 OSMO_ASSERT(tun);
1449 OSMO_ASSERT(tun->endpoint[other_side_idx(p->side_idx)][GTPH_PLANE_CTRL].tei_repl
1450 == p->header_tei_rx);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001451 OSMO_ASSERT(to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001452 }
1453
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001454 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1455 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001456 unsigned int side_idx = p->side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001457
1458 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01001459 int rc;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001460 struct gsn_addr addr_from_ie;
1461 uint32_t tei_from_ie;
1462 int ie_idx;
1463
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001464 /* Fetch GSN Address and TEI from IEs. As ensured by above
1465 * static asserts, plane_idx corresponds to the GSN Address IE
1466 * index (the first one = 0 = ctrl, second one = 1 = user). */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001467 rc = gsn_addr_get(&addr_from_ie, p, plane_idx);
1468 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001469 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1470 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001471 return -1;
1472 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001473 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001474 gtphub_plane_idx_names[plane_idx],
1475 gsn_addr_to_str(&addr_from_ie),
1476 addr_from_ie.len);
1477
1478 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1479 if (ie_idx < 0) {
1480 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001481 LOG(LOGL_ERROR,
1482 "Create PDP Context message invalid:"
1483 " missing IE %d\n",
1484 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001485 return -1;
1486 }
1487 tei_from_ie = 0;
1488 }
1489 else
1490 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1491
1492 /* Make sure an entry for this peer address with default port
1493 * exists */
1494 struct gtphub_peer_port *peer_from_ie =
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001495 gtphub_port_have(hub, &hub->to_gsns[side_idx][plane_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001496 &addr_from_ie,
1497 gtphub_plane_idx_default_port[plane_idx]);
1498
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001499 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1500 peer_from_ie);
1501
Neels Hofmeyr59c1b642015-12-03 11:30:43 +01001502 if (!tei_from_ie &&
1503 !tun->endpoint[side_idx][plane_idx].tei_orig) {
1504 LOG(LOGL_ERROR,
1505 "Create PDP Context message omits %s TEI, but the"
1506 " no TEI has been announced for this tunnel: %s",
1507 gtphub_plane_idx_names[plane_idx],
1508 gtphub_tunnel_str(tun));
1509 return -1;
1510 }
1511
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001512 if (tei_from_ie) {
1513 /* Create TEI mapping and replace in GTP packet IE */
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01001514 uint32_t mapped_tei = nr_pool_next(&hub->tei_pool);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001515
1516 tun->endpoint[side_idx][plane_idx].tei_orig = tei_from_ie;
1517 tun->endpoint[side_idx][plane_idx].tei_repl = mapped_tei;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001518 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001519
Neels Hofmeyrcd865d62015-11-30 12:58:48 +01001520 if (!gtphub_check_reused_teis(hub, tun)) {
1521 /* It's highly unlikely that all TEIs are
1522 * taken. But the code looking for an unused
1523 * TEI is, at the time of writing this comment,
1524 * not able to find gaps in the TEI space. To
1525 * explicitly alert the user of this problem,
1526 * rather abort than carry on. */
1527 LOG(LOGL_FATAL, "TEI range exhausted. Cannot create TEI mapping, aborting.\n");
1528 abort();
1529 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001530 }
1531
1532 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001533 rc = gsn_addr_put(&hub->to_gsns[other_side_idx(side_idx)][plane_idx].local_addr,
1534 p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001535 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001536 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1537 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001538 return -1;
1539 }
1540 }
1541
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001542 if (p->type == GTP_CREATE_PDP_REQ) {
1543 LOG(LOGL_DEBUG, "New tunnel, first half: %s\n",
1544 gtphub_tunnel_str(tun));
Neels Hofmeyr005f1752015-11-30 12:19:11 +01001545 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001546 LOG(LOGL_DEBUG, "New tunnel: %s\n",
1547 gtphub_tunnel_str(tun));
1548 }
1549
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001550 return 0;
1551}
1552
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001553static void pending_delete_del_cb(struct expiring_item *expi)
1554{
1555 struct pending_delete *pd;
1556 pd = container_of(expi, struct pending_delete, expiry_entry);
1557
1558 llist_del(&pd->entry);
1559 INIT_LLIST_HEAD(&pd->entry);
1560
1561 pd->expiry_entry.del_cb = 0;
1562 expiring_item_del(&pd->expiry_entry);
1563
1564 talloc_free(pd);
1565}
1566
1567static struct pending_delete *pending_delete_new(void)
1568{
1569 struct pending_delete *pd = talloc_zero(osmo_gtphub_ctx, struct pending_delete);
1570 INIT_LLIST_HEAD(&pd->entry);
1571 expiring_item_init(&pd->expiry_entry);
1572 pd->expiry_entry.del_cb = pending_delete_del_cb;
1573 return pd;
1574}
1575
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001576static int gtphub_handle_delete_pdp_ctx(struct gtphub *hub,
1577 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001578 struct gtphub_tunnel *known_tun,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001579 struct gtphub_peer_port *from_ctrl,
1580 struct gtphub_peer_port *to_ctrl)
1581{
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001582 if (p->type == GTP_DELETE_PDP_REQ) {
1583 if (!known_tun) {
1584 LOG(LOGL_ERROR, "Cannot find tunnel for Delete PDP Context Request.\n");
1585 return -1;
1586 }
1587
1588 /* Store the Delete Request until a successful Response is seen. */
1589 uint8_t teardown_ind;
1590 uint8_t nsapi;
1591
1592 if (gtpie_gettv1(p->ie, GTPIE_TEARDOWN, 0, &teardown_ind) != 0) {
1593 LOG(LOGL_ERROR, "Missing Teardown Ind IE in Delete PDP Context Request.\n");
1594 return -1;
1595 }
1596
1597 if (gtpie_gettv1(p->ie, GTPIE_NSAPI, 0, &nsapi) != 0) {
1598 LOG(LOGL_ERROR, "Missing NSAPI IE in Delete PDP Context Request.\n");
1599 return -1;
1600 }
1601
1602 struct pending_delete *pd = NULL;
1603
1604 struct pending_delete *pdi = NULL;
1605 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1606 if ((pdi->tun == known_tun)
1607 && (pdi->teardown_ind == teardown_ind)
1608 && (pdi->nsapi == nsapi)) {
1609 pd = pdi;
1610 break;
1611 }
1612 }
1613
1614 if (!pd) {
1615 pd = pending_delete_new();
1616 pd->tun = known_tun;
1617 pd->teardown_ind = teardown_ind;
1618 pd->nsapi = nsapi;
1619
1620 LOG(LOGL_DEBUG, "Tunnel delete pending: %s\n",
1621 gtphub_tunnel_str(known_tun));
1622 llist_add(&pd->entry, &hub->pending_deletes);
1623 }
1624
1625 /* Add or refresh timeout. */
1626 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1627
1628 /* If a pending_delete should expire before the response to
1629 * indicate success comes in, the responding peer will have the
1630 * tunnel deactivated, while the requesting peer gets no reply
1631 * and keeps the tunnel. The hope is that the requesting peer
1632 * will try again and get a useful response. */
1633 } else if (p->type == GTP_DELETE_PDP_RSP) {
1634 /* Find the Delete Request for this Response. */
1635 struct pending_delete *pd = NULL;
1636
1637 struct pending_delete *pdi;
1638 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1639 if (known_tun == pdi->tun) {
1640 pd = pdi;
1641 break;
1642 }
1643 }
1644
1645 if (!pd) {
1646 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1647 " Cannot find matching request.");
1648 /* If we delete the tunnel now, anyone can send a
1649 * Delete response to kill tunnels at will. */
1650 return -1;
1651 }
1652
1653 /* TODO handle teardown_ind and nsapi */
1654
1655 expiring_item_del(&pd->expiry_entry);
1656
1657 uint8_t cause;
1658 if (gtpie_gettv1(p->ie, GTPIE_CAUSE, 0, &cause) != 0) {
1659 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1660 " Missing Cause IE.");
1661 /* If we delete the tunnel now, at least one of the
1662 * peers may still think it is active. */
1663 return -1;
1664 }
1665
1666 if (cause != GTPCAUSE_ACC_REQ) {
1667 LOG(LOGL_NOTICE,
1668 "Delete PDP Context Response indicates failure;"
1669 "for %s\n",
1670 gtphub_tunnel_str(known_tun));
1671 return -1;
1672 }
1673
1674 LOG(LOGL_DEBUG, "Delete PDP Context: removing tunnel %s\n",
1675 gtphub_tunnel_str(known_tun));
1676 expiring_item_del(&known_tun->expiry_entry);
1677 }
1678
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001679 return 0;
1680}
1681
1682static int gtphub_handle_update_pdp_ctx(struct gtphub *hub,
1683 struct gtp_packet_desc *p,
1684 struct gtphub_peer_port *from_ctrl,
1685 struct gtphub_peer_port *to_ctrl)
1686{
1687 /* TODO */
1688 return 0;
1689}
1690
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001691/* Read GSN address IEs from p, and make sure these peer addresses exist in
1692 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1693 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1694 * the packet p. */
1695static int gtphub_handle_pdp_ctx(struct gtphub *hub,
1696 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001697 struct gtphub_tunnel *known_tun,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001698 struct gtphub_peer_port *from_ctrl,
1699 struct gtphub_peer_port *to_ctrl)
1700{
1701 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1702
1703 switch (p->type) {
1704 case GTP_CREATE_PDP_REQ:
1705 case GTP_CREATE_PDP_RSP:
1706 return gtphub_handle_create_pdp_ctx(hub, p,
1707 from_ctrl, to_ctrl);
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001708
1709 case GTP_DELETE_PDP_REQ:
1710 case GTP_DELETE_PDP_RSP:
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001711 return gtphub_handle_delete_pdp_ctx(hub, p, known_tun,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001712 from_ctrl, to_ctrl);
1713
1714 case GTP_UPDATE_PDP_REQ:
1715 case GTP_UPDATE_PDP_RSP:
1716 return gtphub_handle_update_pdp_ctx(hub, p,
1717 from_ctrl, to_ctrl);
1718
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001719 default:
1720 /* Nothing to do for this message type. */
1721 return 0;
1722 }
1723
1724}
1725
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001726static int gtphub_send_del_pdp_ctx(struct gtphub *hub,
1727 struct gtphub_tunnel *tun,
1728 int to_side)
1729{
1730 static uint8_t del_ctx_msg[16] = {
1731 0x32, /* GTP v1 flags */
1732 GTP_DELETE_PDP_REQ,
1733 0x00, 0x08, /* Length in network byte order */
1734 0x00, 0x00, 0x00, 0x00, /* TEI to be replaced */
1735 0, 0, /* Seq, to be replaced */
1736 0, 0, /* no extensions */
1737 0x13, 0xff, /* 19: Teardown ind = 1 */
1738 0x14, 0 /* 20: NSAPI = 0 */
1739 };
1740
1741 uint32_t *tei = (uint32_t*)&del_ctx_msg[4];
1742 uint16_t *seq = (uint16_t*)&del_ctx_msg[8];
1743
1744 struct gtphub_tunnel_endpoint *te =
1745 &tun->endpoint[to_side][GTPH_PLANE_CTRL];
1746
1747 if (! te->peer)
1748 return 0;
1749
1750 *tei = hton32(te->tei_orig);
1751 *seq = hton16(nr_pool_next(&te->peer->peer_addr->peer->seq_pool));
1752
1753 struct gtphub_bind *to_bind = &hub->to_gsns[to_side][GTPH_PLANE_CTRL];
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01001754 int rc = gtphub_write(&to_bind->ofd, &te->peer->sa,
1755 del_ctx_msg, sizeof(del_ctx_msg));
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001756 if (rc != 0) {
1757 LOG(LOGL_ERROR,
1758 "Failed to send out-of-band Delete PDP Context Request to %s\n",
1759 gtphub_port_str(te->peer));
1760 }
1761 return rc;
1762}
1763
1764/* Tell all peers on the other end of tunnels that PDP contexts are void. */
1765static void gtphub_restarted(struct gtphub *hub,
1766 struct gtp_packet_desc *p,
1767 struct gtphub_peer_port *pp)
1768{
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001769 LOG(LOGL_DEBUG, "Peer has restarted: %s\n",
1770 gtphub_port_str(pp));
1771
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001772 struct gtphub_tunnel *tun;
1773 llist_for_each_entry(tun, &hub->tunnels, entry) {
1774 int side_idx;
1775 for_each_side(side_idx) {
Neels Hofmeyrc6d51f52015-12-02 15:44:34 +01001776 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
1777 struct gtphub_tunnel_endpoint *te2 = &tun->endpoint[other_side_idx(side_idx)][GTPH_PLANE_CTRL];
1778 if ((!te->peer)
1779 || (!te2->tei_orig)
1780 || (pp->peer_addr->peer != te->peer->peer_addr->peer))
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001781 continue;
1782
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001783 LOG(LOGL_DEBUG, "Deleting tunnel due to peer restart: %s\n",
1784 gtphub_tunnel_str(tun));
1785
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001786 /* Send a Delete PDP Context Request to the
1787 * peer on the other side, remember the pending
1788 * delete and wait for the response to delete
1789 * the tunnel. Clear this side of the tunnel to
1790 * make sure it isn't used.
1791 *
1792 * Should the delete message send fail, or if no
1793 * response is received, this tunnel will expire. If
1794 * its TEIs come up in a new PDP Context Request, it
1795 * will be removed. If messages for this tunnel should
1796 * come in (from the not restarted side), they will be
1797 * dropped because the tunnel is rendered unusable. */
1798 gtphub_send_del_pdp_ctx(hub, tun, other_side_idx(side_idx));
1799
1800 struct pending_delete *pd;
1801 pd = pending_delete_new();
1802 pd->tun = tun;
1803 pd->teardown_ind = 0xff;
1804 pd->nsapi = 0;
1805 llist_add(&pd->entry, &hub->pending_deletes);
1806 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1807
1808 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_CTRL],
1809 NULL);
1810 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_USER],
1811 NULL);
1812 }
1813 }
1814}
1815
1816static int get_restart_count(struct gtp_packet_desc *p)
1817{
1818 int ie_idx;
1819 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1820 if (ie_idx < 0)
1821 return -1;
1822 return ntoh8(p->ie[ie_idx]->tv1.v);
1823}
1824
1825static void gtphub_check_restart_counter(struct gtphub *hub,
1826 struct gtp_packet_desc *p,
1827 struct gtphub_peer_port *from)
1828{
1829 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1830 * that doesn't match the peer's previously sent restart counter, clear
1831 * that peer and cancel PDP contexts. */
1832
1833 int restart = get_restart_count(p);
1834
1835 if ((restart < 0) || (restart > 255))
1836 return;
1837
1838 if ((from->last_restart_count >= 0) && (from->last_restart_count <= 255)) {
1839 if (from->last_restart_count != restart) {
1840 gtphub_restarted(hub, p, from);
1841 }
1842 }
1843
1844 from->last_restart_count = restart;
1845}
1846
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001847static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1848{
1849 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1850 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
1851 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1852 gtphub_plane_idx_names[plane_idx]);
1853
1854 if (!(what & BSC_FD_READ))
1855 return 0;
1856
1857 struct gtphub *hub = from_sgsns_ofd->data;
1858
1859 static uint8_t buf[4096];
1860 struct osmo_sockaddr from_addr;
1861 struct osmo_sockaddr to_addr;
1862 struct osmo_fd *to_ofd;
1863 int len;
1864 uint8_t *reply_buf;
1865
1866 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1867 if (len < 1)
1868 return 0;
1869
1870 len = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, &from_addr,
1871 buf, len, gtphub_now(),
1872 &reply_buf, &to_ofd, &to_addr);
1873 if (len < 1)
1874 return 0;
1875
1876 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
1877}
1878
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001879static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1880{
1881 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1882 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001883 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1884 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001885 if (!(what & BSC_FD_READ))
1886 return 0;
1887
1888 struct gtphub *hub = from_ggsns_ofd->data;
1889
1890 static uint8_t buf[4096];
1891 struct osmo_sockaddr from_addr;
1892 struct osmo_sockaddr to_addr;
1893 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001894 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001895 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001896
1897 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1898 if (len < 1)
1899 return 0;
1900
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001901 len = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, &from_addr,
1902 buf, len, gtphub_now(),
1903 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001904 if (len < 1)
1905 return 0;
1906
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001907 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001908}
1909
1910static int gtphub_unmap(struct gtphub *hub,
1911 struct gtp_packet_desc *p,
1912 struct gtphub_peer_port *from,
1913 struct gtphub_peer_port *to_proxy,
1914 struct gtphub_peer_port **final_unmapped,
1915 struct gtphub_peer_port **unmapped_from_seq,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001916 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001917{
1918 /* Always (try to) unmap sequence and TEI numbers, which need to be
1919 * replaced in the packet. Either way, give precedence to the proxy, if
1920 * configured. */
1921
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001922 if (unmapped_from_seq)
1923 *unmapped_from_seq = NULL;
1924 if (unmapped_from_tun)
1925 *unmapped_from_tun = NULL;
1926 if (final_unmapped)
1927 *final_unmapped = NULL;
1928
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001929 struct gtphub_peer_port *from_seq = NULL;
1930 struct gtphub_peer_port *from_tei = NULL;
1931 struct gtphub_peer_port *unmapped = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001932 struct gtphub_tunnel *tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001933
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001934 from_seq = gtphub_unmap_seq(p, from);
1935
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001936 if (gtphub_unmap_header_tei(&from_tei, &tun, hub, p, from) < 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001937 return -1;
1938
1939 struct gtphub_peer *from_peer = from->peer_addr->peer;
1940 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001941 LOG(LOGL_DEBUG,
1942 "Seq unmap and TEI unmap yield two different peers."
1943 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001944 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1945 gtphub_plane_idx_names[p->plane_idx],
1946 gtphub_peer_str(from_peer),
1947 (int)p->seq,
1948 gtphub_port_str(from_seq),
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001949 (unsigned int)p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001950 gtphub_port_str2(from_tei)
1951 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001952 }
1953 unmapped = (from_seq? from_seq : from_tei);
1954
1955 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001956 LOG(LOGL_NOTICE,
1957 "Unmap yields a different peer than the configured proxy."
1958 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001959 " unmapped: %s proxy: %s\n",
1960 gtphub_port_str(unmapped),
1961 gtphub_port_str2(to_proxy)
1962 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001963 }
1964 unmapped = (to_proxy? to_proxy : unmapped);
1965
1966 if (!unmapped) {
1967 /* Return no error, but returned pointers are all NULL. */
1968 return 0;
1969 }
1970
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001971 if (unmapped_from_seq)
1972 *unmapped_from_seq = from_seq;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001973 if (unmapped_from_tun)
1974 *unmapped_from_tun = tun;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001975 if (final_unmapped)
1976 *final_unmapped = unmapped;
1977 return 0;
1978}
1979
1980static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1981 uint16_t port,
1982 struct osmo_sockaddr *dst)
1983{
1984 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
1985}
1986
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001987/* If p is an Echo request, replace p's data with the matching response and
1988 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
1989 * detected. */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001990static int gtphub_handle_echo_req(struct gtphub *hub, struct gtp_packet_desc *p,
1991 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001992{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001993 if (p->type != GTP_ECHO_REQ)
1994 return 0;
1995
1996 static uint8_t echo_response_data[14] = {
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001997 0x32, /* GTP v1 flags */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001998 GTP_ECHO_RSP,
1999 0x00, 14 - 8, /* Length in network byte order */
2000 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
2001 0, 0, /* Seq, to be replaced */
2002 0, 0, /* no extensions */
2003 0x0e, /* Recovery IE */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002004 0 /* Restart counter, to be replaced */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002005 };
2006 uint16_t *seq = (uint16_t*)&echo_response_data[8];
2007 uint8_t *recovery = &echo_response_data[13];
2008
2009 *seq = hton16(p->seq);
2010 *recovery = hub->restart_counter;
2011
2012 *reply_buf = echo_response_data;
2013
2014 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002015}
2016
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002017struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2018 const struct osmo_sockaddr *addr);
2019
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002020/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
2021 * address to forward to. Return a pointer to the osmo_fd, but copy the
2022 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
2023 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
2024 * bytes to forward, 0 or less on failure. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002025int gtphub_handle_buf(struct gtphub *hub,
2026 unsigned int side_idx,
2027 unsigned int plane_idx,
2028 const struct osmo_sockaddr *from_addr,
2029 uint8_t *buf,
2030 size_t received,
2031 time_t now,
2032 uint8_t **reply_buf,
2033 struct osmo_fd **to_ofd,
2034 struct osmo_sockaddr *to_addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002035{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002036 struct gtphub_bind *from_bind = &hub->to_gsns[side_idx][plane_idx];
2037 struct gtphub_bind *to_bind = &hub->to_gsns[other_side_idx(side_idx)][plane_idx];
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002038
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002039 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
2040 received);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002041 LOG(LOGL_DEBUG, "%s rx %s from %s %s\n",
2042 (side_idx == GTPH_SIDE_GGSN)? "<-" : "->",
Neels Hofmeyre921e322015-11-11 00:45:50 +01002043 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002044 gtphub_side_idx_names[side_idx],
Neels Hofmeyre921e322015-11-11 00:45:50 +01002045 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002046
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01002047 struct gtp_packet_desc p;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002048 gtp_decode(buf, received, side_idx, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002049
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002050 if (p.rc <= 0) {
2051 LOG(LOGL_ERROR, "INVALID: dropping GTP packet from %s %s %s\n",
2052 gtphub_side_idx_names[side_idx],
2053 gtphub_plane_idx_names[plane_idx],
2054 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002055 return -1;
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002056 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002057
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002058 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2059
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002060 int reply_len;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002061 reply_len = gtphub_handle_echo_req(hub, &p, reply_buf);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002062 if (reply_len > 0) {
2063 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002064 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002065 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002066
2067 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2068 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2069 reply_len);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002070 LOG(LOGL_DEBUG, "%s Echo response to %s: %d bytes to %s\n",
2071 (side_idx == GTPH_SIDE_GGSN)? "-->" : "<--",
2072 gtphub_side_idx_names[side_idx],
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002073 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002074 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002075 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002076 if (reply_len < 0)
2077 return -1;
2078
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002079 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002080
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002081 /* If a proxy is configured, check that it's indeed that proxy talking
2082 * to us. A proxy is a forced 1:1 connection, e.g. to another gtphub,
2083 * so no-one else is allowed to talk to us from that side. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002084 struct gtphub_peer_port *from_peer = hub->proxy[side_idx][plane_idx];
2085 if (from_peer) {
2086 if (osmo_sockaddr_cmp(&from_peer->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002087 LOG(LOGL_ERROR,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002088 "Rejecting: %s proxy configured, but GTP packet"
2089 " received on %s bind is from another sender:"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002090 " proxy: %s sender: %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002091 gtphub_side_idx_names[side_idx],
2092 gtphub_side_idx_names[side_idx],
2093 gtphub_port_str(from_peer),
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002094 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002095 return -1;
2096 }
2097 }
2098
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002099 if (!from_peer) {
2100 /* Find or create a peer with a matching address. The sender's
2101 * port may in fact differ. */
2102 from_peer = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002103 }
2104
2105 /* If any PDP context has been created, we already have an entry for
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002106 * this GSN. If we don't have an entry, a GGSN has nothing to tell us
2107 * about, while an SGSN may initiate a PDP context. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002108 if (!from_peer) {
2109 if (side_idx == GTPH_SIDE_GGSN) {
2110 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
2111 osmo_sockaddr_to_str(from_addr));
2112 return -1;
2113 } else {
2114 /* SGSN */
2115 /* A new peer. If this is on the Ctrl plane, an SGSN
2116 * may make first contact without being known yet, so
2117 * create the peer struct for the current sender. */
2118 if (plane_idx != GTPH_PLANE_CTRL) {
2119 LOG(LOGL_ERROR,
2120 "Dropping packet: User plane peer was not"
2121 "announced by PDP Context: %s\n",
2122 osmo_sockaddr_to_str(from_addr));
2123 return -1;
2124 }
2125
2126 struct gsn_addr from_gsna;
2127 uint16_t from_port;
2128 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
2129 return -1;
2130
2131 from_peer = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
2132 }
2133 }
2134
2135 if (!from_peer) {
2136 /* This could theoretically happen for invalid address data or
2137 * somesuch. */
2138 LOG(LOGL_ERROR, "Dropping packet: invalid %s peer: %s\n",
2139 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002140 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002141 return -1;
2142 }
2143
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002144 LOG(LOGL_DEBUG, "from %s peer: %s\n", gtphub_side_idx_names[side_idx],
2145 gtphub_port_str(from_peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002146
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002147 struct gtphub_peer_port *to_peer_from_seq;
2148 struct gtphub_peer_port *to_peer;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002149 struct gtphub_tunnel *tun;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002150 if (gtphub_unmap(hub, &p, from_peer,
2151 hub->proxy[other_side_idx(side_idx)][plane_idx],
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002152 &to_peer, &to_peer_from_seq, &tun)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002153 != 0) {
2154 return -1;
2155 }
2156
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002157 if ((!to_peer) && (side_idx == GTPH_SIDE_SGSN)) {
2158 if (gtphub_resolve_ggsn(hub, &p, &to_peer) < 0)
2159 return -1;
2160 }
2161
2162 if (!to_peer) {
2163 LOG(LOGL_ERROR, "No %s to send to. Dropping packet.\n",
2164 gtphub_side_idx_names[other_side_idx(side_idx)]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002165 return -1;
2166 }
2167
2168 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002169 /* This may be a Create PDP Context response. If it is, there
2170 * are other addresses in the GTP message to set up apart from
2171 * the sender. */
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002172 if (gtphub_handle_pdp_ctx(hub, &p, tun, from_peer, to_peer)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002173 != 0)
2174 return -1;
2175 }
2176
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002177 gtphub_check_restart_counter(hub, &p, from_peer);
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002178 gtphub_map_restart_counter(hub, &p);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002179
2180 /* If the GGSN is replying to an SGSN request, the sequence nr has
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002181 * already been unmapped above (to_peer_from_seq != NULL), and we need not
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002182 * create a new mapping. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002183 if (!to_peer_from_seq)
2184 gtphub_map_seq(&p, from_peer, to_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002185
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002186 osmo_sockaddr_copy(to_addr, &to_peer->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002187
2188 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01002189
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002190 if (received) {
2191 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2192 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2193 received);
2194 }
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002195 LOG(LOGL_DEBUG, "%s Forward to %s: %d bytes to %s\n",
2196 (side_idx == GTPH_SIDE_SGSN)? "-->" : "<--",
2197 gtphub_side_idx_names[other_side_idx(side_idx)],
Neels Hofmeyre921e322015-11-11 00:45:50 +01002198 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002199 return received;
2200}
2201
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002202static void resolved_gssn_del_cb(struct expiring_item *expi)
2203{
2204 struct gtphub_resolved_ggsn *ggsn;
2205 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
2206
2207 gtphub_port_ref_count_dec(ggsn->peer);
2208 llist_del(&ggsn->entry);
2209
2210 ggsn->expiry_entry.del_cb = 0;
2211 expiring_item_del(&ggsn->expiry_entry);
2212
2213 talloc_free(ggsn);
2214}
2215
2216void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
2217 struct gsn_addr *resolved_addr,
2218 time_t now)
2219{
2220 struct gtphub_peer_port *pp;
2221 struct gtphub_resolved_ggsn *ggsn;
2222
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002223 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002224 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
2225 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01002226
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002227 pp = gtphub_port_have(hub, &hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002228 resolved_addr, 2123);
2229 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002230 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
2231 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002232 return;
2233 }
2234
2235 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
2236 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01002237 INIT_LLIST_HEAD(&ggsn->entry);
2238 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002239
2240 ggsn->peer = pp;
2241 gtphub_port_ref_count_inc(pp);
2242
2243 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01002244 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002245
2246 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002247 expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002248
2249 llist_add(&ggsn->entry, &hub->resolved_ggsns);
2250}
2251
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002252static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
2253{
2254 return pp->ref_count == 0;
2255}
2256
2257static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
2258{
2259 struct gtphub_peer_port *pp, *npp;
2260 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
2261 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002262 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002263 gtphub_port_str(pp));
2264 gtphub_peer_port_del(pp);
2265 }
2266 }
2267 return llist_empty(&pa->ports);
2268}
2269
2270static int gtphub_gc_peer(struct gtphub_peer *p)
2271{
2272 struct gtphub_peer_addr *pa, *npa;
2273 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
2274 if (gtphub_gc_peer_addr(pa)) {
2275 gtphub_peer_addr_del(pa);
2276 }
2277 }
2278
2279 /* Note that there's a ref_count in each gtphub_peer_port instance
2280 * listed within p->addresses, referenced by TEI mappings from
2281 * hub->tei_map. As long as those don't expire, this peer will stay. */
2282
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002283 return llist_empty(&p->addresses)
2284 && nr_map_empty(&p->seq_map);
2285}
2286
2287static void gtphub_gc_bind(struct gtphub_bind *b)
2288{
2289 struct gtphub_peer *p, *n;
2290 llist_for_each_entry_safe(p, n, &b->peers, entry) {
2291 if (gtphub_gc_peer(p)) {
2292 gtphub_peer_del(p);
2293 }
2294 }
2295}
2296
2297void gtphub_gc(struct gtphub *hub, time_t now)
2298{
2299 int expired;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002300 expired = expiry_tick(&hub->expire_quickly, now);
2301 expired += expiry_tick(&hub->expire_slowly, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002302
2303 /* ... */
2304
2305 if (expired) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002306 int s, p;
2307 for_each_side_and_plane(s, p) {
2308 gtphub_gc_bind(&hub->to_gsns[s][p]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002309 }
2310 }
2311}
2312
2313static void gtphub_gc_cb(void *data)
2314{
2315 struct gtphub *hub = data;
2316 gtphub_gc(hub, gtphub_now());
2317 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2318}
2319
2320static void gtphub_gc_start(struct gtphub *hub)
2321{
2322 hub->gc_timer.cb = gtphub_gc_cb;
2323 hub->gc_timer.data = hub;
2324
2325 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2326}
2327
2328/* called by unit tests */
2329void gtphub_init(struct gtphub *hub)
2330{
2331 gtphub_zero(hub);
2332
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002333 INIT_LLIST_HEAD(&hub->tunnels);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002334 INIT_LLIST_HEAD(&hub->pending_deletes);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002335
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002336 expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS);
2337 expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002338
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01002339 nr_pool_init(&hub->tei_pool, 1, 0xffffffff);
2340
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002341 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002342 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002343 for_each_side_and_plane(side_idx, plane_idx) {
2344 gtphub_bind_init(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002345 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002346
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002347 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].label = "SGSN Ctrl";
2348 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].label = "GGSN Ctrl";
2349 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].label = "SGSN User";
2350 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002351}
2352
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002353/* For the test suite, this is kept separate from gtphub_stop(), which also
2354 * closes sockets. The test suite avoids using sockets and would cause
2355 * segfaults when trying to close uninitialized ofds. */
2356void gtphub_free(struct gtphub *hub)
2357{
2358 /* By expiring all mappings, a garbage collection should free
2359 * everything else. A gtphub_bind_free() will assert that everything is
2360 * indeed empty. */
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002361 expiry_clear(&hub->expire_quickly);
2362 expiry_clear(&hub->expire_slowly);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002363
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002364 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002365 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002366 for_each_side_and_plane(side_idx, plane_idx) {
2367 gtphub_gc_bind(&hub->to_gsns[side_idx][plane_idx]);
2368 gtphub_bind_free(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002369 }
2370}
2371
2372void gtphub_stop(struct gtphub *hub)
2373{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002374 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002375 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002376 for_each_side_and_plane(side_idx, plane_idx) {
2377 gtphub_bind_stop(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002378 }
2379 gtphub_free(hub);
2380}
2381
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002382static int gtphub_make_proxy(struct gtphub *hub,
2383 struct gtphub_peer_port **pp,
2384 struct gtphub_bind *bind,
2385 const struct gtphub_cfg_addr *addr)
2386{
2387 if (!addr->addr_str)
2388 return 0;
2389
2390 struct gsn_addr gsna;
2391 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
2392 return -1;
2393
2394 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
2395
2396 /* This is *the* proxy. Make sure it is never expired. */
2397 gtphub_port_ref_count_inc(*pp);
2398 return 0;
2399}
2400
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002401int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
2402 uint8_t restart_counter)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002403{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002404 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002405
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002406 hub->restart_counter = restart_counter;
2407
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002408 /* If a Ctrl plane proxy is configured, ares will never be used. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002409 if (!cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str) {
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002410 if (gtphub_ares_init(hub) != 0) {
2411 LOG(LOGL_FATAL, "Failed to initialize ares\n");
2412 return -1;
2413 }
2414 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002415
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002416 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002417 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002418 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01002419 int rc;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002420 rc = gtphub_bind_start(&hub->to_gsns[side_idx][plane_idx],
2421 &cfg->to_gsns[side_idx][plane_idx],
2422 (side_idx == GTPH_SIDE_SGSN)
2423 ? from_sgsns_read_cb
2424 : from_ggsns_read_cb,
2425 hub, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002426 if (rc) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002427 LOG(LOGL_FATAL, "Failed to bind for %ss (%s)\n",
2428 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002429 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002430 return rc;
2431 }
2432 }
2433
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002434 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002435 if (gtphub_make_proxy(hub,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002436 &hub->proxy[side_idx][plane_idx],
2437 &hub->to_gsns[side_idx][plane_idx],
2438 &cfg->proxy[side_idx][plane_idx])
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002439 != 0) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002440 LOG(LOGL_FATAL, "Cannot configure %s proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002441 " %s port %d.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002442 gtphub_side_idx_names[side_idx],
2443 cfg->proxy[side_idx][plane_idx].addr_str,
2444 (int)cfg->proxy[side_idx][plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002445 return -1;
2446 }
2447 }
2448
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002449 for_each_side_and_plane(side_idx, plane_idx) {
2450 if (hub->proxy[side_idx][plane_idx])
2451 LOG(LOGL_NOTICE, "Using %s %s proxy %s\n",
2452 gtphub_side_idx_names[side_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002453 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002454 gtphub_port_str(hub->proxy[side_idx][plane_idx]));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002455 }
2456
2457 gtphub_gc_start(hub);
2458 return 0;
2459}
2460
2461static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
2462 const struct gsn_addr *addr)
2463{
2464 struct gtphub_peer_addr *a;
2465 llist_for_each_entry(a, &peer->addresses, entry) {
2466 if (gsn_addr_same(&a->addr, addr))
2467 return a;
2468 }
2469 return NULL;
2470}
2471
2472static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2473 uint16_t port)
2474{
2475 OSMO_ASSERT(port);
2476 struct gtphub_peer_port *pp;
2477 llist_for_each_entry(pp, &a->ports, entry) {
2478 if (pp->port == port)
2479 return pp;
2480 }
2481 return NULL;
2482}
2483
2484static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2485 const struct gsn_addr *addr)
2486{
2487 struct gtphub_peer *peer;
2488 llist_for_each_entry(peer, &bind->peers, entry) {
2489 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2490 if (a)
2491 return a;
2492 }
2493 return NULL;
2494}
2495
2496static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2497 const struct gsn_addr *addr,
2498 uint16_t port)
2499{
2500 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2501 if (!a)
2502 return NULL;
2503 return gtphub_addr_find_port(a, port);
2504}
2505
2506struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2507 const struct osmo_sockaddr *addr)
2508{
2509 struct gsn_addr gsna;
2510 uint16_t port;
2511 gsn_addr_from_sockaddr(&gsna, &port, addr);
2512 return gtphub_port_find(bind, &gsna, port);
2513}
2514
2515static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2516 struct gtphub_bind *bind)
2517{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002518 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2519 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002520 OSMO_ASSERT(peer);
2521
2522 INIT_LLIST_HEAD(&peer->addresses);
2523
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002524 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002525 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002526
2527 /* TODO use something random to pick the initial sequence nr.
2528 0x6d31 produces the ASCII character sequence 'm1', currently used in
2529 gtphub_nc_test.sh. */
2530 peer->seq_pool.last_nr = 0x6d31 - 1;
2531
2532 llist_add(&peer->entry, &bind->peers);
2533 return peer;
2534}
2535
2536static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2537 const struct gsn_addr *addr)
2538{
2539 struct gtphub_peer_addr *a;
2540 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2541 OSMO_ASSERT(a);
2542 a->peer = peer;
2543 gsn_addr_copy(&a->addr, addr);
2544 INIT_LLIST_HEAD(&a->ports);
2545 llist_add(&a->entry, &peer->addresses);
2546
2547 return a;
2548}
2549
2550static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2551 struct gtphub_bind *bind,
2552 const struct gsn_addr *addr)
2553{
2554 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2555 if (a)
2556 return a;
2557
2558 /* If we haven't found an address, that means we need to create an
2559 * entirely new peer for the new address. More addresses may be added
2560 * to this peer later, but not via this function. */
2561 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002562
2563 a = gtphub_peer_add_addr(peer, addr);
2564
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002565 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002566 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002567 gsn_addr_to_str(&a->addr));
2568
2569 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002570}
2571
2572static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2573 uint16_t port)
2574{
2575 struct gtphub_peer_port *pp;
2576
2577 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2578 OSMO_ASSERT(pp);
2579 pp->peer_addr = a;
2580 pp->port = port;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002581 pp->last_restart_count = -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002582
2583 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2584 talloc_free(pp);
2585 return NULL;
2586 }
2587
2588 llist_add(&pp->entry, &a->ports);
2589
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002590 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002591 gsn_addr_to_str(&a->addr),
2592 (int)port);
2593
2594 return pp;
2595}
2596
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002597struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2598 struct gtphub_bind *bind,
2599 const struct gsn_addr *addr,
2600 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002601{
2602 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2603
2604 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2605 if (pp)
2606 return pp;
2607
2608 return gtphub_addr_add_port(a, port);
2609}
2610
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002611/* Find a GGSN peer with a matching address. If the address is known but the
2612 * port not, create a new port for that peer address. */
2613struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2614 const struct osmo_sockaddr *addr)
2615{
2616 struct gtphub_peer_addr *pa;
2617 struct gtphub_peer_port *pp;
2618
2619 struct gsn_addr gsna;
2620 uint16_t port;
2621 gsn_addr_from_sockaddr(&gsna, &port, addr);
2622
2623 pa = gtphub_addr_find(bind, &gsna);
2624 if (!pa)
2625 return NULL;
2626
2627 pp = gtphub_addr_find_port(pa, port);
2628
2629 if (!pp)
2630 pp = gtphub_addr_add_port(pa, port);
2631
2632 return pp;
2633}
2634
2635
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002636/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2637 * resolution should be possible but failed, and 1 if resolution was
2638 * successful. *pp will be set to NULL if <1 is returned. */
2639static int gtphub_resolve_ggsn(struct gtphub *hub,
2640 struct gtp_packet_desc *p,
2641 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002642{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002643 *pp = NULL;
2644
2645 /* TODO determine from message type whether IEs should be present? */
2646
2647 int rc;
2648 const char *imsi_str;
2649 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2650 if (rc < 1)
2651 return rc;
2652 OSMO_ASSERT(imsi_str);
2653
2654 const char *apn_str;
2655 rc = get_ie_apn_str(p->ie, &apn_str);
2656 if (rc < 1)
2657 return rc;
2658 OSMO_ASSERT(apn_str);
2659
2660 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2661 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002662}
2663
2664
2665/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002666/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002667/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2668 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002669static int _osmo_getaddrinfo(struct addrinfo **result,
2670 uint16_t family, uint16_t type, uint8_t proto,
2671 const char *host, uint16_t port)
2672{
2673 struct addrinfo hints;
2674 char portbuf[16];
2675
2676 sprintf(portbuf, "%u", port);
2677 memset(&hints, '\0', sizeof(struct addrinfo));
2678 hints.ai_family = family;
2679 if (type == SOCK_RAW) {
2680 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2681 * SOCK_RAW and IPPROTO_GRE is used.
2682 */
2683 hints.ai_socktype = SOCK_DGRAM;
2684 hints.ai_protocol = IPPROTO_UDP;
2685 } else {
2686 hints.ai_socktype = type;
2687 hints.ai_protocol = proto;
2688 }
2689
2690 return getaddrinfo(host, portbuf, &hints, result);
2691}
2692
2693/* TODO move to osmocom/core/socket.c ? */
2694int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2695 uint16_t family, uint16_t type, uint8_t proto,
2696 const char *host, uint16_t port)
2697{
2698 struct addrinfo *res;
2699 int rc;
2700 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2701
2702 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002703 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002704 return -EINVAL;
2705 }
2706
2707 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2708 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2709 addr->l = res->ai_addrlen;
2710 freeaddrinfo(res);
2711
2712 return 0;
2713}
2714
2715int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2716 char *port_str, size_t port_str_len,
2717 const struct osmo_sockaddr *addr,
2718 int flags)
2719{
2720 int rc;
2721
2722 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2723 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2724 return -1;
2725 }
2726
2727 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002728 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2729 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002730 return -1;
2731 }
2732
2733 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2734 addr_str, addr_str_len,
2735 port_str, port_str_len,
2736 flags);
2737
2738 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002739 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2740 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2741 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002742
2743 return rc;
2744}
2745
2746const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2747 char *buf, size_t buf_len)
2748{
2749 const int portbuf_len = 6;
2750 OSMO_ASSERT(buf_len > portbuf_len);
2751 char *portbuf = buf + buf_len - portbuf_len;
2752 buf_len -= portbuf_len;
2753 if (osmo_sockaddr_to_strs(buf, buf_len,
2754 portbuf, portbuf_len,
2755 addr,
2756 NI_NUMERICHOST | NI_NUMERICSERV))
2757 return NULL;
2758
2759 char *pos = buf + strnlen(buf, buf_len-1);
2760 size_t len = buf_len - (pos - buf);
2761
2762 snprintf(pos, len, " port %s", portbuf);
2763 buf[buf_len-1] = '\0';
2764
2765 return buf;
2766}
2767
2768const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2769{
2770 static char buf[256];
2771 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2772 if (! result)
2773 return "(invalid)";
2774 return result;
2775}
2776
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002777int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2778 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002779{
2780 if (a == b)
2781 return 0;
2782 if (!a)
2783 return -1;
2784 if (!b)
2785 return 1;
2786 if (a->l != b->l) {
2787 /* Lengths are not the same, but determine the order. Will
2788 * anyone ever sort a list by osmo_sockaddr though...? */
2789 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2790 if (cmp == 0) {
2791 if (a->l < b->l)
2792 return -1;
2793 else
2794 return 1;
2795 }
2796 return cmp;
2797 }
2798 return memcmp(&a->a, &b->a, a->l);
2799}
2800
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002801void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2802 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002803{
2804 OSMO_ASSERT(src->l <= sizeof(dst->a));
2805 memcpy(&dst->a, &src->a, src->l);
2806 dst->l = src->l;
2807}