blob: 132808a1631d48eb10931736c75d717461c4bd11 [file] [log] [blame]
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001/* GTP Hub Implementation */
2
3/* (C) 2015 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <string.h>
23#include <errno.h>
24#include <inttypes.h>
25#include <time.h>
26#include <limits.h>
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +010027#include <unistd.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020028#include <sys/socket.h>
29#include <netinet/in.h>
30#include <arpa/inet.h>
31
32#include <gtp.h>
33#include <gtpie.h>
34
35#include <openbsc/gtphub.h>
36#include <openbsc/debug.h>
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010037#include <openbsc/gprs_utils.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020038
39#include <osmocom/core/utils.h>
40#include <osmocom/core/logging.h>
41#include <osmocom/core/socket.h>
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +010042#include <osmocom/core/rate_ctr.h>
43#include <osmocom/core/stats.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020044
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010045
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020046static const int GTPH_GC_TICK_SECONDS = 1;
47
48void *osmo_gtphub_ctx;
49
Neels Hofmeyr063a8022015-11-16 14:35:13 +010050/* Convenience makro, note: only within this C file. */
51#define LOG(level, fmt, args...) \
52 LOGP(DGTPHUB, level, fmt, ##args)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020053
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010054#define ZERO_STRUCT(struct_pointer) memset(struct_pointer, '\0', \
55 sizeof(*(struct_pointer)))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020056
57/* TODO move this to osmocom/core/select.h ? */
58typedef int (*osmo_fd_cb_t)(struct osmo_fd *fd, unsigned int what);
59
60/* TODO move this to osmocom/core/linuxlist.h ? */
61#define __llist_first(head) (((head)->next == (head)) ? NULL : (head)->next)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010062#define llist_first(head, type, entry) \
63 llist_entry(__llist_first(head), type, entry)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020064
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +010065#define __llist_last(head) (((head)->next == (head)) ? NULL : (head)->prev)
66#define llist_last(head, type, entry) \
67 llist_entry(__llist_last(head), type, entry)
68
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020069/* TODO move GTP header stuff to openggsn/gtp/ ? See gtp_decaps*() */
70
71enum gtp_rc {
72 GTP_RC_UNKNOWN = 0,
73 GTP_RC_TINY = 1, /* no IEs (like ping/pong) */
Neels Hofmeyre921e322015-11-11 00:45:50 +010074 GTP_RC_PDU_C = 2, /* a real packet with IEs */
75 GTP_RC_PDU_U = 3, /* a real packet with User data */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020076
77 GTP_RC_TOOSHORT = -1,
78 GTP_RC_UNSUPPORTED_VERSION = -2,
79 GTP_RC_INVALID_IE = -3,
80};
81
82struct gtp_packet_desc {
83 union gtp_packet *data;
84 int data_len;
85 int header_len;
86 int version;
87 uint8_t type;
88 uint16_t seq;
Neels Hofmeyre54cd152015-11-24 13:31:06 +010089 uint32_t header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020090 uint32_t header_tei;
91 int rc; /* enum gtp_rc */
92 unsigned int plane_idx;
Neels Hofmeyre54cd152015-11-24 13:31:06 +010093 unsigned int side_idx;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +010094 time_t timestamp;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020095 union gtpie_member *ie[GTPIE_SIZE];
96};
97
Neels Hofmeyr10fc0242015-12-01 00:23:45 +010098struct pending_delete {
99 struct llist_head entry;
100 struct expiring_item expiry_entry;
101
102 struct gtphub_tunnel *tun;
103 uint8_t teardown_ind;
104 uint8_t nsapi;
105};
106
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100107
108/* counters */
109
110enum gtphub_counters_io {
111 GTPH_CTR_PKTS_IN = 0,
112 GTPH_CTR_PKTS_OUT,
113 GTPH_CTR_BYTES_IN,
114 GTPH_CTR_BYTES_OUT
115};
116
117static const struct rate_ctr_desc gtphub_counters_io_desc[] = {
118 { "packets.in", "Packets ( In)" },
119 { "packets.out", "Packets (Out)" },
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100120 { "bytes.in", "Bytes ( In)" },
121 { "bytes.out", "Bytes (Out)" },
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100122};
123
124static const struct rate_ctr_group_desc gtphub_ctrg_io_desc = {
125 .group_name_prefix = "gtphub.bind",
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100126 .group_description = "I/O Statistics",
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100127 .num_ctr = ARRAY_SIZE(gtphub_counters_io_desc),
128 .ctr_desc = gtphub_counters_io_desc,
129 .class_id = OSMO_STATS_CLASS_GLOBAL,
130};
131
132
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200133void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
134{
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +0100135 *gsna = *src;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200136}
137
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200138int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
139 const struct osmo_sockaddr *sa)
140{
141 char addr_str[256];
142 char port_str[6];
143
144 if (osmo_sockaddr_to_strs(addr_str, sizeof(addr_str),
145 port_str, sizeof(port_str),
146 sa, (NI_NUMERICHOST | NI_NUMERICSERV))
147 != 0) {
148 return -1;
149 }
150
151 if (port)
152 *port = atoi(port_str);
153
154 return gsn_addr_from_str(gsna, addr_str);
155}
156
157int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
158{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100159 if ((!gsna) || (!numeric_addr_str))
160 return -1;
161
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200162 int af = AF_INET;
163 gsna->len = 4;
164 const char *pos = numeric_addr_str;
165 for (; *pos; pos++) {
166 if (*pos == ':') {
167 af = AF_INET6;
168 gsna->len = 16;
169 break;
170 }
171 }
172
173 int rc = inet_pton(af, numeric_addr_str, gsna->buf);
174 if (rc != 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100175 LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n",
176 numeric_addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200177 return -1;
178 }
179 return 0;
180}
181
182const char *gsn_addr_to_str(const struct gsn_addr *gsna)
183{
184 static char buf[INET6_ADDRSTRLEN + 1];
185 return gsn_addr_to_strb(gsna, buf, sizeof(buf));
186}
187
188const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
189 char *strbuf,
190 int strbuf_len)
191{
192 int af;
193 switch (gsna->len) {
194 case 4:
195 af = AF_INET;
196 break;
197 case 16:
198 af = AF_INET6;
199 break;
200 default:
201 return NULL;
202 }
203
204 const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
205 if (!r) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100206 LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:"
207 " %s: len=%d, buf=%s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100208 strerror(errno),
209 (int)gsna->len,
210 osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200211 }
212 return r;
213}
214
215int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
216{
217 if (a == b)
218 return 1;
219 if ((!a) || (!b))
220 return 0;
221 if (a->len != b->len)
222 return 0;
223 return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
224}
225
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100226static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p,
227 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200228{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100229 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200230 return -1;
231
232 unsigned int len;
233 /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
234 if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
235 &len, gsna->buf, sizeof(gsna->buf))
236 != 0)
237 return -1;
238 gsna->len = len;
239 return 0;
240}
241
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100242static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p,
243 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200244{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100245 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200246 return -1;
247
248 int ie_idx;
249 ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx);
250
251 if (ie_idx < 0)
252 return -1;
253
254 struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
255 int ie_l = ntoh16(ie->l);
256 if (ie_l != gsna->len) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100257 LOG(LOGL_ERROR, "Not implemented:"
258 " replace an IE address of different size:"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200259 " replace %d with %d\n", (int)ie_l, (int)gsna->len);
260 return -1;
261 }
262
263 memcpy(ie->v, gsna->buf, (int)ie_l);
264 return 0;
265}
266
267/* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there.
268 */
269void validate_gtp0_header(struct gtp_packet_desc *p)
270{
271 const struct gtp0_header *pheader = &(p->data->gtp0.h);
272 p->rc = GTP_RC_UNKNOWN;
273 p->header_len = 0;
274
275 OSMO_ASSERT(p->data_len >= 1);
276 OSMO_ASSERT(p->version == 0);
277
278 if (p->data_len < GTP0_HEADER_SIZE) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100279 LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200280 p->rc = GTP_RC_TOOSHORT;
281 return;
282 }
283
284 p->type = ntoh8(pheader->type);
285 p->seq = ntoh16(pheader->seq);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100286 p->header_tei_rx = 0; /* TODO */
287 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200288
289 if (p->data_len == GTP0_HEADER_SIZE) {
290 p->rc = GTP_RC_TINY;
291 p->header_len = GTP0_HEADER_SIZE;
292 return;
293 }
294
295 /* Check packet length field versus length of packet */
296 if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100297 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
298 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100299 GTP0_HEADER_SIZE, (int)ntoh16(pheader->length),
300 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200301 p->rc = GTP_RC_TOOSHORT;
302 return;
303 }
304
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100305 LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200306 p->header_len = GTP0_HEADER_SIZE;
Neels Hofmeyre921e322015-11-11 00:45:50 +0100307 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200308}
309
310/* Validate GTP version 1 data, and update p->rc with the result, as well as
311 * p->header_len in case of a valid header. */
312void validate_gtp1_header(struct gtp_packet_desc *p)
313{
314 const struct gtp1_header_long *pheader = &(p->data->gtp1l.h);
315 p->rc = GTP_RC_UNKNOWN;
316 p->header_len = 0;
317
318 OSMO_ASSERT(p->data_len >= 1);
319 OSMO_ASSERT(p->version == 1);
320
321 if ((p->data_len < GTP1_HEADER_SIZE_LONG)
322 && (p->data_len != GTP1_HEADER_SIZE_SHORT)){
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100323 LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200324 p->rc = GTP_RC_TOOSHORT;
325 return;
326 }
327
328 p->type = ntoh8(pheader->type);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100329 p->header_tei_rx = ntoh32(pheader->tei);
330 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200331 p->seq = ntoh16(pheader->seq);
332
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100333 LOG(LOGL_DEBUG, "GTPv1\n"
334 "| type = %" PRIu8 " 0x%02" PRIx8 "\n"
335 "| length = %" PRIu16 " 0x%04" PRIx16 "\n"
336 "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n"
337 "| seq = %" PRIu16 " 0x%04" PRIx16 "\n"
338 "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n"
339 "| next = %" PRIu8 " 0x%02" PRIx8 "\n",
340 p->type, p->type,
341 ntoh16(pheader->length), ntoh16(pheader->length),
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100342 p->header_tei_rx, p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100343 p->seq, p->seq,
344 pheader->npdu, pheader->npdu,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200345 pheader->next, pheader->next);
346
347 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
348 p->rc = GTP_RC_TINY;
349 p->header_len = GTP1_HEADER_SIZE_SHORT;
350 return;
351 }
352
353 /* Check packet length field versus length of packet */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100354 int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT;
355 if (p->data_len != announced_len) {
356 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
357 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100358 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
359 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200360 p->rc = GTP_RC_TOOSHORT;
361 return;
362 }
363
Neels Hofmeyre921e322015-11-11 00:45:50 +0100364 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200365 p->header_len = GTP1_HEADER_SIZE_LONG;
366}
367
368/* Examine whether p->data of size p->data_len has a valid GTP header. Set
369 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
370 * gtp_rc). p->data must point at a buffer with p->data_len set. */
371void validate_gtp_header(struct gtp_packet_desc *p)
372{
373 p->rc = GTP_RC_UNKNOWN;
374
375 /* Need at least 1 byte in order to check version */
376 if (p->data_len < 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100377 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n",
378 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200379 p->rc = GTP_RC_TOOSHORT;
380 return;
381 }
382
383 p->version = p->data->flags >> 5;
384
385 switch (p->version) {
386 case 0:
387 validate_gtp0_header(p);
388 break;
389 case 1:
390 validate_gtp1_header(p);
391 break;
392 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100393 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200394 p->rc = GTP_RC_UNSUPPORTED_VERSION;
395 break;
396 }
397}
398
399
400/* Return the value of the i'th IMSI IEI by copying to *imsi.
401 * The first IEI is reached by passing i = 0.
402 * imsi must point at allocated space of (at least) 8 bytes.
403 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100404static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200405{
406 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
407}
408
409/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100410static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200411{
412 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
413}
414
415static char imsi_digit_to_char(uint8_t nibble)
416{
417 nibble &= 0x0f;
418 if (nibble > 9)
419 return (nibble == 0x0f) ? '\0' : '?';
420 return '0' + nibble;
421}
422
423/* Return a human readable IMSI string, in a static buffer.
424 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100425static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200426{
427 static char str[17];
428 int i;
429
430 for (i = 0; i < 8; i++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +0100431 char c;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100432 c = imsi_digit_to_char(imsi[i]);
433 if (c == '?')
434 return -1;
435 str[2*i] = c;
436
437 c = imsi_digit_to_char(imsi[i] >> 4);
438 if (c == '?')
439 return -1;
440 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200441 }
442 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100443 *imsi_str = str;
444 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200445}
446
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100447/* Return 0 if not present, 1 if present and decoded successfully, -1 if
448 * present but cannot be decoded. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100449static int get_ie_imsi_str(union gtpie_member *ie[], int i,
450 const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100451{
452 uint8_t imsi_buf[8];
453 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100454 return 0;
455 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100456}
457
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100458/* Return 0 if not present, 1 if present and decoded successfully, -1 if
459 * present but cannot be decoded. */
460static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100461{
462 static char apn_buf[GSM_APN_LENGTH];
463 unsigned int len;
464 if (gtpie_gettlv(ie, GTPIE_APN, 0,
465 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100466 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100467
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100468 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100469 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
470 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100471 return -1;
472 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100473
474 if (len > (sizeof(apn_buf) - 1))
475 len = sizeof(apn_buf) - 1;
476 apn_buf[len] = '\0';
477
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100478 *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
479 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100480 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
481 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100482 return -1;
483 }
484 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100485}
486
487
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200488/* Validate header, and index information elements. Write decoded packet
489 * information to *res. res->data will point at the given data buffer. On
490 * error, p->rc is set <= 0 (see enum gtp_rc). */
491static void gtp_decode(const uint8_t *data, int data_len,
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100492 unsigned int from_side_idx,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200493 unsigned int from_plane_idx,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100494 struct gtp_packet_desc *res,
495 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200496{
497 ZERO_STRUCT(res);
498 res->data = (union gtp_packet*)data;
499 res->data_len = data_len;
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100500 res->side_idx = from_side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200501 res->plane_idx = from_plane_idx;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100502 res->timestamp = now;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200503
504 validate_gtp_header(res);
505
Neels Hofmeyr87c83d02015-12-03 11:25:27 +0100506 if (res->rc <= 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200507 return;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200508
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100509 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200510
Neels Hofmeyre921e322015-11-11 00:45:50 +0100511 if (from_plane_idx == GTPH_PLANE_USER) {
512 res->rc = GTP_RC_PDU_U;
513 return;
514 }
515
516 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100517 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200518 return;
519 }
520
521 if (gtpie_decaps(res->ie, res->version,
522 (void*)(data + res->header_len),
523 res->data_len - res->header_len) != 0) {
524 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100525 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
526 " Dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200527 return;
528 }
529
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100530#if 1
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100531 /* TODO if (<loglevel is debug>) { ...
532 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200533 int i;
534
535 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100536 const char *imsi;
537 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200538 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100539 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200540 }
541
542 for (i = 0; i < 10; i++) {
543 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100544 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200545 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100546 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200547 }
548
549 for (i = 0; i < 2; i++) {
550 struct gsn_addr addr;
551 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100552 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200553 }
554
555 for (i = 0; i < 10; i++) {
556 uint32_t tei;
557 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
558 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100559 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200560 tei, tei);
561 }
562
563 for (i = 0; i < 10; i++) {
564 uint32_t tei;
565 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
566 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100567 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200568 tei, tei);
569 }
570#endif
571}
572
573
574/* expiry */
575
576void expiry_init(struct expiry *exq, int expiry_in_seconds)
577{
578 ZERO_STRUCT(exq);
579 exq->expiry_in_seconds = expiry_in_seconds;
580 INIT_LLIST_HEAD(&exq->items);
581}
582
583void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
584{
585 item->expiry = now + exq->expiry_in_seconds;
586
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +0100587 OSMO_ASSERT(llist_empty(&exq->items)
588 || (item->expiry
589 >= llist_last(&exq->items, struct expiring_item, entry)->expiry));
590
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200591 /* Add/move to the tail to always sort by expiry, ascending. */
592 llist_del(&item->entry);
593 llist_add_tail(&item->entry, &exq->items);
594}
595
596int expiry_tick(struct expiry *exq, time_t now)
597{
598 int expired = 0;
599 struct expiring_item *m, *n;
600 llist_for_each_entry_safe(m, n, &exq->items, entry) {
601 if (m->expiry <= now) {
602 expiring_item_del(m);
603 expired ++;
604 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200605 /* The items are added sorted by expiry. So when we hit
606 * an unexpired entry, only more unexpired ones will
607 * follow. */
608 break;
609 }
610 }
611 return expired;
612}
613
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100614void expiry_clear(struct expiry *exq)
615{
616 struct expiring_item *m, *n;
617 llist_for_each_entry_safe(m, n, &exq->items, entry) {
618 expiring_item_del(m);
619 }
620}
621
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200622void expiring_item_init(struct expiring_item *item)
623{
624 ZERO_STRUCT(item);
625 INIT_LLIST_HEAD(&item->entry);
626}
627
628void expiring_item_del(struct expiring_item *item)
629{
630 OSMO_ASSERT(item);
631 llist_del(&item->entry);
632 INIT_LLIST_HEAD(&item->entry);
633 if (item->del_cb) {
634 /* avoid loops */
635 del_cb_t del_cb = item->del_cb;
636 item->del_cb = 0;
637 (del_cb)(item);
638 }
639}
640
641
642/* nr_map, nr_pool */
643
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100644void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200645{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100646 *pool = (struct nr_pool){
647 .nr_min = nr_min,
648 .nr_max = nr_max,
649 .last_nr = nr_max
650 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200651}
652
653nr_t nr_pool_next(struct nr_pool *pool)
654{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100655 if (pool->last_nr >= pool->nr_max)
656 pool->last_nr = pool->nr_min;
657 else
658 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200659
660 return pool->last_nr;
661}
662
663void nr_map_init(struct nr_map *map, struct nr_pool *pool,
664 struct expiry *exq)
665{
666 ZERO_STRUCT(map);
667 map->pool = pool;
668 map->add_items_to_expiry = exq;
669 INIT_LLIST_HEAD(&map->mappings);
670}
671
672void nr_mapping_init(struct nr_mapping *m)
673{
674 ZERO_STRUCT(m);
675 INIT_LLIST_HEAD(&m->entry);
676 expiring_item_init(&m->expiry_entry);
677}
678
679void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
680{
681 /* Generate a mapped number */
682 mapping->repl = nr_pool_next(map->pool);
683
684 /* Add to the tail to always yield a list sorted by expiry, in
685 * ascending order. */
686 llist_add_tail(&mapping->entry, &map->mappings);
Neels Hofmeyr508514c2015-11-24 13:30:38 +0100687 nr_map_refresh(map, mapping, now);
688}
689
690void nr_map_refresh(struct nr_map *map, struct nr_mapping *mapping, time_t now)
691{
692 if (!map->add_items_to_expiry)
693 return;
694 expiry_add(map->add_items_to_expiry,
695 &mapping->expiry_entry,
696 now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200697}
698
699void nr_map_clear(struct nr_map *map)
700{
701 struct nr_mapping *m;
702 struct nr_mapping *n;
703 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
704 nr_mapping_del(m);
705 }
706}
707
708int nr_map_empty(const struct nr_map *map)
709{
710 return llist_empty(&map->mappings);
711}
712
713struct nr_mapping *nr_map_get(const struct nr_map *map,
714 void *origin, nr_t nr_orig)
715{
716 struct nr_mapping *mapping;
717 llist_for_each_entry(mapping, &map->mappings, entry) {
718 if ((mapping->origin == origin)
719 && (mapping->orig == nr_orig))
720 return mapping;
721 }
722 /* Not found. */
723 return NULL;
724}
725
726struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
727{
728 struct nr_mapping *mapping;
729 llist_for_each_entry(mapping, &map->mappings, entry) {
730 if (mapping->repl == nr_repl) {
731 return mapping;
732 }
733 }
734 /* Not found. */
735 return NULL;
736}
737
738void nr_mapping_del(struct nr_mapping *mapping)
739{
740 OSMO_ASSERT(mapping);
741 llist_del(&mapping->entry);
742 INIT_LLIST_HEAD(&mapping->entry);
743 expiring_item_del(&mapping->expiry_entry);
744}
745
746
747/* gtphub */
748
749const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
750 "CTRL",
751 "USER",
752};
753
754const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
755 2123,
756 2152,
757};
758
Neels Hofmeyra9905a52015-11-29 23:49:48 +0100759const char* const gtphub_side_idx_names[GTPH_SIDE_N] = {
760 "SGSN",
761 "GGSN",
762};
763
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200764time_t gtphub_now(void)
765{
766 struct timespec now_tp;
767 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
768 return now_tp.tv_sec;
769}
770
771/* Remove a gtphub_peer from its list and free it. */
772static void gtphub_peer_del(struct gtphub_peer *peer)
773{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100774 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200775 nr_map_clear(&peer->seq_map);
776 llist_del(&peer->entry);
777 talloc_free(peer);
778}
779
780static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
781{
782 OSMO_ASSERT(llist_empty(&pa->ports));
783 llist_del(&pa->entry);
784 talloc_free(pa);
785}
786
787static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
788{
789 OSMO_ASSERT(pp->ref_count == 0);
790 llist_del(&pp->entry);
791 talloc_free(pp);
792}
793
794/* From the information in the gtp_packet_desc, return the address of a GGSN.
795 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100796static int gtphub_resolve_ggsn(struct gtphub *hub,
797 struct gtp_packet_desc *p,
798 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200799
800/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100801struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
802 const char *imsi_str,
803 const char *apn_ni_str);
804int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200805
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200806static void gtphub_zero(struct gtphub *hub)
807{
808 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100809 INIT_LLIST_HEAD(&hub->ggsn_lookups);
810 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200811}
812
813static int gtphub_sock_init(struct osmo_fd *ofd,
814 const struct gtphub_cfg_addr *addr,
815 osmo_fd_cb_t cb,
816 void *data,
817 int ofd_id)
818{
819 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100820 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200821 return -1;
822 }
823 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100824 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200825 return -1;
826 }
827
828 ofd->when = BSC_FD_READ;
829 ofd->cb = cb;
830 ofd->data = data;
831 ofd->priv_nr = ofd_id;
832
833 int rc;
834 rc = osmo_sock_init_ofd(ofd,
835 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
836 addr->addr_str, addr->port,
837 OSMO_SOCK_F_BIND);
838 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100839 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
840 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200841 return -1;
842 }
843
844 return 0;
845}
846
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100847static void gtphub_sock_close(struct osmo_fd *ofd)
848{
849 close(ofd->fd);
850 osmo_fd_unregister(ofd);
851 ofd->cb = NULL;
852}
853
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200854static void gtphub_bind_init(struct gtphub_bind *b)
855{
856 ZERO_STRUCT(b);
857
858 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100859
860 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
861 &gtphub_ctrg_io_desc, 0);
862 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200863}
864
865static int gtphub_bind_start(struct gtphub_bind *b,
866 const struct gtphub_cfg_bind *cfg,
867 osmo_fd_cb_t cb, void *cb_data,
868 unsigned int ofd_id)
869{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100870 LOG(LOGL_DEBUG, "Starting bind %s\n", b->label);
871 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0) {
872 LOG(LOGL_FATAL, "Invalid bind address for %s: %s\n",
873 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200874 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100875 }
876 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0) {
877 LOG(LOGL_FATAL, "Cannot bind for %s: %s\n",
878 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200879 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100880 }
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100881 b->local_port = cfg->bind.port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200882 return 0;
883}
884
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100885static void gtphub_bind_free(struct gtphub_bind *b)
886{
887 OSMO_ASSERT(llist_empty(&b->peers));
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100888 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100889}
890
891static void gtphub_bind_stop(struct gtphub_bind *b) {
892 gtphub_sock_close(&b->ofd);
893 gtphub_bind_free(b);
894}
895
Neels Hofmeyr40348972015-11-17 12:22:28 +0100896/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200897 * Return the number of bytes read, zero on error. */
898static int gtphub_read(const struct osmo_fd *from,
899 struct osmo_sockaddr *from_addr,
900 uint8_t *buf, size_t buf_len)
901{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100902 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200903
Neels Hofmeyr40348972015-11-17 12:22:28 +0100904 /* recvfrom requires the available length set in *from_addr_len. */
905 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200906 errno = 0;
907 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100908 (struct sockaddr*)&from_addr->a,
909 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200910 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
911 * is not truncated. Then maybe reduce buf's size. */
912
913 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100914 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
915 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200916 return 0;
917 }
918
Neels Hofmeyr40348972015-11-17 12:22:28 +0100919 LOG(LOGL_DEBUG, "Received %d bytes from %s\n%s\n",
920 (int)received, osmo_sockaddr_to_str(from_addr),
921 osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200922
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200923 return received;
924}
925
926inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
927{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100928 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200929 OSMO_ASSERT(pp->ref_count < UINT_MAX);
930 pp->ref_count++;
931}
932
933inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
934{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100935 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200936 OSMO_ASSERT(pp->ref_count > 0);
937 pp->ref_count--;
938}
939
940inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
941{
942 OSMO_ASSERT(p->version == 1);
943 p->data->gtp1l.h.seq = hton16(seq);
944 p->seq = seq;
945}
946
947inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
948{
949 OSMO_ASSERT(p->version == 1);
950 p->data->gtp1l.h.tei = hton32(tei);
951 p->header_tei = tei;
952}
953
954static void gtphub_mapping_del_cb(struct expiring_item *expi);
955
956static struct nr_mapping *gtphub_mapping_new()
957{
958 struct nr_mapping *nrm;
959 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
960 OSMO_ASSERT(nrm);
961
962 nr_mapping_init(nrm);
963 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
964 return nrm;
965}
966
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100967
968#define APPEND(args...) \
969 l = snprintf(pos, left, args); \
970 pos += l; \
971 left -= l
972
973static const char *gtphub_tunnel_side_str(struct gtphub_tunnel *tun,
974 int side_idx)
975{
976 static char buf[256];
977 char *pos = buf;
978 int left = sizeof(buf);
979 int l;
980
981 struct gtphub_tunnel_endpoint *c, *u;
982 c = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
983 u = &tun->endpoint[side_idx][GTPH_PLANE_USER];
984
985 /* print both only if they differ. */
986 if (!c->peer) {
987 APPEND("(uninitialized)");
988 } else {
989 APPEND("%s", gsn_addr_to_str(&c->peer->peer_addr->addr));
990 }
991
992 if (!u->peer) {
993 if (c->peer) {
994 APPEND(" / (uninitialized)");
995 }
996 } else if ((!c->peer)
997 || (!gsn_addr_same(&u->peer->peer_addr->addr,
998 &c->peer->peer_addr->addr))) {
999 APPEND(" / %s", gsn_addr_to_str(&u->peer->peer_addr->addr));
1000 }
1001
1002 APPEND(" (TEI C %x=%x / U %x=%x)",
1003 c->tei_orig, c->tei_repl,
1004 u->tei_orig, u->tei_repl);
1005 return buf;
1006}
1007
1008const char *gtphub_tunnel_str(struct gtphub_tunnel *tun)
1009{
1010 static char buf[512];
1011 char *pos = buf;
1012 int left = sizeof(buf);
1013 int l;
1014
1015 APPEND("%s", gtphub_tunnel_side_str(tun, GTPH_SIDE_SGSN));
1016 APPEND(" <-> %s", gtphub_tunnel_side_str(tun, GTPH_SIDE_GGSN));
1017
1018 return buf;
1019}
1020
1021#undef APPEND
1022
1023void gtphub_tunnel_endpoint_set_peer(struct gtphub_tunnel_endpoint *te,
1024 struct gtphub_peer_port *pp)
1025{
1026 if (te->peer)
1027 gtphub_port_ref_count_dec(te->peer);
1028 te->peer = pp;
1029 if (te->peer)
1030 gtphub_port_ref_count_inc(te->peer);
1031}
1032
1033int gtphub_tunnel_complete(struct gtphub_tunnel *tun)
1034{
1035 if (!tun)
1036 return 0;
1037 int side_idx;
1038 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001039 for_each_side_and_plane(side_idx, plane_idx) {
1040 struct gtphub_tunnel_endpoint *te =
1041 &tun->endpoint[side_idx][plane_idx];
1042 if (!(te->peer && te->tei_orig && te->tei_repl))
1043 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001044 }
1045 return 1;
1046}
1047
1048static void gtphub_tunnel_del_cb(struct expiring_item *expi)
1049{
1050 struct gtphub_tunnel *tun = container_of(expi,
1051 struct gtphub_tunnel,
1052 expiry_entry);
1053 LOG(LOGL_DEBUG, "expired: %s\n", gtphub_tunnel_str(tun));
1054
1055 llist_del(&tun->entry);
1056 INIT_LLIST_HEAD(&tun->entry); /* mark unused */
1057
1058 expi->del_cb = 0; /* avoid recursion loops */
1059 expiring_item_del(&tun->expiry_entry); /* usually already done, but make sure. */
1060
1061 int side_idx;
1062 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001063 for_each_side_and_plane(side_idx, plane_idx) {
1064 /* clear ref count */
1065 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1066 NULL);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001067 }
1068
1069 talloc_free(tun);
1070}
1071
1072static struct gtphub_tunnel *gtphub_tunnel_new()
1073{
1074 struct gtphub_tunnel *tun;
1075 tun = talloc_zero(osmo_gtphub_ctx, struct gtphub_tunnel);
1076 OSMO_ASSERT(tun);
1077
1078 INIT_LLIST_HEAD(&tun->entry);
1079 expiring_item_init(&tun->expiry_entry);
1080
1081 tun->expiry_entry.del_cb = gtphub_tunnel_del_cb;
1082 return tun;
1083}
1084
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001085static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
1086 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001087{
1088 if (llist_empty(&peer->addresses))
1089 return "(addressless)";
1090
1091 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
1092 struct gtphub_peer_addr,
1093 entry);
1094 return gsn_addr_to_strb(&a->addr, buf, buflen);
1095}
1096
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001097static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
1098 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001099{
1100 if (!port)
1101 return "(null port)";
1102
1103 snprintf(buf, buflen, "%s port %d",
1104 gsn_addr_to_str(&port->peer_addr->addr),
1105 (int)port->port);
1106 return buf;
1107}
1108
1109const char *gtphub_peer_str(struct gtphub_peer *peer)
1110{
1111 static char buf[256];
1112 return gtphub_peer_strb(peer, buf, sizeof(buf));
1113}
1114
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001115const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001116{
1117 static char buf[256];
1118 return gtphub_port_strb(port, buf, sizeof(buf));
1119}
1120
1121static const char *gtphub_port_str2(struct gtphub_peer_port *port)
1122{
1123 static char buf[256];
1124 return gtphub_port_strb(port, buf, sizeof(buf));
1125}
1126
1127static void gtphub_mapping_del_cb(struct expiring_item *expi)
1128{
1129 expi->del_cb = 0; /* avoid recursion loops */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001130 expiring_item_del(expi); /* usually already done, but make sure. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001131
1132 struct nr_mapping *nrm = container_of(expi,
1133 struct nr_mapping,
1134 expiry_entry);
1135 llist_del(&nrm->entry);
1136 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
1137
1138 /* Just for log */
1139 struct gtphub_peer_port *from = nrm->origin;
1140 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001141 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001142 (int)nrm->expiry_entry.expiry,
1143 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001144 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001145
1146 gtphub_port_ref_count_dec(from);
1147
1148 talloc_free(nrm);
1149}
1150
1151static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
1152 struct gtphub_peer_port *from,
1153 nr_t orig_nr,
1154 time_t now)
1155{
1156 struct nr_mapping *nrm;
1157
1158 nrm = nr_map_get(map, from, orig_nr);
1159
1160 if (!nrm) {
1161 nrm = gtphub_mapping_new();
1162 nrm->orig = orig_nr;
1163 nrm->origin = from;
1164 nr_map_add(map, nrm, now);
1165 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001166 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001167 gtphub_port_str(from),
1168 (int)(nrm->orig), (int)(nrm->repl));
1169 } else {
Neels Hofmeyr508514c2015-11-24 13:30:38 +01001170 nr_map_refresh(map, nrm, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001171 }
1172
1173 OSMO_ASSERT(nrm);
1174 return nrm;
1175}
1176
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001177static void gtphub_map_seq(struct gtp_packet_desc *p,
1178 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001179 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001180{
1181 /* Store a mapping in to_peer's map, so when we later receive a GTP
1182 * packet back from to_peer, the seq nr can be unmapped back to its
1183 * origin (from_peer here). */
1184 struct nr_mapping *nrm;
1185 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001186 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001187
1188 /* Change the GTP packet to yield the new, mapped seq nr */
1189 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001190}
1191
1192static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1193 struct gtphub_peer_port *responding_port)
1194{
1195 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001196 struct nr_mapping *nrm =
1197 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1198 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001199 if (!nrm)
1200 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001201 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1202 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001203 set_seq(p, nrm->orig);
1204 return nrm->origin;
1205}
1206
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001207static int gtphub_check_mapped_tei(struct gtphub_tunnel_endpoint *new_te,
1208 struct gtphub_tunnel_endpoint *iterated_te,
1209 uint32_t *tei_min,
1210 uint32_t *tei_max)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001211{
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001212 if (new_te->tei_repl != iterated_te->tei_repl)
1213 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001214
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001215 /* new_te->tei_repl is already taken. Try to find one out of the known
1216 * range. */
1217
1218 if ((*tei_max) < 0xffffffff) {
1219 (*tei_max)++;
1220 new_te->tei_repl = *tei_max;
1221 return 1;
1222 } else if ((*tei_min) > 1) {
1223 (*tei_min)--;
1224 new_te->tei_repl = *tei_min;
1225 return 1;
1226 }
1227
1228 /* None seems to be available. */
1229 return 0;
1230}
1231
1232static int gtphub_check_reused_teis(struct gtphub *hub,
1233 struct gtphub_tunnel *new_tun)
1234{
1235 uint32_t tei_min = 0xffffffff;
1236 uint32_t tei_max = 0;
1237 int side_idx;
1238 int plane_idx;
1239 int side_idx2;
1240 int plane_idx2;
1241 struct gtphub_tunnel_endpoint *te;
1242 struct gtphub_tunnel_endpoint *te2;
1243
1244 struct gtphub_tunnel *tun, *ntun;
1245
1246 llist_for_each_entry_safe(tun, ntun, &hub->tunnels, entry) {
1247 if (tun == new_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001248 continue;
1249
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001250 /* Check whether the GSN sent a TEI that it is reusing from a
1251 * previous tunnel. */
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001252 int tun_continue = 0;
1253 for_each_side(side_idx) {
1254 for_each_plane(plane_idx) {
1255 te = &tun->endpoint[side_idx][plane_idx];
1256 te2 = &new_tun->endpoint[side_idx][plane_idx];
1257 if ((te->tei_orig != te2->tei_orig)
1258 || (!te->peer)
1259 || (!te2->peer)
1260 || !gsn_addr_same(&te->peer->peer_addr->addr,
1261 &te2->peer->peer_addr->addr))
1262 continue;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001263
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001264 /* The peer is reusing a TEI that I believe to
1265 * be part of another tunnel. The other tunnel
1266 * must be stale, then. */
1267 LOG(LOGL_NOTICE,
1268 "Expiring tunnel due to reused TEI:"
1269 " peer %s sent %s TEI %x,"
1270 " previously used by tunnel %s...\n",
1271 gtphub_port_str(te->peer),
1272 gtphub_plane_idx_names[plane_idx],
1273 te->tei_orig,
1274 gtphub_tunnel_str(tun));
1275 LOG(LOGL_NOTICE, "...while establishing tunnel %s\n",
1276 gtphub_tunnel_str(new_tun));
Neels Hofmeyr52c0bd32015-12-02 14:14:32 +01001277
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001278 expiring_item_del(&tun->expiry_entry);
1279 /* continue to find more matches. There shouldn't be
1280 * any, but let's make sure. However, tun is deleted,
1281 * so we need to skip to the next tunnel. */
1282 tun_continue = 1;
1283 break;
1284 }
1285 if (tun_continue)
1286 break;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001287 }
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001288 if (tun_continue)
1289 continue;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001290
1291 /* Check whether the mapped TEIs assigned to the endpoints are
1292 * used anywhere else. */
1293 for_each_side_and_plane(side_idx, plane_idx) {
1294 te = &tun->endpoint[side_idx][plane_idx];
1295 tei_min = (tei_min < te->tei_repl)? tei_min : te->tei_repl;
1296 tei_max = (tei_max > te->tei_repl)? tei_max : te->tei_repl;
1297
1298 for_each_side_and_plane(side_idx2, plane_idx2) {
1299 te2 = &new_tun->endpoint[side_idx2][plane_idx2];
1300 if (!gtphub_check_mapped_tei(te2, te, &tei_min, &tei_max)) {
1301 LOG(LOGL_ERROR,
1302 "No mapped TEI is readily available."
1303 " Searching for holes between occupied"
1304 " TEIs not implemented.");
1305 return 0;
1306 }
1307 }
1308 }
1309
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001310 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001311
1312 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001313}
1314
1315static void gtphub_tunnel_refresh(struct gtphub *hub,
1316 struct gtphub_tunnel *tun,
1317 time_t now)
1318{
1319 expiry_add(&hub->expire_slowly,
1320 &tun->expiry_entry,
1321 now);
1322}
1323
1324static struct gtphub_tunnel_endpoint *gtphub_unmap_tei(struct gtphub *hub,
1325 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001326 struct gtphub_peer_port *from,
1327 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001328{
1329 OSMO_ASSERT(from);
1330 int other_side = other_side_idx(p->side_idx);
1331
1332 struct gtphub_tunnel *tun;
1333 llist_for_each_entry(tun, &hub->tunnels, entry) {
1334 struct gtphub_tunnel_endpoint *te_from =
1335 &tun->endpoint[p->side_idx][p->plane_idx];
1336 struct gtphub_tunnel_endpoint *te_to =
1337 &tun->endpoint[other_side][p->plane_idx];
1338 if ((te_to->tei_repl == p->header_tei_rx)
Neels Hofmeyrfc1be3a2015-12-02 00:31:31 +01001339 && te_from->peer
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001340 && gsn_addr_same(&te_from->peer->peer_addr->addr,
1341 &from->peer_addr->addr)) {
1342 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001343 if (unmapped_from_tun)
1344 *unmapped_from_tun = tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001345 return te_to;
1346 }
1347 }
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001348
1349 if (unmapped_from_tun)
1350 *unmapped_from_tun = NULL;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001351 return NULL;
1352}
1353
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001354static void gtphub_map_restart_counter(struct gtphub *hub,
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001355 struct gtp_packet_desc *p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001356{
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001357 if (p->rc != GTP_RC_PDU_C)
1358 return;
1359
1360 int ie_idx;
1361 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1362 if (ie_idx < 0)
1363 return;
1364
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001365 /* Always send gtphub's own restart counter */
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001366 p->ie[ie_idx]->tv1.v = hton8(hub->restart_counter);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001367}
1368
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001369static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001370 struct gtphub_tunnel **unmapped_from_tun,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001371 struct gtphub *hub,
1372 struct gtp_packet_desc *p,
1373 struct gtphub_peer_port *from_port)
1374{
1375 OSMO_ASSERT(p->version == 1);
1376 *to_port_p = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001377 if (unmapped_from_tun)
1378 *unmapped_from_tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001379
1380 /* If the header's TEI is zero, no PDP context has been established
1381 * yet. If nonzero, a mapping should actually already exist for this
1382 * TEI, since it must have been announced in a PDP context creation. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001383 if (!p->header_tei_rx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001384 return 0;
1385
1386 /* to_peer has previously announced a TEI, which was stored and
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001387 * mapped in a tunnel struct. */
1388 struct gtphub_tunnel_endpoint *to;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001389 to = gtphub_unmap_tei(hub, p, from_port, unmapped_from_tun);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001390 if (!to) {
1391 LOG(LOGL_ERROR, "Received unknown TEI %" PRIx32 " from %s\n",
1392 p->header_tei_rx, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001393 return -1;
1394 }
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001395 OSMO_ASSERT(*unmapped_from_tun);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001396
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001397 uint32_t unmapped_tei = to->tei_orig;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001398 set_tei(p, unmapped_tei);
1399
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001400 LOG(LOGL_DEBUG, "Unmapped TEI coming from: %s\n",
1401 gtphub_tunnel_str(*unmapped_from_tun));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001402
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001403 /* May be NULL for an invalidated tunnel. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001404 *to_port_p = to->peer;
1405
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001406 return 0;
1407}
1408
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001409static int gtphub_handle_create_pdp_ctx(struct gtphub *hub,
1410 struct gtp_packet_desc *p,
1411 struct gtphub_peer_port *from_ctrl,
1412 struct gtphub_peer_port *to_ctrl)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001413{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001414 int plane_idx;
1415
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001416 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1417 plane_nrs_match_GSN_addr_IE_indices);
1418
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001419 struct gtphub_tunnel *tun = NULL;
1420
1421 if (p->type == GTP_CREATE_PDP_REQ) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001422 if (p->side_idx != GTPH_SIDE_SGSN) {
1423 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
1424 " Request from the GGSN side");
1425 return -1;
1426 }
1427
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001428 /* A new tunnel. */
1429 tun = gtphub_tunnel_new();
1430 llist_add(&tun->entry, &hub->tunnels);
1431 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001432 /* The endpoint peers on this side (SGSN) will be set from IEs
1433 * below. Also set the GGSN Ctrl endpoint, for logging. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001434 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001435 to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001436 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001437 if (p->side_idx != GTPH_SIDE_GGSN) {
1438 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
1439 " Response from the SGSN side");
1440 return -1;
1441 }
1442
Neels Hofmeyr8c5b0732015-12-03 13:45:15 +01001443 /* The tunnel should already have been resolved from the header
1444 * TEI and be available in tun (== p->tun). Just fill in the
1445 * GSN Addresses below.*/
1446 OSMO_ASSERT(tun);
1447 OSMO_ASSERT(tun->endpoint[other_side_idx(p->side_idx)][GTPH_PLANE_CTRL].tei_repl
1448 == p->header_tei_rx);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001449 OSMO_ASSERT(to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001450 }
1451
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001452 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1453 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001454 unsigned int side_idx = p->side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001455
1456 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01001457 int rc;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001458 struct gsn_addr addr_from_ie;
1459 uint32_t tei_from_ie;
1460 int ie_idx;
1461
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001462 /* Fetch GSN Address and TEI from IEs. As ensured by above
1463 * static asserts, plane_idx corresponds to the GSN Address IE
1464 * index (the first one = 0 = ctrl, second one = 1 = user). */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001465 rc = gsn_addr_get(&addr_from_ie, p, plane_idx);
1466 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001467 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1468 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001469 return -1;
1470 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001471 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001472 gtphub_plane_idx_names[plane_idx],
1473 gsn_addr_to_str(&addr_from_ie),
1474 addr_from_ie.len);
1475
1476 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1477 if (ie_idx < 0) {
1478 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001479 LOG(LOGL_ERROR,
1480 "Create PDP Context message invalid:"
1481 " missing IE %d\n",
1482 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001483 return -1;
1484 }
1485 tei_from_ie = 0;
1486 }
1487 else
1488 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1489
1490 /* Make sure an entry for this peer address with default port
1491 * exists */
1492 struct gtphub_peer_port *peer_from_ie =
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001493 gtphub_port_have(hub, &hub->to_gsns[side_idx][plane_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001494 &addr_from_ie,
1495 gtphub_plane_idx_default_port[plane_idx]);
1496
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001497 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1498 peer_from_ie);
1499
Neels Hofmeyr59c1b642015-12-03 11:30:43 +01001500 if (!tei_from_ie &&
1501 !tun->endpoint[side_idx][plane_idx].tei_orig) {
1502 LOG(LOGL_ERROR,
1503 "Create PDP Context message omits %s TEI, but the"
1504 " no TEI has been announced for this tunnel: %s",
1505 gtphub_plane_idx_names[plane_idx],
1506 gtphub_tunnel_str(tun));
1507 return -1;
1508 }
1509
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001510 if (tei_from_ie) {
1511 /* Create TEI mapping and replace in GTP packet IE */
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01001512 uint32_t mapped_tei = nr_pool_next(&hub->tei_pool);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001513
1514 tun->endpoint[side_idx][plane_idx].tei_orig = tei_from_ie;
1515 tun->endpoint[side_idx][plane_idx].tei_repl = mapped_tei;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001516 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001517
Neels Hofmeyrcd865d62015-11-30 12:58:48 +01001518 if (!gtphub_check_reused_teis(hub, tun)) {
1519 /* It's highly unlikely that all TEIs are
1520 * taken. But the code looking for an unused
1521 * TEI is, at the time of writing this comment,
1522 * not able to find gaps in the TEI space. To
1523 * explicitly alert the user of this problem,
1524 * rather abort than carry on. */
1525 LOG(LOGL_FATAL, "TEI range exhausted. Cannot create TEI mapping, aborting.\n");
1526 abort();
1527 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001528 }
1529
1530 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001531 rc = gsn_addr_put(&hub->to_gsns[other_side_idx(side_idx)][plane_idx].local_addr,
1532 p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001533 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001534 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1535 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001536 return -1;
1537 }
1538 }
1539
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001540 if (p->type == GTP_CREATE_PDP_REQ) {
1541 LOG(LOGL_DEBUG, "New tunnel, first half: %s\n",
1542 gtphub_tunnel_str(tun));
Neels Hofmeyr005f1752015-11-30 12:19:11 +01001543 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001544 LOG(LOGL_DEBUG, "New tunnel: %s\n",
1545 gtphub_tunnel_str(tun));
1546 }
1547
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001548 return 0;
1549}
1550
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001551static void pending_delete_del_cb(struct expiring_item *expi)
1552{
1553 struct pending_delete *pd;
1554 pd = container_of(expi, struct pending_delete, expiry_entry);
1555
1556 llist_del(&pd->entry);
1557 INIT_LLIST_HEAD(&pd->entry);
1558
1559 pd->expiry_entry.del_cb = 0;
1560 expiring_item_del(&pd->expiry_entry);
1561
1562 talloc_free(pd);
1563}
1564
1565static struct pending_delete *pending_delete_new(void)
1566{
1567 struct pending_delete *pd = talloc_zero(osmo_gtphub_ctx, struct pending_delete);
1568 INIT_LLIST_HEAD(&pd->entry);
1569 expiring_item_init(&pd->expiry_entry);
1570 pd->expiry_entry.del_cb = pending_delete_del_cb;
1571 return pd;
1572}
1573
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001574static int gtphub_handle_delete_pdp_ctx(struct gtphub *hub,
1575 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001576 struct gtphub_tunnel *known_tun,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001577 struct gtphub_peer_port *from_ctrl,
1578 struct gtphub_peer_port *to_ctrl)
1579{
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001580 if (p->type == GTP_DELETE_PDP_REQ) {
1581 if (!known_tun) {
1582 LOG(LOGL_ERROR, "Cannot find tunnel for Delete PDP Context Request.\n");
1583 return -1;
1584 }
1585
1586 /* Store the Delete Request until a successful Response is seen. */
1587 uint8_t teardown_ind;
1588 uint8_t nsapi;
1589
1590 if (gtpie_gettv1(p->ie, GTPIE_TEARDOWN, 0, &teardown_ind) != 0) {
1591 LOG(LOGL_ERROR, "Missing Teardown Ind IE in Delete PDP Context Request.\n");
1592 return -1;
1593 }
1594
1595 if (gtpie_gettv1(p->ie, GTPIE_NSAPI, 0, &nsapi) != 0) {
1596 LOG(LOGL_ERROR, "Missing NSAPI IE in Delete PDP Context Request.\n");
1597 return -1;
1598 }
1599
1600 struct pending_delete *pd = NULL;
1601
1602 struct pending_delete *pdi = NULL;
1603 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1604 if ((pdi->tun == known_tun)
1605 && (pdi->teardown_ind == teardown_ind)
1606 && (pdi->nsapi == nsapi)) {
1607 pd = pdi;
1608 break;
1609 }
1610 }
1611
1612 if (!pd) {
1613 pd = pending_delete_new();
1614 pd->tun = known_tun;
1615 pd->teardown_ind = teardown_ind;
1616 pd->nsapi = nsapi;
1617
1618 LOG(LOGL_DEBUG, "Tunnel delete pending: %s\n",
1619 gtphub_tunnel_str(known_tun));
1620 llist_add(&pd->entry, &hub->pending_deletes);
1621 }
1622
1623 /* Add or refresh timeout. */
1624 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1625
1626 /* If a pending_delete should expire before the response to
1627 * indicate success comes in, the responding peer will have the
1628 * tunnel deactivated, while the requesting peer gets no reply
1629 * and keeps the tunnel. The hope is that the requesting peer
1630 * will try again and get a useful response. */
1631 } else if (p->type == GTP_DELETE_PDP_RSP) {
1632 /* Find the Delete Request for this Response. */
1633 struct pending_delete *pd = NULL;
1634
1635 struct pending_delete *pdi;
1636 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1637 if (known_tun == pdi->tun) {
1638 pd = pdi;
1639 break;
1640 }
1641 }
1642
1643 if (!pd) {
1644 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1645 " Cannot find matching request.");
1646 /* If we delete the tunnel now, anyone can send a
1647 * Delete response to kill tunnels at will. */
1648 return -1;
1649 }
1650
1651 /* TODO handle teardown_ind and nsapi */
1652
1653 expiring_item_del(&pd->expiry_entry);
1654
1655 uint8_t cause;
1656 if (gtpie_gettv1(p->ie, GTPIE_CAUSE, 0, &cause) != 0) {
1657 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1658 " Missing Cause IE.");
1659 /* If we delete the tunnel now, at least one of the
1660 * peers may still think it is active. */
1661 return -1;
1662 }
1663
1664 if (cause != GTPCAUSE_ACC_REQ) {
1665 LOG(LOGL_NOTICE,
1666 "Delete PDP Context Response indicates failure;"
1667 "for %s\n",
1668 gtphub_tunnel_str(known_tun));
1669 return -1;
1670 }
1671
1672 LOG(LOGL_DEBUG, "Delete PDP Context: removing tunnel %s\n",
1673 gtphub_tunnel_str(known_tun));
1674 expiring_item_del(&known_tun->expiry_entry);
1675 }
1676
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001677 return 0;
1678}
1679
1680static int gtphub_handle_update_pdp_ctx(struct gtphub *hub,
1681 struct gtp_packet_desc *p,
1682 struct gtphub_peer_port *from_ctrl,
1683 struct gtphub_peer_port *to_ctrl)
1684{
1685 /* TODO */
1686 return 0;
1687}
1688
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001689/* Read GSN address IEs from p, and make sure these peer addresses exist in
1690 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1691 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1692 * the packet p. */
1693static int gtphub_handle_pdp_ctx(struct gtphub *hub,
1694 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001695 struct gtphub_tunnel *known_tun,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001696 struct gtphub_peer_port *from_ctrl,
1697 struct gtphub_peer_port *to_ctrl)
1698{
1699 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1700
1701 switch (p->type) {
1702 case GTP_CREATE_PDP_REQ:
1703 case GTP_CREATE_PDP_RSP:
1704 return gtphub_handle_create_pdp_ctx(hub, p,
1705 from_ctrl, to_ctrl);
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001706
1707 case GTP_DELETE_PDP_REQ:
1708 case GTP_DELETE_PDP_RSP:
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001709 return gtphub_handle_delete_pdp_ctx(hub, p, known_tun,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001710 from_ctrl, to_ctrl);
1711
1712 case GTP_UPDATE_PDP_REQ:
1713 case GTP_UPDATE_PDP_RSP:
1714 return gtphub_handle_update_pdp_ctx(hub, p,
1715 from_ctrl, to_ctrl);
1716
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001717 default:
1718 /* Nothing to do for this message type. */
1719 return 0;
1720 }
1721
1722}
1723
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001724static int gtphub_send_del_pdp_ctx(struct gtphub *hub,
1725 struct gtphub_tunnel *tun,
1726 int to_side)
1727{
1728 static uint8_t del_ctx_msg[16] = {
1729 0x32, /* GTP v1 flags */
1730 GTP_DELETE_PDP_REQ,
1731 0x00, 0x08, /* Length in network byte order */
1732 0x00, 0x00, 0x00, 0x00, /* TEI to be replaced */
1733 0, 0, /* Seq, to be replaced */
1734 0, 0, /* no extensions */
1735 0x13, 0xff, /* 19: Teardown ind = 1 */
1736 0x14, 0 /* 20: NSAPI = 0 */
1737 };
1738
1739 uint32_t *tei = (uint32_t*)&del_ctx_msg[4];
1740 uint16_t *seq = (uint16_t*)&del_ctx_msg[8];
1741
1742 struct gtphub_tunnel_endpoint *te =
1743 &tun->endpoint[to_side][GTPH_PLANE_CTRL];
1744
1745 if (! te->peer)
1746 return 0;
1747
1748 *tei = hton32(te->tei_orig);
1749 *seq = hton16(nr_pool_next(&te->peer->peer_addr->peer->seq_pool));
1750
1751 struct gtphub_bind *to_bind = &hub->to_gsns[to_side][GTPH_PLANE_CTRL];
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01001752 int rc = gtphub_write(&to_bind->ofd, &te->peer->sa,
1753 del_ctx_msg, sizeof(del_ctx_msg));
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001754 if (rc != 0) {
1755 LOG(LOGL_ERROR,
1756 "Failed to send out-of-band Delete PDP Context Request to %s\n",
1757 gtphub_port_str(te->peer));
1758 }
1759 return rc;
1760}
1761
1762/* Tell all peers on the other end of tunnels that PDP contexts are void. */
1763static void gtphub_restarted(struct gtphub *hub,
1764 struct gtp_packet_desc *p,
1765 struct gtphub_peer_port *pp)
1766{
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001767 LOG(LOGL_DEBUG, "Peer has restarted: %s\n",
1768 gtphub_port_str(pp));
1769
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001770 struct gtphub_tunnel *tun;
1771 llist_for_each_entry(tun, &hub->tunnels, entry) {
1772 int side_idx;
1773 for_each_side(side_idx) {
Neels Hofmeyrc6d51f52015-12-02 15:44:34 +01001774 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
1775 struct gtphub_tunnel_endpoint *te2 = &tun->endpoint[other_side_idx(side_idx)][GTPH_PLANE_CTRL];
1776 if ((!te->peer)
1777 || (!te2->tei_orig)
1778 || (pp->peer_addr->peer != te->peer->peer_addr->peer))
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001779 continue;
1780
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001781 LOG(LOGL_DEBUG, "Deleting tunnel due to peer restart: %s\n",
1782 gtphub_tunnel_str(tun));
1783
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001784 /* Send a Delete PDP Context Request to the
1785 * peer on the other side, remember the pending
1786 * delete and wait for the response to delete
1787 * the tunnel. Clear this side of the tunnel to
1788 * make sure it isn't used.
1789 *
1790 * Should the delete message send fail, or if no
1791 * response is received, this tunnel will expire. If
1792 * its TEIs come up in a new PDP Context Request, it
1793 * will be removed. If messages for this tunnel should
1794 * come in (from the not restarted side), they will be
1795 * dropped because the tunnel is rendered unusable. */
1796 gtphub_send_del_pdp_ctx(hub, tun, other_side_idx(side_idx));
1797
1798 struct pending_delete *pd;
1799 pd = pending_delete_new();
1800 pd->tun = tun;
1801 pd->teardown_ind = 0xff;
1802 pd->nsapi = 0;
1803 llist_add(&pd->entry, &hub->pending_deletes);
1804 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1805
1806 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_CTRL],
1807 NULL);
1808 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_USER],
1809 NULL);
1810 }
1811 }
1812}
1813
1814static int get_restart_count(struct gtp_packet_desc *p)
1815{
1816 int ie_idx;
1817 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1818 if (ie_idx < 0)
1819 return -1;
1820 return ntoh8(p->ie[ie_idx]->tv1.v);
1821}
1822
1823static void gtphub_check_restart_counter(struct gtphub *hub,
1824 struct gtp_packet_desc *p,
1825 struct gtphub_peer_port *from)
1826{
1827 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1828 * that doesn't match the peer's previously sent restart counter, clear
1829 * that peer and cancel PDP contexts. */
1830
1831 int restart = get_restart_count(p);
1832
1833 if ((restart < 0) || (restart > 255))
1834 return;
1835
1836 if ((from->last_restart_count >= 0) && (from->last_restart_count <= 255)) {
1837 if (from->last_restart_count != restart) {
1838 gtphub_restarted(hub, p, from);
1839 }
1840 }
1841
1842 from->last_restart_count = restart;
1843}
1844
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001845static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1846{
1847 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1848 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
1849 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1850 gtphub_plane_idx_names[plane_idx]);
1851
1852 if (!(what & BSC_FD_READ))
1853 return 0;
1854
1855 struct gtphub *hub = from_sgsns_ofd->data;
1856
1857 static uint8_t buf[4096];
1858 struct osmo_sockaddr from_addr;
1859 struct osmo_sockaddr to_addr;
1860 struct osmo_fd *to_ofd;
1861 int len;
1862 uint8_t *reply_buf;
1863
1864 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1865 if (len < 1)
1866 return 0;
1867
1868 len = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, &from_addr,
1869 buf, len, gtphub_now(),
1870 &reply_buf, &to_ofd, &to_addr);
1871 if (len < 1)
1872 return 0;
1873
1874 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
1875}
1876
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001877static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1878{
1879 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1880 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001881 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1882 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001883 if (!(what & BSC_FD_READ))
1884 return 0;
1885
1886 struct gtphub *hub = from_ggsns_ofd->data;
1887
1888 static uint8_t buf[4096];
1889 struct osmo_sockaddr from_addr;
1890 struct osmo_sockaddr to_addr;
1891 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001892 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001893 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001894
1895 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1896 if (len < 1)
1897 return 0;
1898
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001899 len = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, &from_addr,
1900 buf, len, gtphub_now(),
1901 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001902 if (len < 1)
1903 return 0;
1904
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001905 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001906}
1907
1908static int gtphub_unmap(struct gtphub *hub,
1909 struct gtp_packet_desc *p,
1910 struct gtphub_peer_port *from,
1911 struct gtphub_peer_port *to_proxy,
1912 struct gtphub_peer_port **final_unmapped,
1913 struct gtphub_peer_port **unmapped_from_seq,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001914 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001915{
1916 /* Always (try to) unmap sequence and TEI numbers, which need to be
1917 * replaced in the packet. Either way, give precedence to the proxy, if
1918 * configured. */
1919
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001920 if (unmapped_from_seq)
1921 *unmapped_from_seq = NULL;
1922 if (unmapped_from_tun)
1923 *unmapped_from_tun = NULL;
1924 if (final_unmapped)
1925 *final_unmapped = NULL;
1926
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001927 struct gtphub_peer_port *from_seq = NULL;
1928 struct gtphub_peer_port *from_tei = NULL;
1929 struct gtphub_peer_port *unmapped = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001930 struct gtphub_tunnel *tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001931
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001932 from_seq = gtphub_unmap_seq(p, from);
1933
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001934 if (gtphub_unmap_header_tei(&from_tei, &tun, hub, p, from) < 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001935 return -1;
1936
1937 struct gtphub_peer *from_peer = from->peer_addr->peer;
1938 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001939 LOG(LOGL_DEBUG,
1940 "Seq unmap and TEI unmap yield two different peers."
1941 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001942 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1943 gtphub_plane_idx_names[p->plane_idx],
1944 gtphub_peer_str(from_peer),
1945 (int)p->seq,
1946 gtphub_port_str(from_seq),
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001947 (unsigned int)p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001948 gtphub_port_str2(from_tei)
1949 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001950 }
1951 unmapped = (from_seq? from_seq : from_tei);
1952
1953 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001954 LOG(LOGL_NOTICE,
1955 "Unmap yields a different peer than the configured proxy."
1956 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001957 " unmapped: %s proxy: %s\n",
1958 gtphub_port_str(unmapped),
1959 gtphub_port_str2(to_proxy)
1960 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001961 }
1962 unmapped = (to_proxy? to_proxy : unmapped);
1963
1964 if (!unmapped) {
1965 /* Return no error, but returned pointers are all NULL. */
1966 return 0;
1967 }
1968
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001969 if (unmapped_from_seq)
1970 *unmapped_from_seq = from_seq;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001971 if (unmapped_from_tun)
1972 *unmapped_from_tun = tun;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001973 if (final_unmapped)
1974 *final_unmapped = unmapped;
1975 return 0;
1976}
1977
1978static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1979 uint16_t port,
1980 struct osmo_sockaddr *dst)
1981{
1982 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
1983}
1984
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001985/* If p is an Echo request, replace p's data with the matching response and
1986 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
1987 * detected. */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001988static int gtphub_handle_echo_req(struct gtphub *hub, struct gtp_packet_desc *p,
1989 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001990{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001991 if (p->type != GTP_ECHO_REQ)
1992 return 0;
1993
1994 static uint8_t echo_response_data[14] = {
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001995 0x32, /* GTP v1 flags */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001996 GTP_ECHO_RSP,
1997 0x00, 14 - 8, /* Length in network byte order */
1998 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
1999 0, 0, /* Seq, to be replaced */
2000 0, 0, /* no extensions */
2001 0x0e, /* Recovery IE */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002002 0 /* Restart counter, to be replaced */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002003 };
2004 uint16_t *seq = (uint16_t*)&echo_response_data[8];
2005 uint8_t *recovery = &echo_response_data[13];
2006
2007 *seq = hton16(p->seq);
2008 *recovery = hub->restart_counter;
2009
2010 *reply_buf = echo_response_data;
2011
2012 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002013}
2014
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002015struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2016 const struct osmo_sockaddr *addr);
2017
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002018/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
2019 * address to forward to. Return a pointer to the osmo_fd, but copy the
2020 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
2021 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
2022 * bytes to forward, 0 or less on failure. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002023int gtphub_handle_buf(struct gtphub *hub,
2024 unsigned int side_idx,
2025 unsigned int plane_idx,
2026 const struct osmo_sockaddr *from_addr,
2027 uint8_t *buf,
2028 size_t received,
2029 time_t now,
2030 uint8_t **reply_buf,
2031 struct osmo_fd **to_ofd,
2032 struct osmo_sockaddr *to_addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002033{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002034 struct gtphub_bind *from_bind = &hub->to_gsns[side_idx][plane_idx];
2035 struct gtphub_bind *to_bind = &hub->to_gsns[other_side_idx(side_idx)][plane_idx];
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002036
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002037 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
2038 received);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002039 LOG(LOGL_DEBUG, "%s rx %s from %s %s\n",
2040 (side_idx == GTPH_SIDE_GGSN)? "<-" : "->",
Neels Hofmeyre921e322015-11-11 00:45:50 +01002041 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002042 gtphub_side_idx_names[side_idx],
Neels Hofmeyre921e322015-11-11 00:45:50 +01002043 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002044
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01002045 struct gtp_packet_desc p;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002046 gtp_decode(buf, received, side_idx, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002047
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002048 if (p.rc <= 0) {
2049 LOG(LOGL_ERROR, "INVALID: dropping GTP packet from %s %s %s\n",
2050 gtphub_side_idx_names[side_idx],
2051 gtphub_plane_idx_names[plane_idx],
2052 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002053 return -1;
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002054 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002055
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002056 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2057
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002058 int reply_len;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002059 reply_len = gtphub_handle_echo_req(hub, &p, reply_buf);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002060 if (reply_len > 0) {
2061 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002062 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002063 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002064
2065 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2066 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2067 reply_len);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002068 LOG(LOGL_DEBUG, "%s Echo response to %s: %d bytes to %s\n",
2069 (side_idx == GTPH_SIDE_GGSN)? "-->" : "<--",
2070 gtphub_side_idx_names[side_idx],
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002071 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002072 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002073 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002074 if (reply_len < 0)
2075 return -1;
2076
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002077 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002078
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002079 /* If a proxy is configured, check that it's indeed that proxy talking
2080 * to us. A proxy is a forced 1:1 connection, e.g. to another gtphub,
2081 * so no-one else is allowed to talk to us from that side. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002082 struct gtphub_peer_port *from_peer = hub->proxy[side_idx][plane_idx];
2083 if (from_peer) {
2084 if (osmo_sockaddr_cmp(&from_peer->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002085 LOG(LOGL_ERROR,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002086 "Rejecting: %s proxy configured, but GTP packet"
2087 " received on %s bind is from another sender:"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002088 " proxy: %s sender: %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002089 gtphub_side_idx_names[side_idx],
2090 gtphub_side_idx_names[side_idx],
2091 gtphub_port_str(from_peer),
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002092 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002093 return -1;
2094 }
2095 }
2096
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002097 if (!from_peer) {
2098 /* Find or create a peer with a matching address. The sender's
2099 * port may in fact differ. */
2100 from_peer = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002101 }
2102
2103 /* If any PDP context has been created, we already have an entry for
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002104 * this GSN. If we don't have an entry, a GGSN has nothing to tell us
2105 * about, while an SGSN may initiate a PDP context. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002106 if (!from_peer) {
2107 if (side_idx == GTPH_SIDE_GGSN) {
2108 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
2109 osmo_sockaddr_to_str(from_addr));
2110 return -1;
2111 } else {
2112 /* SGSN */
2113 /* A new peer. If this is on the Ctrl plane, an SGSN
2114 * may make first contact without being known yet, so
2115 * create the peer struct for the current sender. */
2116 if (plane_idx != GTPH_PLANE_CTRL) {
2117 LOG(LOGL_ERROR,
2118 "Dropping packet: User plane peer was not"
2119 "announced by PDP Context: %s\n",
2120 osmo_sockaddr_to_str(from_addr));
2121 return -1;
2122 }
2123
2124 struct gsn_addr from_gsna;
2125 uint16_t from_port;
2126 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
2127 return -1;
2128
2129 from_peer = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
2130 }
2131 }
2132
2133 if (!from_peer) {
2134 /* This could theoretically happen for invalid address data or
2135 * somesuch. */
2136 LOG(LOGL_ERROR, "Dropping packet: invalid %s peer: %s\n",
2137 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002138 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002139 return -1;
2140 }
2141
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002142 LOG(LOGL_DEBUG, "from %s peer: %s\n", gtphub_side_idx_names[side_idx],
2143 gtphub_port_str(from_peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002144
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002145 struct gtphub_peer_port *to_peer_from_seq;
2146 struct gtphub_peer_port *to_peer;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002147 struct gtphub_tunnel *tun;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002148 if (gtphub_unmap(hub, &p, from_peer,
2149 hub->proxy[other_side_idx(side_idx)][plane_idx],
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002150 &to_peer, &to_peer_from_seq, &tun)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002151 != 0) {
2152 return -1;
2153 }
2154
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002155 if ((!to_peer) && (side_idx == GTPH_SIDE_SGSN)) {
2156 if (gtphub_resolve_ggsn(hub, &p, &to_peer) < 0)
2157 return -1;
2158 }
2159
2160 if (!to_peer) {
2161 LOG(LOGL_ERROR, "No %s to send to. Dropping packet.\n",
2162 gtphub_side_idx_names[other_side_idx(side_idx)]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002163 return -1;
2164 }
2165
2166 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002167 /* This may be a Create PDP Context response. If it is, there
2168 * are other addresses in the GTP message to set up apart from
2169 * the sender. */
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002170 if (gtphub_handle_pdp_ctx(hub, &p, tun, from_peer, to_peer)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002171 != 0)
2172 return -1;
2173 }
2174
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002175 gtphub_check_restart_counter(hub, &p, from_peer);
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002176 gtphub_map_restart_counter(hub, &p);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002177
2178 /* If the GGSN is replying to an SGSN request, the sequence nr has
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002179 * already been unmapped above (to_peer_from_seq != NULL), and we need not
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002180 * create a new mapping. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002181 if (!to_peer_from_seq)
2182 gtphub_map_seq(&p, from_peer, to_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002183
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002184 osmo_sockaddr_copy(to_addr, &to_peer->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002185
2186 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01002187
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002188 if (received) {
2189 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2190 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2191 received);
2192 }
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002193 LOG(LOGL_DEBUG, "%s Forward to %s: %d bytes to %s\n",
2194 (side_idx == GTPH_SIDE_SGSN)? "-->" : "<--",
2195 gtphub_side_idx_names[other_side_idx(side_idx)],
Neels Hofmeyre921e322015-11-11 00:45:50 +01002196 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002197 return received;
2198}
2199
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002200static void resolved_gssn_del_cb(struct expiring_item *expi)
2201{
2202 struct gtphub_resolved_ggsn *ggsn;
2203 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
2204
2205 gtphub_port_ref_count_dec(ggsn->peer);
2206 llist_del(&ggsn->entry);
2207
2208 ggsn->expiry_entry.del_cb = 0;
2209 expiring_item_del(&ggsn->expiry_entry);
2210
2211 talloc_free(ggsn);
2212}
2213
2214void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
2215 struct gsn_addr *resolved_addr,
2216 time_t now)
2217{
2218 struct gtphub_peer_port *pp;
2219 struct gtphub_resolved_ggsn *ggsn;
2220
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002221 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002222 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
2223 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01002224
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002225 pp = gtphub_port_have(hub, &hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002226 resolved_addr, 2123);
2227 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002228 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
2229 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002230 return;
2231 }
2232
2233 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
2234 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01002235 INIT_LLIST_HEAD(&ggsn->entry);
2236 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002237
2238 ggsn->peer = pp;
2239 gtphub_port_ref_count_inc(pp);
2240
2241 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01002242 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002243
2244 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002245 expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002246
2247 llist_add(&ggsn->entry, &hub->resolved_ggsns);
2248}
2249
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002250static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
2251{
2252 return pp->ref_count == 0;
2253}
2254
2255static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
2256{
2257 struct gtphub_peer_port *pp, *npp;
2258 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
2259 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002260 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002261 gtphub_port_str(pp));
2262 gtphub_peer_port_del(pp);
2263 }
2264 }
2265 return llist_empty(&pa->ports);
2266}
2267
2268static int gtphub_gc_peer(struct gtphub_peer *p)
2269{
2270 struct gtphub_peer_addr *pa, *npa;
2271 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
2272 if (gtphub_gc_peer_addr(pa)) {
2273 gtphub_peer_addr_del(pa);
2274 }
2275 }
2276
2277 /* Note that there's a ref_count in each gtphub_peer_port instance
2278 * listed within p->addresses, referenced by TEI mappings from
2279 * hub->tei_map. As long as those don't expire, this peer will stay. */
2280
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002281 return llist_empty(&p->addresses)
2282 && nr_map_empty(&p->seq_map);
2283}
2284
2285static void gtphub_gc_bind(struct gtphub_bind *b)
2286{
2287 struct gtphub_peer *p, *n;
2288 llist_for_each_entry_safe(p, n, &b->peers, entry) {
2289 if (gtphub_gc_peer(p)) {
2290 gtphub_peer_del(p);
2291 }
2292 }
2293}
2294
2295void gtphub_gc(struct gtphub *hub, time_t now)
2296{
2297 int expired;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002298 expired = expiry_tick(&hub->expire_quickly, now);
2299 expired += expiry_tick(&hub->expire_slowly, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002300
2301 /* ... */
2302
2303 if (expired) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002304 int s, p;
2305 for_each_side_and_plane(s, p) {
2306 gtphub_gc_bind(&hub->to_gsns[s][p]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002307 }
2308 }
2309}
2310
2311static void gtphub_gc_cb(void *data)
2312{
2313 struct gtphub *hub = data;
2314 gtphub_gc(hub, gtphub_now());
2315 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2316}
2317
2318static void gtphub_gc_start(struct gtphub *hub)
2319{
2320 hub->gc_timer.cb = gtphub_gc_cb;
2321 hub->gc_timer.data = hub;
2322
2323 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2324}
2325
2326/* called by unit tests */
2327void gtphub_init(struct gtphub *hub)
2328{
2329 gtphub_zero(hub);
2330
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002331 INIT_LLIST_HEAD(&hub->tunnels);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002332 INIT_LLIST_HEAD(&hub->pending_deletes);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002333
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002334 expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS);
2335 expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002336
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01002337 nr_pool_init(&hub->tei_pool, 1, 0xffffffff);
2338
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002339 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002340 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002341 for_each_side_and_plane(side_idx, plane_idx) {
2342 gtphub_bind_init(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002343 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002344
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002345 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].label = "SGSN Ctrl";
2346 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].label = "GGSN Ctrl";
2347 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].label = "SGSN User";
2348 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002349}
2350
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002351/* For the test suite, this is kept separate from gtphub_stop(), which also
2352 * closes sockets. The test suite avoids using sockets and would cause
2353 * segfaults when trying to close uninitialized ofds. */
2354void gtphub_free(struct gtphub *hub)
2355{
2356 /* By expiring all mappings, a garbage collection should free
2357 * everything else. A gtphub_bind_free() will assert that everything is
2358 * indeed empty. */
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002359 expiry_clear(&hub->expire_quickly);
2360 expiry_clear(&hub->expire_slowly);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002361
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002362 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002363 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002364 for_each_side_and_plane(side_idx, plane_idx) {
2365 gtphub_gc_bind(&hub->to_gsns[side_idx][plane_idx]);
2366 gtphub_bind_free(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002367 }
2368}
2369
2370void gtphub_stop(struct gtphub *hub)
2371{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002372 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002373 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002374 for_each_side_and_plane(side_idx, plane_idx) {
2375 gtphub_bind_stop(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002376 }
2377 gtphub_free(hub);
2378}
2379
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002380static int gtphub_make_proxy(struct gtphub *hub,
2381 struct gtphub_peer_port **pp,
2382 struct gtphub_bind *bind,
2383 const struct gtphub_cfg_addr *addr)
2384{
2385 if (!addr->addr_str)
2386 return 0;
2387
2388 struct gsn_addr gsna;
2389 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
2390 return -1;
2391
2392 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
2393
2394 /* This is *the* proxy. Make sure it is never expired. */
2395 gtphub_port_ref_count_inc(*pp);
2396 return 0;
2397}
2398
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002399int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
2400 uint8_t restart_counter)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002401{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002402 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002403
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002404 hub->restart_counter = restart_counter;
2405
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002406 /* If a Ctrl plane proxy is configured, ares will never be used. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002407 if (!cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str) {
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002408 if (gtphub_ares_init(hub) != 0) {
2409 LOG(LOGL_FATAL, "Failed to initialize ares\n");
2410 return -1;
2411 }
2412 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002413
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002414 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002415 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002416 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01002417 int rc;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002418 rc = gtphub_bind_start(&hub->to_gsns[side_idx][plane_idx],
2419 &cfg->to_gsns[side_idx][plane_idx],
2420 (side_idx == GTPH_SIDE_SGSN)
2421 ? from_sgsns_read_cb
2422 : from_ggsns_read_cb,
2423 hub, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002424 if (rc) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002425 LOG(LOGL_FATAL, "Failed to bind for %ss (%s)\n",
2426 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002427 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002428 return rc;
2429 }
2430 }
2431
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002432 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002433 if (gtphub_make_proxy(hub,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002434 &hub->proxy[side_idx][plane_idx],
2435 &hub->to_gsns[side_idx][plane_idx],
2436 &cfg->proxy[side_idx][plane_idx])
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002437 != 0) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002438 LOG(LOGL_FATAL, "Cannot configure %s proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002439 " %s port %d.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002440 gtphub_side_idx_names[side_idx],
2441 cfg->proxy[side_idx][plane_idx].addr_str,
2442 (int)cfg->proxy[side_idx][plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002443 return -1;
2444 }
2445 }
2446
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002447 for_each_side_and_plane(side_idx, plane_idx) {
2448 if (hub->proxy[side_idx][plane_idx])
2449 LOG(LOGL_NOTICE, "Using %s %s proxy %s\n",
2450 gtphub_side_idx_names[side_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002451 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002452 gtphub_port_str(hub->proxy[side_idx][plane_idx]));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002453 }
2454
2455 gtphub_gc_start(hub);
2456 return 0;
2457}
2458
2459static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
2460 const struct gsn_addr *addr)
2461{
2462 struct gtphub_peer_addr *a;
2463 llist_for_each_entry(a, &peer->addresses, entry) {
2464 if (gsn_addr_same(&a->addr, addr))
2465 return a;
2466 }
2467 return NULL;
2468}
2469
2470static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2471 uint16_t port)
2472{
2473 OSMO_ASSERT(port);
2474 struct gtphub_peer_port *pp;
2475 llist_for_each_entry(pp, &a->ports, entry) {
2476 if (pp->port == port)
2477 return pp;
2478 }
2479 return NULL;
2480}
2481
2482static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2483 const struct gsn_addr *addr)
2484{
2485 struct gtphub_peer *peer;
2486 llist_for_each_entry(peer, &bind->peers, entry) {
2487 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2488 if (a)
2489 return a;
2490 }
2491 return NULL;
2492}
2493
2494static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2495 const struct gsn_addr *addr,
2496 uint16_t port)
2497{
2498 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2499 if (!a)
2500 return NULL;
2501 return gtphub_addr_find_port(a, port);
2502}
2503
2504struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2505 const struct osmo_sockaddr *addr)
2506{
2507 struct gsn_addr gsna;
2508 uint16_t port;
2509 gsn_addr_from_sockaddr(&gsna, &port, addr);
2510 return gtphub_port_find(bind, &gsna, port);
2511}
2512
2513static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2514 struct gtphub_bind *bind)
2515{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002516 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2517 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002518 OSMO_ASSERT(peer);
2519
2520 INIT_LLIST_HEAD(&peer->addresses);
2521
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002522 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002523 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002524
2525 /* TODO use something random to pick the initial sequence nr.
2526 0x6d31 produces the ASCII character sequence 'm1', currently used in
2527 gtphub_nc_test.sh. */
2528 peer->seq_pool.last_nr = 0x6d31 - 1;
2529
2530 llist_add(&peer->entry, &bind->peers);
2531 return peer;
2532}
2533
2534static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2535 const struct gsn_addr *addr)
2536{
2537 struct gtphub_peer_addr *a;
2538 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2539 OSMO_ASSERT(a);
2540 a->peer = peer;
2541 gsn_addr_copy(&a->addr, addr);
2542 INIT_LLIST_HEAD(&a->ports);
2543 llist_add(&a->entry, &peer->addresses);
2544
2545 return a;
2546}
2547
2548static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2549 struct gtphub_bind *bind,
2550 const struct gsn_addr *addr)
2551{
2552 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2553 if (a)
2554 return a;
2555
2556 /* If we haven't found an address, that means we need to create an
2557 * entirely new peer for the new address. More addresses may be added
2558 * to this peer later, but not via this function. */
2559 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002560
2561 a = gtphub_peer_add_addr(peer, addr);
2562
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002563 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002564 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002565 gsn_addr_to_str(&a->addr));
2566
2567 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002568}
2569
2570static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2571 uint16_t port)
2572{
2573 struct gtphub_peer_port *pp;
2574
2575 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2576 OSMO_ASSERT(pp);
2577 pp->peer_addr = a;
2578 pp->port = port;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002579 pp->last_restart_count = -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002580
2581 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2582 talloc_free(pp);
2583 return NULL;
2584 }
2585
2586 llist_add(&pp->entry, &a->ports);
2587
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002588 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002589 gsn_addr_to_str(&a->addr),
2590 (int)port);
2591
2592 return pp;
2593}
2594
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002595struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2596 struct gtphub_bind *bind,
2597 const struct gsn_addr *addr,
2598 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002599{
2600 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2601
2602 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2603 if (pp)
2604 return pp;
2605
2606 return gtphub_addr_add_port(a, port);
2607}
2608
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002609/* Find a GGSN peer with a matching address. If the address is known but the
2610 * port not, create a new port for that peer address. */
2611struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2612 const struct osmo_sockaddr *addr)
2613{
2614 struct gtphub_peer_addr *pa;
2615 struct gtphub_peer_port *pp;
2616
2617 struct gsn_addr gsna;
2618 uint16_t port;
2619 gsn_addr_from_sockaddr(&gsna, &port, addr);
2620
2621 pa = gtphub_addr_find(bind, &gsna);
2622 if (!pa)
2623 return NULL;
2624
2625 pp = gtphub_addr_find_port(pa, port);
2626
2627 if (!pp)
2628 pp = gtphub_addr_add_port(pa, port);
2629
2630 return pp;
2631}
2632
2633
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002634/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2635 * resolution should be possible but failed, and 1 if resolution was
2636 * successful. *pp will be set to NULL if <1 is returned. */
2637static int gtphub_resolve_ggsn(struct gtphub *hub,
2638 struct gtp_packet_desc *p,
2639 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002640{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002641 *pp = NULL;
2642
2643 /* TODO determine from message type whether IEs should be present? */
2644
2645 int rc;
2646 const char *imsi_str;
2647 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2648 if (rc < 1)
2649 return rc;
2650 OSMO_ASSERT(imsi_str);
2651
2652 const char *apn_str;
2653 rc = get_ie_apn_str(p->ie, &apn_str);
2654 if (rc < 1)
2655 return rc;
2656 OSMO_ASSERT(apn_str);
2657
2658 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2659 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002660}
2661
2662
2663/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002664/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002665/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2666 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002667static int _osmo_getaddrinfo(struct addrinfo **result,
2668 uint16_t family, uint16_t type, uint8_t proto,
2669 const char *host, uint16_t port)
2670{
2671 struct addrinfo hints;
2672 char portbuf[16];
2673
2674 sprintf(portbuf, "%u", port);
2675 memset(&hints, '\0', sizeof(struct addrinfo));
2676 hints.ai_family = family;
2677 if (type == SOCK_RAW) {
2678 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2679 * SOCK_RAW and IPPROTO_GRE is used.
2680 */
2681 hints.ai_socktype = SOCK_DGRAM;
2682 hints.ai_protocol = IPPROTO_UDP;
2683 } else {
2684 hints.ai_socktype = type;
2685 hints.ai_protocol = proto;
2686 }
2687
2688 return getaddrinfo(host, portbuf, &hints, result);
2689}
2690
2691/* TODO move to osmocom/core/socket.c ? */
2692int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2693 uint16_t family, uint16_t type, uint8_t proto,
2694 const char *host, uint16_t port)
2695{
2696 struct addrinfo *res;
2697 int rc;
2698 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2699
2700 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002701 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002702 return -EINVAL;
2703 }
2704
2705 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2706 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2707 addr->l = res->ai_addrlen;
2708 freeaddrinfo(res);
2709
2710 return 0;
2711}
2712
2713int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2714 char *port_str, size_t port_str_len,
2715 const struct osmo_sockaddr *addr,
2716 int flags)
2717{
2718 int rc;
2719
2720 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2721 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2722 return -1;
2723 }
2724
2725 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002726 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2727 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002728 return -1;
2729 }
2730
2731 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2732 addr_str, addr_str_len,
2733 port_str, port_str_len,
2734 flags);
2735
2736 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002737 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2738 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2739 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002740
2741 return rc;
2742}
2743
2744const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2745 char *buf, size_t buf_len)
2746{
2747 const int portbuf_len = 6;
2748 OSMO_ASSERT(buf_len > portbuf_len);
2749 char *portbuf = buf + buf_len - portbuf_len;
2750 buf_len -= portbuf_len;
2751 if (osmo_sockaddr_to_strs(buf, buf_len,
2752 portbuf, portbuf_len,
2753 addr,
2754 NI_NUMERICHOST | NI_NUMERICSERV))
2755 return NULL;
2756
2757 char *pos = buf + strnlen(buf, buf_len-1);
2758 size_t len = buf_len - (pos - buf);
2759
2760 snprintf(pos, len, " port %s", portbuf);
2761 buf[buf_len-1] = '\0';
2762
2763 return buf;
2764}
2765
2766const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2767{
2768 static char buf[256];
2769 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2770 if (! result)
2771 return "(invalid)";
2772 return result;
2773}
2774
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002775int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2776 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002777{
2778 if (a == b)
2779 return 0;
2780 if (!a)
2781 return -1;
2782 if (!b)
2783 return 1;
2784 if (a->l != b->l) {
2785 /* Lengths are not the same, but determine the order. Will
2786 * anyone ever sort a list by osmo_sockaddr though...? */
2787 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2788 if (cmp == 0) {
2789 if (a->l < b->l)
2790 return -1;
2791 else
2792 return 1;
2793 }
2794 return cmp;
2795 }
2796 return memcmp(&a->a, &b->a, a->l);
2797}
2798
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002799void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2800 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002801{
2802 OSMO_ASSERT(src->l <= sizeof(dst->a));
2803 memcpy(&dst->a, &src->a, src->l);
2804 dst->l = src->l;
2805}