blob: e96088c74f4388253d7765198743a4b5989c21a9 [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 Hofmeyrd53c6042015-12-03 13:59:49 +010094 struct gtphub_tunnel *tun;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +010095 time_t timestamp;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020096 union gtpie_member *ie[GTPIE_SIZE];
97};
98
Neels Hofmeyr10fc0242015-12-01 00:23:45 +010099struct pending_delete {
100 struct llist_head entry;
101 struct expiring_item expiry_entry;
102
103 struct gtphub_tunnel *tun;
104 uint8_t teardown_ind;
105 uint8_t nsapi;
106};
107
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100108
109/* counters */
110
111enum gtphub_counters_io {
112 GTPH_CTR_PKTS_IN = 0,
113 GTPH_CTR_PKTS_OUT,
114 GTPH_CTR_BYTES_IN,
115 GTPH_CTR_BYTES_OUT
116};
117
118static const struct rate_ctr_desc gtphub_counters_io_desc[] = {
119 { "packets.in", "Packets ( In)" },
120 { "packets.out", "Packets (Out)" },
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100121 { "bytes.in", "Bytes ( In)" },
122 { "bytes.out", "Bytes (Out)" },
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100123};
124
125static const struct rate_ctr_group_desc gtphub_ctrg_io_desc = {
126 .group_name_prefix = "gtphub.bind",
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100127 .group_description = "I/O Statistics",
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100128 .num_ctr = ARRAY_SIZE(gtphub_counters_io_desc),
129 .ctr_desc = gtphub_counters_io_desc,
130 .class_id = OSMO_STATS_CLASS_GLOBAL,
131};
132
133
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200134void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
135{
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +0100136 *gsna = *src;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200137}
138
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200139int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
140 const struct osmo_sockaddr *sa)
141{
142 char addr_str[256];
143 char port_str[6];
144
145 if (osmo_sockaddr_to_strs(addr_str, sizeof(addr_str),
146 port_str, sizeof(port_str),
147 sa, (NI_NUMERICHOST | NI_NUMERICSERV))
148 != 0) {
149 return -1;
150 }
151
152 if (port)
153 *port = atoi(port_str);
154
155 return gsn_addr_from_str(gsna, addr_str);
156}
157
158int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
159{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100160 if ((!gsna) || (!numeric_addr_str))
161 return -1;
162
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200163 int af = AF_INET;
164 gsna->len = 4;
165 const char *pos = numeric_addr_str;
166 for (; *pos; pos++) {
167 if (*pos == ':') {
168 af = AF_INET6;
169 gsna->len = 16;
170 break;
171 }
172 }
173
174 int rc = inet_pton(af, numeric_addr_str, gsna->buf);
175 if (rc != 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100176 LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n",
177 numeric_addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200178 return -1;
179 }
180 return 0;
181}
182
183const char *gsn_addr_to_str(const struct gsn_addr *gsna)
184{
185 static char buf[INET6_ADDRSTRLEN + 1];
186 return gsn_addr_to_strb(gsna, buf, sizeof(buf));
187}
188
189const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
190 char *strbuf,
191 int strbuf_len)
192{
193 int af;
194 switch (gsna->len) {
195 case 4:
196 af = AF_INET;
197 break;
198 case 16:
199 af = AF_INET6;
200 break;
201 default:
202 return NULL;
203 }
204
205 const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
206 if (!r) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100207 LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:"
208 " %s: len=%d, buf=%s\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100209 strerror(errno),
210 (int)gsna->len,
211 osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200212 }
213 return r;
214}
215
216int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
217{
218 if (a == b)
219 return 1;
220 if ((!a) || (!b))
221 return 0;
222 if (a->len != b->len)
223 return 0;
224 return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
225}
226
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100227static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p,
228 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200229{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100230 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200231 return -1;
232
233 unsigned int len;
234 /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
235 if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
236 &len, gsna->buf, sizeof(gsna->buf))
237 != 0)
238 return -1;
239 gsna->len = len;
240 return 0;
241}
242
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100243static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p,
244 int idx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200245{
Neels Hofmeyre921e322015-11-11 00:45:50 +0100246 if (p->rc != GTP_RC_PDU_C)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200247 return -1;
248
249 int ie_idx;
250 ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx);
251
252 if (ie_idx < 0)
253 return -1;
254
255 struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
256 int ie_l = ntoh16(ie->l);
257 if (ie_l != gsna->len) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100258 LOG(LOGL_ERROR, "Not implemented:"
259 " replace an IE address of different size:"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200260 " replace %d with %d\n", (int)ie_l, (int)gsna->len);
261 return -1;
262 }
263
264 memcpy(ie->v, gsna->buf, (int)ie_l);
265 return 0;
266}
267
268/* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there.
269 */
270void validate_gtp0_header(struct gtp_packet_desc *p)
271{
272 const struct gtp0_header *pheader = &(p->data->gtp0.h);
273 p->rc = GTP_RC_UNKNOWN;
274 p->header_len = 0;
275
276 OSMO_ASSERT(p->data_len >= 1);
277 OSMO_ASSERT(p->version == 0);
278
279 if (p->data_len < GTP0_HEADER_SIZE) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100280 LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200281 p->rc = GTP_RC_TOOSHORT;
282 return;
283 }
284
285 p->type = ntoh8(pheader->type);
286 p->seq = ntoh16(pheader->seq);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100287 p->header_tei_rx = 0; /* TODO */
288 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200289
290 if (p->data_len == GTP0_HEADER_SIZE) {
291 p->rc = GTP_RC_TINY;
292 p->header_len = GTP0_HEADER_SIZE;
293 return;
294 }
295
296 /* Check packet length field versus length of packet */
297 if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100298 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
299 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100300 GTP0_HEADER_SIZE, (int)ntoh16(pheader->length),
301 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200302 p->rc = GTP_RC_TOOSHORT;
303 return;
304 }
305
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100306 LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200307 p->header_len = GTP0_HEADER_SIZE;
Neels Hofmeyre921e322015-11-11 00:45:50 +0100308 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200309}
310
311/* Validate GTP version 1 data, and update p->rc with the result, as well as
312 * p->header_len in case of a valid header. */
313void validate_gtp1_header(struct gtp_packet_desc *p)
314{
315 const struct gtp1_header_long *pheader = &(p->data->gtp1l.h);
316 p->rc = GTP_RC_UNKNOWN;
317 p->header_len = 0;
318
319 OSMO_ASSERT(p->data_len >= 1);
320 OSMO_ASSERT(p->version == 1);
321
322 if ((p->data_len < GTP1_HEADER_SIZE_LONG)
323 && (p->data_len != GTP1_HEADER_SIZE_SHORT)){
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100324 LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200325 p->rc = GTP_RC_TOOSHORT;
326 return;
327 }
328
329 p->type = ntoh8(pheader->type);
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100330 p->header_tei_rx = ntoh32(pheader->tei);
331 p->header_tei = p->header_tei_rx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200332 p->seq = ntoh16(pheader->seq);
333
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100334 LOG(LOGL_DEBUG, "GTPv1\n"
335 "| type = %" PRIu8 " 0x%02" PRIx8 "\n"
336 "| length = %" PRIu16 " 0x%04" PRIx16 "\n"
337 "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n"
338 "| seq = %" PRIu16 " 0x%04" PRIx16 "\n"
339 "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n"
340 "| next = %" PRIu8 " 0x%02" PRIx8 "\n",
341 p->type, p->type,
342 ntoh16(pheader->length), ntoh16(pheader->length),
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100343 p->header_tei_rx, p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100344 p->seq, p->seq,
345 pheader->npdu, pheader->npdu,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200346 pheader->next, pheader->next);
347
348 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
349 p->rc = GTP_RC_TINY;
350 p->header_len = GTP1_HEADER_SIZE_SHORT;
351 return;
352 }
353
354 /* Check packet length field versus length of packet */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100355 int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT;
356 if (p->data_len != announced_len) {
357 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
358 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100359 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
360 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200361 p->rc = GTP_RC_TOOSHORT;
362 return;
363 }
364
Neels Hofmeyre921e322015-11-11 00:45:50 +0100365 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200366 p->header_len = GTP1_HEADER_SIZE_LONG;
367}
368
369/* Examine whether p->data of size p->data_len has a valid GTP header. Set
370 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
371 * gtp_rc). p->data must point at a buffer with p->data_len set. */
372void validate_gtp_header(struct gtp_packet_desc *p)
373{
374 p->rc = GTP_RC_UNKNOWN;
375
376 /* Need at least 1 byte in order to check version */
377 if (p->data_len < 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100378 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n",
379 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200380 p->rc = GTP_RC_TOOSHORT;
381 return;
382 }
383
384 p->version = p->data->flags >> 5;
385
386 switch (p->version) {
387 case 0:
388 validate_gtp0_header(p);
389 break;
390 case 1:
391 validate_gtp1_header(p);
392 break;
393 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100394 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200395 p->rc = GTP_RC_UNSUPPORTED_VERSION;
396 break;
397 }
398}
399
400
401/* Return the value of the i'th IMSI IEI by copying to *imsi.
402 * The first IEI is reached by passing i = 0.
403 * imsi must point at allocated space of (at least) 8 bytes.
404 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100405static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200406{
407 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
408}
409
410/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100411static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200412{
413 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
414}
415
416static char imsi_digit_to_char(uint8_t nibble)
417{
418 nibble &= 0x0f;
419 if (nibble > 9)
420 return (nibble == 0x0f) ? '\0' : '?';
421 return '0' + nibble;
422}
423
424/* Return a human readable IMSI string, in a static buffer.
425 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100426static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200427{
428 static char str[17];
429 int i;
430
431 for (i = 0; i < 8; i++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +0100432 char c;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100433 c = imsi_digit_to_char(imsi[i]);
434 if (c == '?')
435 return -1;
436 str[2*i] = c;
437
438 c = imsi_digit_to_char(imsi[i] >> 4);
439 if (c == '?')
440 return -1;
441 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200442 }
443 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100444 *imsi_str = str;
445 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200446}
447
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100448/* Return 0 if not present, 1 if present and decoded successfully, -1 if
449 * present but cannot be decoded. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100450static int get_ie_imsi_str(union gtpie_member *ie[], int i,
451 const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100452{
453 uint8_t imsi_buf[8];
454 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100455 return 0;
456 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100457}
458
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100459/* Return 0 if not present, 1 if present and decoded successfully, -1 if
460 * present but cannot be decoded. */
461static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100462{
463 static char apn_buf[GSM_APN_LENGTH];
464 unsigned int len;
465 if (gtpie_gettlv(ie, GTPIE_APN, 0,
466 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100467 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100468
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100469 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100470 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
471 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100472 return -1;
473 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100474
475 if (len > (sizeof(apn_buf) - 1))
476 len = sizeof(apn_buf) - 1;
477 apn_buf[len] = '\0';
478
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100479 *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
480 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100481 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
482 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100483 return -1;
484 }
485 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100486}
487
488
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200489/* Validate header, and index information elements. Write decoded packet
490 * information to *res. res->data will point at the given data buffer. On
491 * error, p->rc is set <= 0 (see enum gtp_rc). */
492static void gtp_decode(const uint8_t *data, int data_len,
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100493 unsigned int from_side_idx,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200494 unsigned int from_plane_idx,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100495 struct gtp_packet_desc *res,
496 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200497{
498 ZERO_STRUCT(res);
499 res->data = (union gtp_packet*)data;
500 res->data_len = data_len;
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100501 res->side_idx = from_side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200502 res->plane_idx = from_plane_idx;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100503 res->timestamp = now;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200504
505 validate_gtp_header(res);
506
Neels Hofmeyr87c83d02015-12-03 11:25:27 +0100507 if (res->rc <= 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200508 return;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200509
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100510 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200511
Neels Hofmeyre921e322015-11-11 00:45:50 +0100512 if (from_plane_idx == GTPH_PLANE_USER) {
513 res->rc = GTP_RC_PDU_U;
514 return;
515 }
516
517 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100518 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200519 return;
520 }
521
522 if (gtpie_decaps(res->ie, res->version,
523 (void*)(data + res->header_len),
524 res->data_len - res->header_len) != 0) {
525 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100526 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
527 " Dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200528 return;
529 }
530
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100531#if 1
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100532 /* TODO if (<loglevel is debug>) { ...
533 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200534 int i;
535
536 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100537 const char *imsi;
538 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200539 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100540 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200541 }
542
543 for (i = 0; i < 10; i++) {
544 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100545 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200546 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100547 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200548 }
549
550 for (i = 0; i < 2; i++) {
551 struct gsn_addr addr;
552 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100553 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200554 }
555
556 for (i = 0; i < 10; i++) {
557 uint32_t tei;
558 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
559 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100560 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200561 tei, tei);
562 }
563
564 for (i = 0; i < 10; i++) {
565 uint32_t tei;
566 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
567 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100568 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200569 tei, tei);
570 }
571#endif
572}
573
574
575/* expiry */
576
577void expiry_init(struct expiry *exq, int expiry_in_seconds)
578{
579 ZERO_STRUCT(exq);
580 exq->expiry_in_seconds = expiry_in_seconds;
581 INIT_LLIST_HEAD(&exq->items);
582}
583
584void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
585{
586 item->expiry = now + exq->expiry_in_seconds;
587
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +0100588 OSMO_ASSERT(llist_empty(&exq->items)
589 || (item->expiry
590 >= llist_last(&exq->items, struct expiring_item, entry)->expiry));
591
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200592 /* Add/move to the tail to always sort by expiry, ascending. */
593 llist_del(&item->entry);
594 llist_add_tail(&item->entry, &exq->items);
595}
596
597int expiry_tick(struct expiry *exq, time_t now)
598{
599 int expired = 0;
600 struct expiring_item *m, *n;
601 llist_for_each_entry_safe(m, n, &exq->items, entry) {
602 if (m->expiry <= now) {
603 expiring_item_del(m);
604 expired ++;
605 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200606 /* The items are added sorted by expiry. So when we hit
607 * an unexpired entry, only more unexpired ones will
608 * follow. */
609 break;
610 }
611 }
612 return expired;
613}
614
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100615void expiry_clear(struct expiry *exq)
616{
617 struct expiring_item *m, *n;
618 llist_for_each_entry_safe(m, n, &exq->items, entry) {
619 expiring_item_del(m);
620 }
621}
622
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200623void expiring_item_init(struct expiring_item *item)
624{
625 ZERO_STRUCT(item);
626 INIT_LLIST_HEAD(&item->entry);
627}
628
629void expiring_item_del(struct expiring_item *item)
630{
631 OSMO_ASSERT(item);
632 llist_del(&item->entry);
633 INIT_LLIST_HEAD(&item->entry);
634 if (item->del_cb) {
635 /* avoid loops */
636 del_cb_t del_cb = item->del_cb;
637 item->del_cb = 0;
638 (del_cb)(item);
639 }
640}
641
642
643/* nr_map, nr_pool */
644
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100645void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200646{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100647 *pool = (struct nr_pool){
648 .nr_min = nr_min,
649 .nr_max = nr_max,
650 .last_nr = nr_max
651 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200652}
653
654nr_t nr_pool_next(struct nr_pool *pool)
655{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100656 if (pool->last_nr >= pool->nr_max)
657 pool->last_nr = pool->nr_min;
658 else
659 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200660
661 return pool->last_nr;
662}
663
664void nr_map_init(struct nr_map *map, struct nr_pool *pool,
665 struct expiry *exq)
666{
667 ZERO_STRUCT(map);
668 map->pool = pool;
669 map->add_items_to_expiry = exq;
670 INIT_LLIST_HEAD(&map->mappings);
671}
672
673void nr_mapping_init(struct nr_mapping *m)
674{
675 ZERO_STRUCT(m);
676 INIT_LLIST_HEAD(&m->entry);
677 expiring_item_init(&m->expiry_entry);
678}
679
680void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
681{
682 /* Generate a mapped number */
683 mapping->repl = nr_pool_next(map->pool);
684
685 /* Add to the tail to always yield a list sorted by expiry, in
686 * ascending order. */
687 llist_add_tail(&mapping->entry, &map->mappings);
Neels Hofmeyr508514c2015-11-24 13:30:38 +0100688 nr_map_refresh(map, mapping, now);
689}
690
691void nr_map_refresh(struct nr_map *map, struct nr_mapping *mapping, time_t now)
692{
693 if (!map->add_items_to_expiry)
694 return;
695 expiry_add(map->add_items_to_expiry,
696 &mapping->expiry_entry,
697 now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200698}
699
700void nr_map_clear(struct nr_map *map)
701{
702 struct nr_mapping *m;
703 struct nr_mapping *n;
704 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
705 nr_mapping_del(m);
706 }
707}
708
709int nr_map_empty(const struct nr_map *map)
710{
711 return llist_empty(&map->mappings);
712}
713
714struct nr_mapping *nr_map_get(const struct nr_map *map,
715 void *origin, nr_t nr_orig)
716{
717 struct nr_mapping *mapping;
718 llist_for_each_entry(mapping, &map->mappings, entry) {
719 if ((mapping->origin == origin)
720 && (mapping->orig == nr_orig))
721 return mapping;
722 }
723 /* Not found. */
724 return NULL;
725}
726
727struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
728{
729 struct nr_mapping *mapping;
730 llist_for_each_entry(mapping, &map->mappings, entry) {
731 if (mapping->repl == nr_repl) {
732 return mapping;
733 }
734 }
735 /* Not found. */
736 return NULL;
737}
738
739void nr_mapping_del(struct nr_mapping *mapping)
740{
741 OSMO_ASSERT(mapping);
742 llist_del(&mapping->entry);
743 INIT_LLIST_HEAD(&mapping->entry);
744 expiring_item_del(&mapping->expiry_entry);
745}
746
747
748/* gtphub */
749
750const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
751 "CTRL",
752 "USER",
753};
754
755const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
756 2123,
757 2152,
758};
759
Neels Hofmeyra9905a52015-11-29 23:49:48 +0100760const char* const gtphub_side_idx_names[GTPH_SIDE_N] = {
761 "SGSN",
762 "GGSN",
763};
764
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200765time_t gtphub_now(void)
766{
767 struct timespec now_tp;
768 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
769 return now_tp.tv_sec;
770}
771
772/* Remove a gtphub_peer from its list and free it. */
773static void gtphub_peer_del(struct gtphub_peer *peer)
774{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100775 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200776 nr_map_clear(&peer->seq_map);
777 llist_del(&peer->entry);
778 talloc_free(peer);
779}
780
781static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
782{
783 OSMO_ASSERT(llist_empty(&pa->ports));
784 llist_del(&pa->entry);
785 talloc_free(pa);
786}
787
788static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
789{
790 OSMO_ASSERT(pp->ref_count == 0);
791 llist_del(&pp->entry);
792 talloc_free(pp);
793}
794
795/* From the information in the gtp_packet_desc, return the address of a GGSN.
796 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100797static int gtphub_resolve_ggsn(struct gtphub *hub,
798 struct gtp_packet_desc *p,
799 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200800
801/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100802struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
803 const char *imsi_str,
804 const char *apn_ni_str);
805int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200806
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200807static void gtphub_zero(struct gtphub *hub)
808{
809 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100810 INIT_LLIST_HEAD(&hub->ggsn_lookups);
811 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200812}
813
814static int gtphub_sock_init(struct osmo_fd *ofd,
815 const struct gtphub_cfg_addr *addr,
816 osmo_fd_cb_t cb,
817 void *data,
818 int ofd_id)
819{
820 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100821 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200822 return -1;
823 }
824 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100825 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200826 return -1;
827 }
828
829 ofd->when = BSC_FD_READ;
830 ofd->cb = cb;
831 ofd->data = data;
832 ofd->priv_nr = ofd_id;
833
834 int rc;
835 rc = osmo_sock_init_ofd(ofd,
836 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
837 addr->addr_str, addr->port,
838 OSMO_SOCK_F_BIND);
839 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100840 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
841 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200842 return -1;
843 }
844
845 return 0;
846}
847
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100848static void gtphub_sock_close(struct osmo_fd *ofd)
849{
850 close(ofd->fd);
851 osmo_fd_unregister(ofd);
852 ofd->cb = NULL;
853}
854
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200855static void gtphub_bind_init(struct gtphub_bind *b)
856{
857 ZERO_STRUCT(b);
858
859 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100860
861 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
862 &gtphub_ctrg_io_desc, 0);
863 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200864}
865
866static int gtphub_bind_start(struct gtphub_bind *b,
867 const struct gtphub_cfg_bind *cfg,
868 osmo_fd_cb_t cb, void *cb_data,
869 unsigned int ofd_id)
870{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100871 LOG(LOGL_DEBUG, "Starting bind %s\n", b->label);
872 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0) {
873 LOG(LOGL_FATAL, "Invalid bind address for %s: %s\n",
874 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200875 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100876 }
877 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0) {
878 LOG(LOGL_FATAL, "Cannot bind for %s: %s\n",
879 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200880 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100881 }
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100882 b->local_port = cfg->bind.port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200883 return 0;
884}
885
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100886static void gtphub_bind_free(struct gtphub_bind *b)
887{
888 OSMO_ASSERT(llist_empty(&b->peers));
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100889 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100890}
891
892static void gtphub_bind_stop(struct gtphub_bind *b) {
893 gtphub_sock_close(&b->ofd);
894 gtphub_bind_free(b);
895}
896
Neels Hofmeyr40348972015-11-17 12:22:28 +0100897/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200898 * Return the number of bytes read, zero on error. */
899static int gtphub_read(const struct osmo_fd *from,
900 struct osmo_sockaddr *from_addr,
901 uint8_t *buf, size_t buf_len)
902{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100903 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200904
Neels Hofmeyr40348972015-11-17 12:22:28 +0100905 /* recvfrom requires the available length set in *from_addr_len. */
906 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200907 errno = 0;
908 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100909 (struct sockaddr*)&from_addr->a,
910 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200911 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
912 * is not truncated. Then maybe reduce buf's size. */
913
914 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100915 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
916 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200917 return 0;
918 }
919
Neels Hofmeyr40348972015-11-17 12:22:28 +0100920 LOG(LOGL_DEBUG, "Received %d bytes from %s\n%s\n",
921 (int)received, osmo_sockaddr_to_str(from_addr),
922 osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200923
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200924 return received;
925}
926
927inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
928{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100929 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200930 OSMO_ASSERT(pp->ref_count < UINT_MAX);
931 pp->ref_count++;
932}
933
934inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
935{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100936 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200937 OSMO_ASSERT(pp->ref_count > 0);
938 pp->ref_count--;
939}
940
941inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
942{
943 OSMO_ASSERT(p->version == 1);
944 p->data->gtp1l.h.seq = hton16(seq);
945 p->seq = seq;
946}
947
948inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
949{
950 OSMO_ASSERT(p->version == 1);
951 p->data->gtp1l.h.tei = hton32(tei);
952 p->header_tei = tei;
953}
954
955static void gtphub_mapping_del_cb(struct expiring_item *expi);
956
957static struct nr_mapping *gtphub_mapping_new()
958{
959 struct nr_mapping *nrm;
960 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
961 OSMO_ASSERT(nrm);
962
963 nr_mapping_init(nrm);
964 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
965 return nrm;
966}
967
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100968
969#define APPEND(args...) \
970 l = snprintf(pos, left, args); \
971 pos += l; \
972 left -= l
973
974static const char *gtphub_tunnel_side_str(struct gtphub_tunnel *tun,
975 int side_idx)
976{
977 static char buf[256];
978 char *pos = buf;
979 int left = sizeof(buf);
980 int l;
981
982 struct gtphub_tunnel_endpoint *c, *u;
983 c = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
984 u = &tun->endpoint[side_idx][GTPH_PLANE_USER];
985
986 /* print both only if they differ. */
987 if (!c->peer) {
988 APPEND("(uninitialized)");
989 } else {
990 APPEND("%s", gsn_addr_to_str(&c->peer->peer_addr->addr));
991 }
992
993 if (!u->peer) {
994 if (c->peer) {
995 APPEND(" / (uninitialized)");
996 }
997 } else if ((!c->peer)
998 || (!gsn_addr_same(&u->peer->peer_addr->addr,
999 &c->peer->peer_addr->addr))) {
1000 APPEND(" / %s", gsn_addr_to_str(&u->peer->peer_addr->addr));
1001 }
1002
1003 APPEND(" (TEI C %x=%x / U %x=%x)",
1004 c->tei_orig, c->tei_repl,
1005 u->tei_orig, u->tei_repl);
1006 return buf;
1007}
1008
1009const char *gtphub_tunnel_str(struct gtphub_tunnel *tun)
1010{
1011 static char buf[512];
1012 char *pos = buf;
1013 int left = sizeof(buf);
1014 int l;
1015
1016 APPEND("%s", gtphub_tunnel_side_str(tun, GTPH_SIDE_SGSN));
1017 APPEND(" <-> %s", gtphub_tunnel_side_str(tun, GTPH_SIDE_GGSN));
1018
1019 return buf;
1020}
1021
1022#undef APPEND
1023
1024void gtphub_tunnel_endpoint_set_peer(struct gtphub_tunnel_endpoint *te,
1025 struct gtphub_peer_port *pp)
1026{
1027 if (te->peer)
1028 gtphub_port_ref_count_dec(te->peer);
1029 te->peer = pp;
1030 if (te->peer)
1031 gtphub_port_ref_count_inc(te->peer);
1032}
1033
1034int gtphub_tunnel_complete(struct gtphub_tunnel *tun)
1035{
1036 if (!tun)
1037 return 0;
1038 int side_idx;
1039 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001040 for_each_side_and_plane(side_idx, plane_idx) {
1041 struct gtphub_tunnel_endpoint *te =
1042 &tun->endpoint[side_idx][plane_idx];
1043 if (!(te->peer && te->tei_orig && te->tei_repl))
1044 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001045 }
1046 return 1;
1047}
1048
1049static void gtphub_tunnel_del_cb(struct expiring_item *expi)
1050{
1051 struct gtphub_tunnel *tun = container_of(expi,
1052 struct gtphub_tunnel,
1053 expiry_entry);
1054 LOG(LOGL_DEBUG, "expired: %s\n", gtphub_tunnel_str(tun));
1055
1056 llist_del(&tun->entry);
1057 INIT_LLIST_HEAD(&tun->entry); /* mark unused */
1058
1059 expi->del_cb = 0; /* avoid recursion loops */
1060 expiring_item_del(&tun->expiry_entry); /* usually already done, but make sure. */
1061
1062 int side_idx;
1063 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001064 for_each_side_and_plane(side_idx, plane_idx) {
1065 /* clear ref count */
1066 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1067 NULL);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001068 }
1069
1070 talloc_free(tun);
1071}
1072
1073static struct gtphub_tunnel *gtphub_tunnel_new()
1074{
1075 struct gtphub_tunnel *tun;
1076 tun = talloc_zero(osmo_gtphub_ctx, struct gtphub_tunnel);
1077 OSMO_ASSERT(tun);
1078
1079 INIT_LLIST_HEAD(&tun->entry);
1080 expiring_item_init(&tun->expiry_entry);
1081
1082 tun->expiry_entry.del_cb = gtphub_tunnel_del_cb;
1083 return tun;
1084}
1085
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001086static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
1087 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001088{
1089 if (llist_empty(&peer->addresses))
1090 return "(addressless)";
1091
1092 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
1093 struct gtphub_peer_addr,
1094 entry);
1095 return gsn_addr_to_strb(&a->addr, buf, buflen);
1096}
1097
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001098static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
1099 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001100{
1101 if (!port)
1102 return "(null port)";
1103
1104 snprintf(buf, buflen, "%s port %d",
1105 gsn_addr_to_str(&port->peer_addr->addr),
1106 (int)port->port);
1107 return buf;
1108}
1109
1110const char *gtphub_peer_str(struct gtphub_peer *peer)
1111{
1112 static char buf[256];
1113 return gtphub_peer_strb(peer, buf, sizeof(buf));
1114}
1115
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001116const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001117{
1118 static char buf[256];
1119 return gtphub_port_strb(port, buf, sizeof(buf));
1120}
1121
1122static const char *gtphub_port_str2(struct gtphub_peer_port *port)
1123{
1124 static char buf[256];
1125 return gtphub_port_strb(port, buf, sizeof(buf));
1126}
1127
1128static void gtphub_mapping_del_cb(struct expiring_item *expi)
1129{
1130 expi->del_cb = 0; /* avoid recursion loops */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001131 expiring_item_del(expi); /* usually already done, but make sure. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001132
1133 struct nr_mapping *nrm = container_of(expi,
1134 struct nr_mapping,
1135 expiry_entry);
1136 llist_del(&nrm->entry);
1137 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
1138
1139 /* Just for log */
1140 struct gtphub_peer_port *from = nrm->origin;
1141 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001142 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001143 (int)nrm->expiry_entry.expiry,
1144 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001145 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001146
1147 gtphub_port_ref_count_dec(from);
1148
1149 talloc_free(nrm);
1150}
1151
1152static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
1153 struct gtphub_peer_port *from,
1154 nr_t orig_nr,
1155 time_t now)
1156{
1157 struct nr_mapping *nrm;
1158
1159 nrm = nr_map_get(map, from, orig_nr);
1160
1161 if (!nrm) {
1162 nrm = gtphub_mapping_new();
1163 nrm->orig = orig_nr;
1164 nrm->origin = from;
1165 nr_map_add(map, nrm, now);
1166 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001167 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001168 gtphub_port_str(from),
1169 (int)(nrm->orig), (int)(nrm->repl));
1170 } else {
Neels Hofmeyr508514c2015-11-24 13:30:38 +01001171 nr_map_refresh(map, nrm, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001172 }
1173
1174 OSMO_ASSERT(nrm);
1175 return nrm;
1176}
1177
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001178static void gtphub_map_seq(struct gtp_packet_desc *p,
1179 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001180 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001181{
1182 /* Store a mapping in to_peer's map, so when we later receive a GTP
1183 * packet back from to_peer, the seq nr can be unmapped back to its
1184 * origin (from_peer here). */
1185 struct nr_mapping *nrm;
1186 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001187 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001188
1189 /* Change the GTP packet to yield the new, mapped seq nr */
1190 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001191}
1192
1193static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1194 struct gtphub_peer_port *responding_port)
1195{
1196 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001197 struct nr_mapping *nrm =
1198 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1199 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001200 if (!nrm)
1201 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001202 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1203 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001204 set_seq(p, nrm->orig);
1205 return nrm->origin;
1206}
1207
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001208static int gtphub_check_mapped_tei(struct gtphub_tunnel_endpoint *new_te,
1209 struct gtphub_tunnel_endpoint *iterated_te,
1210 uint32_t *tei_min,
1211 uint32_t *tei_max)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001212{
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001213 if (new_te->tei_repl != iterated_te->tei_repl)
1214 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001215
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001216 /* new_te->tei_repl is already taken. Try to find one out of the known
1217 * range. */
1218
1219 if ((*tei_max) < 0xffffffff) {
1220 (*tei_max)++;
1221 new_te->tei_repl = *tei_max;
1222 return 1;
1223 } else if ((*tei_min) > 1) {
1224 (*tei_min)--;
1225 new_te->tei_repl = *tei_min;
1226 return 1;
1227 }
1228
1229 /* None seems to be available. */
1230 return 0;
1231}
1232
1233static int gtphub_check_reused_teis(struct gtphub *hub,
1234 struct gtphub_tunnel *new_tun)
1235{
1236 uint32_t tei_min = 0xffffffff;
1237 uint32_t tei_max = 0;
1238 int side_idx;
1239 int plane_idx;
1240 int side_idx2;
1241 int plane_idx2;
1242 struct gtphub_tunnel_endpoint *te;
1243 struct gtphub_tunnel_endpoint *te2;
1244
1245 struct gtphub_tunnel *tun, *ntun;
1246
1247 llist_for_each_entry_safe(tun, ntun, &hub->tunnels, entry) {
1248 if (tun == new_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001249 continue;
1250
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001251 /* Check whether the GSN sent a TEI that it is reusing from a
1252 * previous tunnel. */
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001253 int tun_continue = 0;
1254 for_each_side(side_idx) {
1255 for_each_plane(plane_idx) {
1256 te = &tun->endpoint[side_idx][plane_idx];
1257 te2 = &new_tun->endpoint[side_idx][plane_idx];
1258 if ((te->tei_orig != te2->tei_orig)
1259 || (!te->peer)
1260 || (!te2->peer)
1261 || !gsn_addr_same(&te->peer->peer_addr->addr,
1262 &te2->peer->peer_addr->addr))
1263 continue;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001264
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001265 /* The peer is reusing a TEI that I believe to
1266 * be part of another tunnel. The other tunnel
1267 * must be stale, then. */
1268 LOG(LOGL_NOTICE,
1269 "Expiring tunnel due to reused TEI:"
1270 " peer %s sent %s TEI %x,"
1271 " previously used by tunnel %s...\n",
1272 gtphub_port_str(te->peer),
1273 gtphub_plane_idx_names[plane_idx],
1274 te->tei_orig,
1275 gtphub_tunnel_str(tun));
1276 LOG(LOGL_NOTICE, "...while establishing tunnel %s\n",
1277 gtphub_tunnel_str(new_tun));
Neels Hofmeyr52c0bd32015-12-02 14:14:32 +01001278
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001279 expiring_item_del(&tun->expiry_entry);
1280 /* continue to find more matches. There shouldn't be
1281 * any, but let's make sure. However, tun is deleted,
1282 * so we need to skip to the next tunnel. */
1283 tun_continue = 1;
1284 break;
1285 }
1286 if (tun_continue)
1287 break;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001288 }
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001289 if (tun_continue)
1290 continue;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001291
1292 /* Check whether the mapped TEIs assigned to the endpoints are
1293 * used anywhere else. */
1294 for_each_side_and_plane(side_idx, plane_idx) {
1295 te = &tun->endpoint[side_idx][plane_idx];
1296 tei_min = (tei_min < te->tei_repl)? tei_min : te->tei_repl;
1297 tei_max = (tei_max > te->tei_repl)? tei_max : te->tei_repl;
1298
1299 for_each_side_and_plane(side_idx2, plane_idx2) {
1300 te2 = &new_tun->endpoint[side_idx2][plane_idx2];
1301 if (!gtphub_check_mapped_tei(te2, te, &tei_min, &tei_max)) {
1302 LOG(LOGL_ERROR,
1303 "No mapped TEI is readily available."
1304 " Searching for holes between occupied"
1305 " TEIs not implemented.");
1306 return 0;
1307 }
1308 }
1309 }
1310
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001311 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001312
1313 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001314}
1315
1316static void gtphub_tunnel_refresh(struct gtphub *hub,
1317 struct gtphub_tunnel *tun,
1318 time_t now)
1319{
1320 expiry_add(&hub->expire_slowly,
1321 &tun->expiry_entry,
1322 now);
1323}
1324
1325static struct gtphub_tunnel_endpoint *gtphub_unmap_tei(struct gtphub *hub,
1326 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001327 struct gtphub_peer_port *from,
1328 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001329{
1330 OSMO_ASSERT(from);
1331 int other_side = other_side_idx(p->side_idx);
1332
1333 struct gtphub_tunnel *tun;
1334 llist_for_each_entry(tun, &hub->tunnels, entry) {
1335 struct gtphub_tunnel_endpoint *te_from =
1336 &tun->endpoint[p->side_idx][p->plane_idx];
1337 struct gtphub_tunnel_endpoint *te_to =
1338 &tun->endpoint[other_side][p->plane_idx];
1339 if ((te_to->tei_repl == p->header_tei_rx)
Neels Hofmeyrfc1be3a2015-12-02 00:31:31 +01001340 && te_from->peer
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001341 && gsn_addr_same(&te_from->peer->peer_addr->addr,
1342 &from->peer_addr->addr)) {
1343 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001344 if (unmapped_from_tun)
1345 *unmapped_from_tun = tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001346 return te_to;
1347 }
1348 }
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001349
1350 if (unmapped_from_tun)
1351 *unmapped_from_tun = NULL;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001352 return NULL;
1353}
1354
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001355static void gtphub_map_restart_counter(struct gtphub *hub,
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001356 struct gtp_packet_desc *p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001357{
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001358 if (p->rc != GTP_RC_PDU_C)
1359 return;
1360
1361 int ie_idx;
1362 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1363 if (ie_idx < 0)
1364 return;
1365
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001366 /* Always send gtphub's own restart counter */
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001367 p->ie[ie_idx]->tv1.v = hton8(hub->restart_counter);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001368}
1369
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001370static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001371 struct gtphub_tunnel **unmapped_from_tun,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001372 struct gtphub *hub,
1373 struct gtp_packet_desc *p,
1374 struct gtphub_peer_port *from_port)
1375{
1376 OSMO_ASSERT(p->version == 1);
1377 *to_port_p = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001378 if (unmapped_from_tun)
1379 *unmapped_from_tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001380
1381 /* If the header's TEI is zero, no PDP context has been established
1382 * yet. If nonzero, a mapping should actually already exist for this
1383 * TEI, since it must have been announced in a PDP context creation. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001384 if (!p->header_tei_rx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001385 return 0;
1386
1387 /* to_peer has previously announced a TEI, which was stored and
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001388 * mapped in a tunnel struct. */
1389 struct gtphub_tunnel_endpoint *to;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001390 to = gtphub_unmap_tei(hub, p, from_port, unmapped_from_tun);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001391 if (!to) {
1392 LOG(LOGL_ERROR, "Received unknown TEI %" PRIx32 " from %s\n",
1393 p->header_tei_rx, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001394 return -1;
1395 }
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001396 OSMO_ASSERT(*unmapped_from_tun);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001397
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001398 uint32_t unmapped_tei = to->tei_orig;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001399 set_tei(p, unmapped_tei);
1400
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001401 LOG(LOGL_DEBUG, "Unmapped TEI coming from: %s\n",
1402 gtphub_tunnel_str(*unmapped_from_tun));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001403
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001404 /* May be NULL for an invalidated tunnel. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001405 *to_port_p = to->peer;
1406
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001407 return 0;
1408}
1409
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001410static int gtphub_handle_create_pdp_ctx(struct gtphub *hub,
1411 struct gtp_packet_desc *p,
1412 struct gtphub_peer_port *from_ctrl,
1413 struct gtphub_peer_port *to_ctrl)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001414{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001415 int plane_idx;
1416
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001417 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1418 plane_nrs_match_GSN_addr_IE_indices);
1419
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001420 struct gtphub_tunnel *tun = p->tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001421
1422 if (p->type == GTP_CREATE_PDP_REQ) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001423 if (p->side_idx != GTPH_SIDE_SGSN) {
1424 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001425 " Request from the GGSN side: %s",
1426 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001427 return -1;
1428 }
1429
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001430 if (tun) {
1431 LOG(LOGL_ERROR, "Not implemented: Received"
1432 " Create PDP Context Request for an already"
1433 " established tunnel:"
1434 " from %s, tunnel %s\n",
1435 gtphub_port_str(from_ctrl),
1436 gtphub_tunnel_str(p->tun));
1437 return -1;
1438 }
1439
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001440 /* A new tunnel. */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001441 p->tun = tun = gtphub_tunnel_new();
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001442 llist_add(&tun->entry, &hub->tunnels);
1443 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001444 /* The endpoint peers on this side (SGSN) will be set from IEs
1445 * below. Also set the GGSN Ctrl endpoint, for logging. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001446 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001447 to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001448 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001449 if (p->side_idx != GTPH_SIDE_GGSN) {
1450 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001451 " Response from the SGSN side: %s",
1452 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001453 return -1;
1454 }
1455
Neels Hofmeyr8c5b0732015-12-03 13:45:15 +01001456 /* The tunnel should already have been resolved from the header
1457 * TEI and be available in tun (== p->tun). Just fill in the
1458 * GSN Addresses below.*/
1459 OSMO_ASSERT(tun);
1460 OSMO_ASSERT(tun->endpoint[other_side_idx(p->side_idx)][GTPH_PLANE_CTRL].tei_repl
1461 == p->header_tei_rx);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001462 OSMO_ASSERT(to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001463 }
1464
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001465 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1466 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001467 unsigned int side_idx = p->side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001468
1469 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01001470 int rc;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001471 struct gsn_addr use_addr;
1472 uint16_t use_port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001473 uint32_t tei_from_ie;
1474 int ie_idx;
1475
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001476 /* Fetch GSN Address and TEI from IEs. As ensured by above
1477 * static asserts, plane_idx corresponds to the GSN Address IE
1478 * index (the first one = 0 = ctrl, second one = 1 = user). */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001479 rc = gsn_addr_get(&use_addr, p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001480 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001481 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1482 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001483 return -1;
1484 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001485 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001486 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001487 gsn_addr_to_str(&use_addr),
1488 use_addr.len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001489
1490 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1491 if (ie_idx < 0) {
1492 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001493 LOG(LOGL_ERROR,
1494 "Create PDP Context message invalid:"
1495 " missing IE %d\n",
1496 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001497 return -1;
1498 }
1499 tei_from_ie = 0;
1500 }
1501 else
1502 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1503
1504 /* Make sure an entry for this peer address with default port
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001505 * exists. */
1506 struct gtphub_peer_port *peer_from_ie;
1507 use_port = gtphub_plane_idx_default_port[plane_idx];
1508 peer_from_ie = gtphub_port_have(hub,
1509 &hub->to_gsns[side_idx][plane_idx],
1510 &use_addr, use_port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001511
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001512 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1513 peer_from_ie);
1514
Neels Hofmeyr59c1b642015-12-03 11:30:43 +01001515 if (!tei_from_ie &&
1516 !tun->endpoint[side_idx][plane_idx].tei_orig) {
1517 LOG(LOGL_ERROR,
1518 "Create PDP Context message omits %s TEI, but the"
1519 " no TEI has been announced for this tunnel: %s",
1520 gtphub_plane_idx_names[plane_idx],
1521 gtphub_tunnel_str(tun));
1522 return -1;
1523 }
1524
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001525 if (tei_from_ie) {
1526 /* Create TEI mapping and replace in GTP packet IE */
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01001527 uint32_t mapped_tei = nr_pool_next(&hub->tei_pool);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001528
1529 tun->endpoint[side_idx][plane_idx].tei_orig = tei_from_ie;
1530 tun->endpoint[side_idx][plane_idx].tei_repl = mapped_tei;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001531 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001532
Neels Hofmeyrcd865d62015-11-30 12:58:48 +01001533 if (!gtphub_check_reused_teis(hub, tun)) {
1534 /* It's highly unlikely that all TEIs are
1535 * taken. But the code looking for an unused
1536 * TEI is, at the time of writing this comment,
1537 * not able to find gaps in the TEI space. To
1538 * explicitly alert the user of this problem,
1539 * rather abort than carry on. */
1540 LOG(LOGL_FATAL, "TEI range exhausted. Cannot create TEI mapping, aborting.\n");
1541 abort();
1542 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001543 }
1544
1545 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001546 rc = gsn_addr_put(&hub->to_gsns[other_side_idx(side_idx)][plane_idx].local_addr,
1547 p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001548 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001549 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1550 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001551 return -1;
1552 }
1553 }
1554
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001555 if (p->type == GTP_CREATE_PDP_REQ) {
1556 LOG(LOGL_DEBUG, "New tunnel, first half: %s\n",
1557 gtphub_tunnel_str(tun));
Neels Hofmeyr005f1752015-11-30 12:19:11 +01001558 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001559 LOG(LOGL_DEBUG, "New tunnel: %s\n",
1560 gtphub_tunnel_str(tun));
1561 }
1562
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001563 return 0;
1564}
1565
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001566static void pending_delete_del_cb(struct expiring_item *expi)
1567{
1568 struct pending_delete *pd;
1569 pd = container_of(expi, struct pending_delete, expiry_entry);
1570
1571 llist_del(&pd->entry);
1572 INIT_LLIST_HEAD(&pd->entry);
1573
1574 pd->expiry_entry.del_cb = 0;
1575 expiring_item_del(&pd->expiry_entry);
1576
1577 talloc_free(pd);
1578}
1579
1580static struct pending_delete *pending_delete_new(void)
1581{
1582 struct pending_delete *pd = talloc_zero(osmo_gtphub_ctx, struct pending_delete);
1583 INIT_LLIST_HEAD(&pd->entry);
1584 expiring_item_init(&pd->expiry_entry);
1585 pd->expiry_entry.del_cb = pending_delete_del_cb;
1586 return pd;
1587}
1588
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001589static int gtphub_handle_delete_pdp_ctx(struct gtphub *hub,
1590 struct gtp_packet_desc *p,
1591 struct gtphub_peer_port *from_ctrl,
1592 struct gtphub_peer_port *to_ctrl)
1593{
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001594 struct gtphub_tunnel *known_tun = p->tun;
1595
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001596 if (p->type == GTP_DELETE_PDP_REQ) {
1597 if (!known_tun) {
1598 LOG(LOGL_ERROR, "Cannot find tunnel for Delete PDP Context Request.\n");
1599 return -1;
1600 }
1601
1602 /* Store the Delete Request until a successful Response is seen. */
1603 uint8_t teardown_ind;
1604 uint8_t nsapi;
1605
1606 if (gtpie_gettv1(p->ie, GTPIE_TEARDOWN, 0, &teardown_ind) != 0) {
1607 LOG(LOGL_ERROR, "Missing Teardown Ind IE in Delete PDP Context Request.\n");
1608 return -1;
1609 }
1610
1611 if (gtpie_gettv1(p->ie, GTPIE_NSAPI, 0, &nsapi) != 0) {
1612 LOG(LOGL_ERROR, "Missing NSAPI IE in Delete PDP Context Request.\n");
1613 return -1;
1614 }
1615
1616 struct pending_delete *pd = NULL;
1617
1618 struct pending_delete *pdi = NULL;
1619 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1620 if ((pdi->tun == known_tun)
1621 && (pdi->teardown_ind == teardown_ind)
1622 && (pdi->nsapi == nsapi)) {
1623 pd = pdi;
1624 break;
1625 }
1626 }
1627
1628 if (!pd) {
1629 pd = pending_delete_new();
1630 pd->tun = known_tun;
1631 pd->teardown_ind = teardown_ind;
1632 pd->nsapi = nsapi;
1633
1634 LOG(LOGL_DEBUG, "Tunnel delete pending: %s\n",
1635 gtphub_tunnel_str(known_tun));
1636 llist_add(&pd->entry, &hub->pending_deletes);
1637 }
1638
1639 /* Add or refresh timeout. */
1640 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1641
1642 /* If a pending_delete should expire before the response to
1643 * indicate success comes in, the responding peer will have the
1644 * tunnel deactivated, while the requesting peer gets no reply
1645 * and keeps the tunnel. The hope is that the requesting peer
1646 * will try again and get a useful response. */
1647 } else if (p->type == GTP_DELETE_PDP_RSP) {
1648 /* Find the Delete Request for this Response. */
1649 struct pending_delete *pd = NULL;
1650
1651 struct pending_delete *pdi;
1652 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1653 if (known_tun == pdi->tun) {
1654 pd = pdi;
1655 break;
1656 }
1657 }
1658
1659 if (!pd) {
1660 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1661 " Cannot find matching request.");
1662 /* If we delete the tunnel now, anyone can send a
1663 * Delete response to kill tunnels at will. */
1664 return -1;
1665 }
1666
1667 /* TODO handle teardown_ind and nsapi */
1668
1669 expiring_item_del(&pd->expiry_entry);
1670
1671 uint8_t cause;
1672 if (gtpie_gettv1(p->ie, GTPIE_CAUSE, 0, &cause) != 0) {
1673 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1674 " Missing Cause IE.");
1675 /* If we delete the tunnel now, at least one of the
1676 * peers may still think it is active. */
1677 return -1;
1678 }
1679
1680 if (cause != GTPCAUSE_ACC_REQ) {
1681 LOG(LOGL_NOTICE,
1682 "Delete PDP Context Response indicates failure;"
1683 "for %s\n",
1684 gtphub_tunnel_str(known_tun));
1685 return -1;
1686 }
1687
1688 LOG(LOGL_DEBUG, "Delete PDP Context: removing tunnel %s\n",
1689 gtphub_tunnel_str(known_tun));
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001690 p->tun = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001691 expiring_item_del(&known_tun->expiry_entry);
1692 }
1693
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001694 return 0;
1695}
1696
1697static int gtphub_handle_update_pdp_ctx(struct gtphub *hub,
1698 struct gtp_packet_desc *p,
1699 struct gtphub_peer_port *from_ctrl,
1700 struct gtphub_peer_port *to_ctrl)
1701{
1702 /* TODO */
1703 return 0;
1704}
1705
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001706/* Read GSN address IEs from p, and make sure these peer addresses exist in
1707 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1708 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1709 * the packet p. */
1710static int gtphub_handle_pdp_ctx(struct gtphub *hub,
1711 struct gtp_packet_desc *p,
1712 struct gtphub_peer_port *from_ctrl,
1713 struct gtphub_peer_port *to_ctrl)
1714{
1715 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1716
1717 switch (p->type) {
1718 case GTP_CREATE_PDP_REQ:
1719 case GTP_CREATE_PDP_RSP:
1720 return gtphub_handle_create_pdp_ctx(hub, p,
1721 from_ctrl, to_ctrl);
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001722
1723 case GTP_DELETE_PDP_REQ:
1724 case GTP_DELETE_PDP_RSP:
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001725 return gtphub_handle_delete_pdp_ctx(hub, p,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001726 from_ctrl, to_ctrl);
1727
1728 case GTP_UPDATE_PDP_REQ:
1729 case GTP_UPDATE_PDP_RSP:
1730 return gtphub_handle_update_pdp_ctx(hub, p,
1731 from_ctrl, to_ctrl);
1732
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001733 default:
1734 /* Nothing to do for this message type. */
1735 return 0;
1736 }
1737
1738}
1739
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001740static int gtphub_send_del_pdp_ctx(struct gtphub *hub,
1741 struct gtphub_tunnel *tun,
1742 int to_side)
1743{
1744 static uint8_t del_ctx_msg[16] = {
1745 0x32, /* GTP v1 flags */
1746 GTP_DELETE_PDP_REQ,
1747 0x00, 0x08, /* Length in network byte order */
1748 0x00, 0x00, 0x00, 0x00, /* TEI to be replaced */
1749 0, 0, /* Seq, to be replaced */
1750 0, 0, /* no extensions */
1751 0x13, 0xff, /* 19: Teardown ind = 1 */
1752 0x14, 0 /* 20: NSAPI = 0 */
1753 };
1754
1755 uint32_t *tei = (uint32_t*)&del_ctx_msg[4];
1756 uint16_t *seq = (uint16_t*)&del_ctx_msg[8];
1757
1758 struct gtphub_tunnel_endpoint *te =
1759 &tun->endpoint[to_side][GTPH_PLANE_CTRL];
1760
1761 if (! te->peer)
1762 return 0;
1763
1764 *tei = hton32(te->tei_orig);
1765 *seq = hton16(nr_pool_next(&te->peer->peer_addr->peer->seq_pool));
1766
1767 struct gtphub_bind *to_bind = &hub->to_gsns[to_side][GTPH_PLANE_CTRL];
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01001768 int rc = gtphub_write(&to_bind->ofd, &te->peer->sa,
1769 del_ctx_msg, sizeof(del_ctx_msg));
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001770 if (rc != 0) {
1771 LOG(LOGL_ERROR,
1772 "Failed to send out-of-band Delete PDP Context Request to %s\n",
1773 gtphub_port_str(te->peer));
1774 }
1775 return rc;
1776}
1777
1778/* Tell all peers on the other end of tunnels that PDP contexts are void. */
1779static void gtphub_restarted(struct gtphub *hub,
1780 struct gtp_packet_desc *p,
1781 struct gtphub_peer_port *pp)
1782{
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001783 LOG(LOGL_DEBUG, "Peer has restarted: %s\n",
1784 gtphub_port_str(pp));
1785
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001786 struct gtphub_tunnel *tun;
1787 llist_for_each_entry(tun, &hub->tunnels, entry) {
1788 int side_idx;
1789 for_each_side(side_idx) {
Neels Hofmeyrc6d51f52015-12-02 15:44:34 +01001790 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
1791 struct gtphub_tunnel_endpoint *te2 = &tun->endpoint[other_side_idx(side_idx)][GTPH_PLANE_CTRL];
1792 if ((!te->peer)
1793 || (!te2->tei_orig)
1794 || (pp->peer_addr->peer != te->peer->peer_addr->peer))
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001795 continue;
1796
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001797 LOG(LOGL_DEBUG, "Deleting tunnel due to peer restart: %s\n",
1798 gtphub_tunnel_str(tun));
1799
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001800 /* Send a Delete PDP Context Request to the
1801 * peer on the other side, remember the pending
1802 * delete and wait for the response to delete
1803 * the tunnel. Clear this side of the tunnel to
1804 * make sure it isn't used.
1805 *
1806 * Should the delete message send fail, or if no
1807 * response is received, this tunnel will expire. If
1808 * its TEIs come up in a new PDP Context Request, it
1809 * will be removed. If messages for this tunnel should
1810 * come in (from the not restarted side), they will be
1811 * dropped because the tunnel is rendered unusable. */
1812 gtphub_send_del_pdp_ctx(hub, tun, other_side_idx(side_idx));
1813
1814 struct pending_delete *pd;
1815 pd = pending_delete_new();
1816 pd->tun = tun;
1817 pd->teardown_ind = 0xff;
1818 pd->nsapi = 0;
1819 llist_add(&pd->entry, &hub->pending_deletes);
1820 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1821
1822 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_CTRL],
1823 NULL);
1824 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_USER],
1825 NULL);
1826 }
1827 }
1828}
1829
1830static int get_restart_count(struct gtp_packet_desc *p)
1831{
1832 int ie_idx;
1833 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1834 if (ie_idx < 0)
1835 return -1;
1836 return ntoh8(p->ie[ie_idx]->tv1.v);
1837}
1838
1839static void gtphub_check_restart_counter(struct gtphub *hub,
1840 struct gtp_packet_desc *p,
1841 struct gtphub_peer_port *from)
1842{
1843 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1844 * that doesn't match the peer's previously sent restart counter, clear
1845 * that peer and cancel PDP contexts. */
1846
1847 int restart = get_restart_count(p);
1848
1849 if ((restart < 0) || (restart > 255))
1850 return;
1851
1852 if ((from->last_restart_count >= 0) && (from->last_restart_count <= 255)) {
1853 if (from->last_restart_count != restart) {
1854 gtphub_restarted(hub, p, from);
1855 }
1856 }
1857
1858 from->last_restart_count = restart;
1859}
1860
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001861static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1862{
1863 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1864 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
1865 LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n",
1866 gtphub_plane_idx_names[plane_idx]);
1867
1868 if (!(what & BSC_FD_READ))
1869 return 0;
1870
1871 struct gtphub *hub = from_sgsns_ofd->data;
1872
1873 static uint8_t buf[4096];
1874 struct osmo_sockaddr from_addr;
1875 struct osmo_sockaddr to_addr;
1876 struct osmo_fd *to_ofd;
1877 int len;
1878 uint8_t *reply_buf;
1879
1880 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1881 if (len < 1)
1882 return 0;
1883
1884 len = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, &from_addr,
1885 buf, len, gtphub_now(),
1886 &reply_buf, &to_ofd, &to_addr);
1887 if (len < 1)
1888 return 0;
1889
1890 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
1891}
1892
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001893static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1894{
1895 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1896 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001897 LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n",
1898 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001899 if (!(what & BSC_FD_READ))
1900 return 0;
1901
1902 struct gtphub *hub = from_ggsns_ofd->data;
1903
1904 static uint8_t buf[4096];
1905 struct osmo_sockaddr from_addr;
1906 struct osmo_sockaddr to_addr;
1907 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001908 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001909 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001910
1911 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1912 if (len < 1)
1913 return 0;
1914
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001915 len = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, &from_addr,
1916 buf, len, gtphub_now(),
1917 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001918 if (len < 1)
1919 return 0;
1920
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001921 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001922}
1923
1924static int gtphub_unmap(struct gtphub *hub,
1925 struct gtp_packet_desc *p,
1926 struct gtphub_peer_port *from,
1927 struct gtphub_peer_port *to_proxy,
1928 struct gtphub_peer_port **final_unmapped,
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001929 struct gtphub_peer_port **unmapped_from_seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001930{
1931 /* Always (try to) unmap sequence and TEI numbers, which need to be
1932 * replaced in the packet. Either way, give precedence to the proxy, if
1933 * configured. */
1934
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001935 if (unmapped_from_seq)
1936 *unmapped_from_seq = NULL;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001937 if (final_unmapped)
1938 *final_unmapped = NULL;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001939 p->tun = NULL;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001940
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001941 struct gtphub_peer_port *from_seq = NULL;
1942 struct gtphub_peer_port *from_tei = NULL;
1943 struct gtphub_peer_port *unmapped = NULL;
1944
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001945 from_seq = gtphub_unmap_seq(p, from);
1946
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001947 if (gtphub_unmap_header_tei(&from_tei, &p->tun, hub, p, from) < 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001948 return -1;
1949
1950 struct gtphub_peer *from_peer = from->peer_addr->peer;
1951 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001952 LOG(LOGL_DEBUG,
1953 "Seq unmap and TEI unmap yield two different peers."
1954 " Using seq unmap."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001955 "(from %s %s: seq %d yields %s, tei %u yields %s)\n",
1956 gtphub_plane_idx_names[p->plane_idx],
1957 gtphub_peer_str(from_peer),
1958 (int)p->seq,
1959 gtphub_port_str(from_seq),
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001960 (unsigned int)p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001961 gtphub_port_str2(from_tei)
1962 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001963 }
1964 unmapped = (from_seq? from_seq : from_tei);
1965
1966 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001967 LOG(LOGL_NOTICE,
1968 "Unmap yields a different peer than the configured proxy."
1969 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001970 " unmapped: %s proxy: %s\n",
1971 gtphub_port_str(unmapped),
1972 gtphub_port_str2(to_proxy)
1973 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001974 }
1975 unmapped = (to_proxy? to_proxy : unmapped);
1976
1977 if (!unmapped) {
1978 /* Return no error, but returned pointers are all NULL. */
1979 return 0;
1980 }
1981
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001982 if (unmapped_from_seq)
1983 *unmapped_from_seq = from_seq;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001984 if (final_unmapped)
1985 *final_unmapped = unmapped;
1986 return 0;
1987}
1988
1989static int gsn_addr_to_sockaddr(struct gsn_addr *src,
1990 uint16_t port,
1991 struct osmo_sockaddr *dst)
1992{
1993 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
1994}
1995
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001996/* If p is an Echo request, replace p's data with the matching response and
1997 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
1998 * detected. */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001999static int gtphub_handle_echo_req(struct gtphub *hub, struct gtp_packet_desc *p,
2000 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002001{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002002 if (p->type != GTP_ECHO_REQ)
2003 return 0;
2004
2005 static uint8_t echo_response_data[14] = {
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002006 0x32, /* GTP v1 flags */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002007 GTP_ECHO_RSP,
2008 0x00, 14 - 8, /* Length in network byte order */
2009 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
2010 0, 0, /* Seq, to be replaced */
2011 0, 0, /* no extensions */
2012 0x0e, /* Recovery IE */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002013 0 /* Restart counter, to be replaced */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002014 };
2015 uint16_t *seq = (uint16_t*)&echo_response_data[8];
2016 uint8_t *recovery = &echo_response_data[13];
2017
2018 *seq = hton16(p->seq);
2019 *recovery = hub->restart_counter;
2020
2021 *reply_buf = echo_response_data;
2022
2023 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002024}
2025
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002026struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2027 const struct osmo_sockaddr *addr);
2028
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002029/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
2030 * address to forward to. Return a pointer to the osmo_fd, but copy the
2031 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
2032 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
2033 * bytes to forward, 0 or less on failure. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002034int gtphub_handle_buf(struct gtphub *hub,
2035 unsigned int side_idx,
2036 unsigned int plane_idx,
2037 const struct osmo_sockaddr *from_addr,
2038 uint8_t *buf,
2039 size_t received,
2040 time_t now,
2041 uint8_t **reply_buf,
2042 struct osmo_fd **to_ofd,
2043 struct osmo_sockaddr *to_addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002044{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002045 struct gtphub_bind *from_bind = &hub->to_gsns[side_idx][plane_idx];
2046 struct gtphub_bind *to_bind = &hub->to_gsns[other_side_idx(side_idx)][plane_idx];
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002047
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002048 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
2049 received);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002050 LOG(LOGL_DEBUG, "%s rx %s from %s %s\n",
2051 (side_idx == GTPH_SIDE_GGSN)? "<-" : "->",
Neels Hofmeyre921e322015-11-11 00:45:50 +01002052 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002053 gtphub_side_idx_names[side_idx],
Neels Hofmeyre921e322015-11-11 00:45:50 +01002054 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002055
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01002056 struct gtp_packet_desc p;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002057 gtp_decode(buf, received, side_idx, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002058
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002059 if (p.rc <= 0) {
2060 LOG(LOGL_ERROR, "INVALID: dropping GTP packet from %s %s %s\n",
2061 gtphub_side_idx_names[side_idx],
2062 gtphub_plane_idx_names[plane_idx],
2063 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002064 return -1;
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002065 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002066
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002067 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2068
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002069 int reply_len;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002070 reply_len = gtphub_handle_echo_req(hub, &p, reply_buf);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002071 if (reply_len > 0) {
2072 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002073 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002074 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002075
2076 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2077 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2078 reply_len);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002079 LOG(LOGL_DEBUG, "%s Echo response to %s: %d bytes to %s\n",
2080 (side_idx == GTPH_SIDE_GGSN)? "-->" : "<--",
2081 gtphub_side_idx_names[side_idx],
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002082 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002083 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002084 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002085 if (reply_len < 0)
2086 return -1;
2087
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002088 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002089
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002090 /* If a proxy is configured, check that it's indeed that proxy talking
2091 * to us. A proxy is a forced 1:1 connection, e.g. to another gtphub,
2092 * so no-one else is allowed to talk to us from that side. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002093 struct gtphub_peer_port *from_peer = hub->proxy[side_idx][plane_idx];
2094 if (from_peer) {
2095 if (osmo_sockaddr_cmp(&from_peer->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002096 LOG(LOGL_ERROR,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002097 "Rejecting: %s proxy configured, but GTP packet"
2098 " received on %s bind is from another sender:"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002099 " proxy: %s sender: %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002100 gtphub_side_idx_names[side_idx],
2101 gtphub_side_idx_names[side_idx],
2102 gtphub_port_str(from_peer),
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002103 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002104 return -1;
2105 }
2106 }
2107
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002108 if (!from_peer) {
2109 /* Find or create a peer with a matching address. The sender's
2110 * port may in fact differ. */
2111 from_peer = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002112 }
2113
2114 /* If any PDP context has been created, we already have an entry for
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002115 * this GSN. If we don't have an entry, a GGSN has nothing to tell us
2116 * about, while an SGSN may initiate a PDP context. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002117 if (!from_peer) {
2118 if (side_idx == GTPH_SIDE_GGSN) {
2119 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
2120 osmo_sockaddr_to_str(from_addr));
2121 return -1;
2122 } else {
2123 /* SGSN */
2124 /* A new peer. If this is on the Ctrl plane, an SGSN
2125 * may make first contact without being known yet, so
2126 * create the peer struct for the current sender. */
2127 if (plane_idx != GTPH_PLANE_CTRL) {
2128 LOG(LOGL_ERROR,
2129 "Dropping packet: User plane peer was not"
2130 "announced by PDP Context: %s\n",
2131 osmo_sockaddr_to_str(from_addr));
2132 return -1;
2133 }
2134
2135 struct gsn_addr from_gsna;
2136 uint16_t from_port;
2137 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
2138 return -1;
2139
2140 from_peer = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
2141 }
2142 }
2143
2144 if (!from_peer) {
2145 /* This could theoretically happen for invalid address data or
2146 * somesuch. */
2147 LOG(LOGL_ERROR, "Dropping packet: invalid %s peer: %s\n",
2148 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002149 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002150 return -1;
2151 }
2152
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002153 LOG(LOGL_DEBUG, "from %s peer: %s\n", gtphub_side_idx_names[side_idx],
2154 gtphub_port_str(from_peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002155
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002156 struct gtphub_peer_port *to_peer_from_seq;
2157 struct gtphub_peer_port *to_peer;
2158 if (gtphub_unmap(hub, &p, from_peer,
2159 hub->proxy[other_side_idx(side_idx)][plane_idx],
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002160 &to_peer, &to_peer_from_seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002161 != 0) {
2162 return -1;
2163 }
2164
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002165 if ((!to_peer) && (side_idx == GTPH_SIDE_SGSN)) {
2166 if (gtphub_resolve_ggsn(hub, &p, &to_peer) < 0)
2167 return -1;
2168 }
2169
2170 if (!to_peer) {
2171 LOG(LOGL_ERROR, "No %s to send to. Dropping packet.\n",
2172 gtphub_side_idx_names[other_side_idx(side_idx)]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002173 return -1;
2174 }
2175
2176 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002177 /* This may be a Create PDP Context response. If it is, there
2178 * are other addresses in the GTP message to set up apart from
2179 * the sender. */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002180 if (gtphub_handle_pdp_ctx(hub, &p, from_peer, to_peer)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002181 != 0)
2182 return -1;
2183 }
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002184
2185 /* Either to_peer was resolved from an existing tunnel,
2186 * or a PDP Ctx and thus a tunnel has just been created,
2187 * or the tunnel has been deleted due to this message. */
2188 OSMO_ASSERT(p.tun || (p.type == GTP_DELETE_PDP_RSP));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002189
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002190 gtphub_check_restart_counter(hub, &p, from_peer);
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002191 gtphub_map_restart_counter(hub, &p);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002192
2193 /* If the GGSN is replying to an SGSN request, the sequence nr has
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002194 * already been unmapped above (to_peer_from_seq != NULL), and we need not
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002195 * create a new mapping. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002196 if (!to_peer_from_seq)
2197 gtphub_map_seq(&p, from_peer, to_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002198
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002199 osmo_sockaddr_copy(to_addr, &to_peer->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002200
2201 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01002202
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002203 if (received) {
2204 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2205 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2206 received);
2207 }
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002208 LOG(LOGL_DEBUG, "%s Forward to %s: %d bytes to %s\n",
2209 (side_idx == GTPH_SIDE_SGSN)? "-->" : "<--",
2210 gtphub_side_idx_names[other_side_idx(side_idx)],
Neels Hofmeyre921e322015-11-11 00:45:50 +01002211 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002212 return received;
2213}
2214
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002215static void resolved_gssn_del_cb(struct expiring_item *expi)
2216{
2217 struct gtphub_resolved_ggsn *ggsn;
2218 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
2219
2220 gtphub_port_ref_count_dec(ggsn->peer);
2221 llist_del(&ggsn->entry);
2222
2223 ggsn->expiry_entry.del_cb = 0;
2224 expiring_item_del(&ggsn->expiry_entry);
2225
2226 talloc_free(ggsn);
2227}
2228
2229void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
2230 struct gsn_addr *resolved_addr,
2231 time_t now)
2232{
2233 struct gtphub_peer_port *pp;
2234 struct gtphub_resolved_ggsn *ggsn;
2235
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002236 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002237 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
2238 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01002239
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002240 pp = gtphub_port_have(hub, &hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002241 resolved_addr, 2123);
2242 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002243 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
2244 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002245 return;
2246 }
2247
2248 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
2249 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01002250 INIT_LLIST_HEAD(&ggsn->entry);
2251 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002252
2253 ggsn->peer = pp;
2254 gtphub_port_ref_count_inc(pp);
2255
2256 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01002257 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002258
2259 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002260 expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002261
2262 llist_add(&ggsn->entry, &hub->resolved_ggsns);
2263}
2264
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002265static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
2266{
2267 return pp->ref_count == 0;
2268}
2269
2270static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
2271{
2272 struct gtphub_peer_port *pp, *npp;
2273 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
2274 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002275 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002276 gtphub_port_str(pp));
2277 gtphub_peer_port_del(pp);
2278 }
2279 }
2280 return llist_empty(&pa->ports);
2281}
2282
2283static int gtphub_gc_peer(struct gtphub_peer *p)
2284{
2285 struct gtphub_peer_addr *pa, *npa;
2286 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
2287 if (gtphub_gc_peer_addr(pa)) {
2288 gtphub_peer_addr_del(pa);
2289 }
2290 }
2291
2292 /* Note that there's a ref_count in each gtphub_peer_port instance
2293 * listed within p->addresses, referenced by TEI mappings from
2294 * hub->tei_map. As long as those don't expire, this peer will stay. */
2295
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002296 return llist_empty(&p->addresses)
2297 && nr_map_empty(&p->seq_map);
2298}
2299
2300static void gtphub_gc_bind(struct gtphub_bind *b)
2301{
2302 struct gtphub_peer *p, *n;
2303 llist_for_each_entry_safe(p, n, &b->peers, entry) {
2304 if (gtphub_gc_peer(p)) {
2305 gtphub_peer_del(p);
2306 }
2307 }
2308}
2309
2310void gtphub_gc(struct gtphub *hub, time_t now)
2311{
2312 int expired;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002313 expired = expiry_tick(&hub->expire_quickly, now);
2314 expired += expiry_tick(&hub->expire_slowly, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002315
2316 /* ... */
2317
2318 if (expired) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002319 int s, p;
2320 for_each_side_and_plane(s, p) {
2321 gtphub_gc_bind(&hub->to_gsns[s][p]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002322 }
2323 }
2324}
2325
2326static void gtphub_gc_cb(void *data)
2327{
2328 struct gtphub *hub = data;
2329 gtphub_gc(hub, gtphub_now());
2330 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2331}
2332
2333static void gtphub_gc_start(struct gtphub *hub)
2334{
2335 hub->gc_timer.cb = gtphub_gc_cb;
2336 hub->gc_timer.data = hub;
2337
2338 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2339}
2340
2341/* called by unit tests */
2342void gtphub_init(struct gtphub *hub)
2343{
2344 gtphub_zero(hub);
2345
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002346 INIT_LLIST_HEAD(&hub->tunnels);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002347 INIT_LLIST_HEAD(&hub->pending_deletes);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002348
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002349 expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS);
2350 expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002351
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01002352 nr_pool_init(&hub->tei_pool, 1, 0xffffffff);
2353
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002354 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002355 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002356 for_each_side_and_plane(side_idx, plane_idx) {
2357 gtphub_bind_init(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002358 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002359
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002360 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].label = "SGSN Ctrl";
2361 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].label = "GGSN Ctrl";
2362 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].label = "SGSN User";
2363 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002364}
2365
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002366/* For the test suite, this is kept separate from gtphub_stop(), which also
2367 * closes sockets. The test suite avoids using sockets and would cause
2368 * segfaults when trying to close uninitialized ofds. */
2369void gtphub_free(struct gtphub *hub)
2370{
2371 /* By expiring all mappings, a garbage collection should free
2372 * everything else. A gtphub_bind_free() will assert that everything is
2373 * indeed empty. */
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002374 expiry_clear(&hub->expire_quickly);
2375 expiry_clear(&hub->expire_slowly);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002376
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002377 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002378 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002379 for_each_side_and_plane(side_idx, plane_idx) {
2380 gtphub_gc_bind(&hub->to_gsns[side_idx][plane_idx]);
2381 gtphub_bind_free(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002382 }
2383}
2384
2385void gtphub_stop(struct gtphub *hub)
2386{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002387 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002388 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002389 for_each_side_and_plane(side_idx, plane_idx) {
2390 gtphub_bind_stop(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002391 }
2392 gtphub_free(hub);
2393}
2394
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002395static int gtphub_make_proxy(struct gtphub *hub,
2396 struct gtphub_peer_port **pp,
2397 struct gtphub_bind *bind,
2398 const struct gtphub_cfg_addr *addr)
2399{
2400 if (!addr->addr_str)
2401 return 0;
2402
2403 struct gsn_addr gsna;
2404 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
2405 return -1;
2406
2407 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
2408
2409 /* This is *the* proxy. Make sure it is never expired. */
2410 gtphub_port_ref_count_inc(*pp);
2411 return 0;
2412}
2413
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002414int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
2415 uint8_t restart_counter)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002416{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002417 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002418
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002419 hub->restart_counter = restart_counter;
2420
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002421 /* If a Ctrl plane proxy is configured, ares will never be used. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002422 if (!cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str) {
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002423 if (gtphub_ares_init(hub) != 0) {
2424 LOG(LOGL_FATAL, "Failed to initialize ares\n");
2425 return -1;
2426 }
2427 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002428
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002429 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002430 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002431 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01002432 int rc;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002433 rc = gtphub_bind_start(&hub->to_gsns[side_idx][plane_idx],
2434 &cfg->to_gsns[side_idx][plane_idx],
2435 (side_idx == GTPH_SIDE_SGSN)
2436 ? from_sgsns_read_cb
2437 : from_ggsns_read_cb,
2438 hub, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002439 if (rc) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002440 LOG(LOGL_FATAL, "Failed to bind for %ss (%s)\n",
2441 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002442 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002443 return rc;
2444 }
2445 }
2446
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002447 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002448 if (gtphub_make_proxy(hub,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002449 &hub->proxy[side_idx][plane_idx],
2450 &hub->to_gsns[side_idx][plane_idx],
2451 &cfg->proxy[side_idx][plane_idx])
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002452 != 0) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002453 LOG(LOGL_FATAL, "Cannot configure %s proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002454 " %s port %d.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002455 gtphub_side_idx_names[side_idx],
2456 cfg->proxy[side_idx][plane_idx].addr_str,
2457 (int)cfg->proxy[side_idx][plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002458 return -1;
2459 }
2460 }
2461
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002462 for_each_side_and_plane(side_idx, plane_idx) {
2463 if (hub->proxy[side_idx][plane_idx])
2464 LOG(LOGL_NOTICE, "Using %s %s proxy %s\n",
2465 gtphub_side_idx_names[side_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002466 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002467 gtphub_port_str(hub->proxy[side_idx][plane_idx]));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002468 }
2469
2470 gtphub_gc_start(hub);
2471 return 0;
2472}
2473
2474static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
2475 const struct gsn_addr *addr)
2476{
2477 struct gtphub_peer_addr *a;
2478 llist_for_each_entry(a, &peer->addresses, entry) {
2479 if (gsn_addr_same(&a->addr, addr))
2480 return a;
2481 }
2482 return NULL;
2483}
2484
2485static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2486 uint16_t port)
2487{
2488 OSMO_ASSERT(port);
2489 struct gtphub_peer_port *pp;
2490 llist_for_each_entry(pp, &a->ports, entry) {
2491 if (pp->port == port)
2492 return pp;
2493 }
2494 return NULL;
2495}
2496
2497static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2498 const struct gsn_addr *addr)
2499{
2500 struct gtphub_peer *peer;
2501 llist_for_each_entry(peer, &bind->peers, entry) {
2502 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2503 if (a)
2504 return a;
2505 }
2506 return NULL;
2507}
2508
2509static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2510 const struct gsn_addr *addr,
2511 uint16_t port)
2512{
2513 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2514 if (!a)
2515 return NULL;
2516 return gtphub_addr_find_port(a, port);
2517}
2518
2519struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2520 const struct osmo_sockaddr *addr)
2521{
2522 struct gsn_addr gsna;
2523 uint16_t port;
2524 gsn_addr_from_sockaddr(&gsna, &port, addr);
2525 return gtphub_port_find(bind, &gsna, port);
2526}
2527
2528static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2529 struct gtphub_bind *bind)
2530{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002531 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2532 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002533 OSMO_ASSERT(peer);
2534
2535 INIT_LLIST_HEAD(&peer->addresses);
2536
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002537 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002538 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002539
2540 /* TODO use something random to pick the initial sequence nr.
2541 0x6d31 produces the ASCII character sequence 'm1', currently used in
2542 gtphub_nc_test.sh. */
2543 peer->seq_pool.last_nr = 0x6d31 - 1;
2544
2545 llist_add(&peer->entry, &bind->peers);
2546 return peer;
2547}
2548
2549static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2550 const struct gsn_addr *addr)
2551{
2552 struct gtphub_peer_addr *a;
2553 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2554 OSMO_ASSERT(a);
2555 a->peer = peer;
2556 gsn_addr_copy(&a->addr, addr);
2557 INIT_LLIST_HEAD(&a->ports);
2558 llist_add(&a->entry, &peer->addresses);
2559
2560 return a;
2561}
2562
2563static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2564 struct gtphub_bind *bind,
2565 const struct gsn_addr *addr)
2566{
2567 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2568 if (a)
2569 return a;
2570
2571 /* If we haven't found an address, that means we need to create an
2572 * entirely new peer for the new address. More addresses may be added
2573 * to this peer later, but not via this function. */
2574 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002575
2576 a = gtphub_peer_add_addr(peer, addr);
2577
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002578 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002579 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002580 gsn_addr_to_str(&a->addr));
2581
2582 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002583}
2584
2585static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2586 uint16_t port)
2587{
2588 struct gtphub_peer_port *pp;
2589
2590 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2591 OSMO_ASSERT(pp);
2592 pp->peer_addr = a;
2593 pp->port = port;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002594 pp->last_restart_count = -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002595
2596 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2597 talloc_free(pp);
2598 return NULL;
2599 }
2600
2601 llist_add(&pp->entry, &a->ports);
2602
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002603 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002604 gsn_addr_to_str(&a->addr),
2605 (int)port);
2606
2607 return pp;
2608}
2609
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002610struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2611 struct gtphub_bind *bind,
2612 const struct gsn_addr *addr,
2613 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002614{
2615 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2616
2617 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2618 if (pp)
2619 return pp;
2620
2621 return gtphub_addr_add_port(a, port);
2622}
2623
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002624/* Find a GGSN peer with a matching address. If the address is known but the
2625 * port not, create a new port for that peer address. */
2626struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2627 const struct osmo_sockaddr *addr)
2628{
2629 struct gtphub_peer_addr *pa;
2630 struct gtphub_peer_port *pp;
2631
2632 struct gsn_addr gsna;
2633 uint16_t port;
2634 gsn_addr_from_sockaddr(&gsna, &port, addr);
2635
2636 pa = gtphub_addr_find(bind, &gsna);
2637 if (!pa)
2638 return NULL;
2639
2640 pp = gtphub_addr_find_port(pa, port);
2641
2642 if (!pp)
2643 pp = gtphub_addr_add_port(pa, port);
2644
2645 return pp;
2646}
2647
2648
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002649/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2650 * resolution should be possible but failed, and 1 if resolution was
2651 * successful. *pp will be set to NULL if <1 is returned. */
2652static int gtphub_resolve_ggsn(struct gtphub *hub,
2653 struct gtp_packet_desc *p,
2654 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002655{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002656 *pp = NULL;
2657
2658 /* TODO determine from message type whether IEs should be present? */
2659
2660 int rc;
2661 const char *imsi_str;
2662 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2663 if (rc < 1)
2664 return rc;
2665 OSMO_ASSERT(imsi_str);
2666
2667 const char *apn_str;
2668 rc = get_ie_apn_str(p->ie, &apn_str);
2669 if (rc < 1)
2670 return rc;
2671 OSMO_ASSERT(apn_str);
2672
2673 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2674 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002675}
2676
2677
2678/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002679/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002680/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2681 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002682static int _osmo_getaddrinfo(struct addrinfo **result,
2683 uint16_t family, uint16_t type, uint8_t proto,
2684 const char *host, uint16_t port)
2685{
2686 struct addrinfo hints;
2687 char portbuf[16];
2688
2689 sprintf(portbuf, "%u", port);
2690 memset(&hints, '\0', sizeof(struct addrinfo));
2691 hints.ai_family = family;
2692 if (type == SOCK_RAW) {
2693 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2694 * SOCK_RAW and IPPROTO_GRE is used.
2695 */
2696 hints.ai_socktype = SOCK_DGRAM;
2697 hints.ai_protocol = IPPROTO_UDP;
2698 } else {
2699 hints.ai_socktype = type;
2700 hints.ai_protocol = proto;
2701 }
2702
2703 return getaddrinfo(host, portbuf, &hints, result);
2704}
2705
2706/* TODO move to osmocom/core/socket.c ? */
2707int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2708 uint16_t family, uint16_t type, uint8_t proto,
2709 const char *host, uint16_t port)
2710{
2711 struct addrinfo *res;
2712 int rc;
2713 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2714
2715 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002716 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002717 return -EINVAL;
2718 }
2719
2720 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2721 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2722 addr->l = res->ai_addrlen;
2723 freeaddrinfo(res);
2724
2725 return 0;
2726}
2727
2728int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2729 char *port_str, size_t port_str_len,
2730 const struct osmo_sockaddr *addr,
2731 int flags)
2732{
2733 int rc;
2734
2735 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2736 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2737 return -1;
2738 }
2739
2740 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002741 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2742 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002743 return -1;
2744 }
2745
2746 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2747 addr_str, addr_str_len,
2748 port_str, port_str_len,
2749 flags);
2750
2751 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002752 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2753 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2754 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002755
2756 return rc;
2757}
2758
2759const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2760 char *buf, size_t buf_len)
2761{
2762 const int portbuf_len = 6;
2763 OSMO_ASSERT(buf_len > portbuf_len);
2764 char *portbuf = buf + buf_len - portbuf_len;
2765 buf_len -= portbuf_len;
2766 if (osmo_sockaddr_to_strs(buf, buf_len,
2767 portbuf, portbuf_len,
2768 addr,
2769 NI_NUMERICHOST | NI_NUMERICSERV))
2770 return NULL;
2771
2772 char *pos = buf + strnlen(buf, buf_len-1);
2773 size_t len = buf_len - (pos - buf);
2774
2775 snprintf(pos, len, " port %s", portbuf);
2776 buf[buf_len-1] = '\0';
2777
2778 return buf;
2779}
2780
2781const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2782{
2783 static char buf[256];
2784 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2785 if (! result)
2786 return "(invalid)";
2787 return result;
2788}
2789
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002790int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2791 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002792{
2793 if (a == b)
2794 return 0;
2795 if (!a)
2796 return -1;
2797 if (!b)
2798 return 1;
2799 if (a->l != b->l) {
2800 /* Lengths are not the same, but determine the order. Will
2801 * anyone ever sort a list by osmo_sockaddr though...? */
2802 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2803 if (cmp == 0) {
2804 if (a->l < b->l)
2805 return -1;
2806 else
2807 return 1;
2808 }
2809 return cmp;
2810 }
2811 return memcmp(&a->a, &b->a, a->l);
2812}
2813
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002814void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2815 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002816{
2817 OSMO_ASSERT(src->l <= sizeof(dst->a));
2818 memcpy(&dst->a, &src->a, src->l);
2819 dst->l = src->l;
2820}