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