blob: eb463d7ed6f10a59c6a045c84e85fd1032b43bdc [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 Hofmeyr28a70f22015-12-06 15:22:54 +0100334 LOG(LOGL_DEBUG, "| GTPv1\n");
335 LOG(LOGL_DEBUG, "| type = %" PRIu8 " 0x%02" PRIx8 "\n", p->type, p->type);
336 LOG(LOGL_DEBUG, "| length = %" PRIu16 " 0x%04" PRIx16 "\n", ntoh16(pheader->length), ntoh16(pheader->length));
337 LOG(LOGL_DEBUG, "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n", p->header_tei_rx, p->header_tei_rx);
338 LOG(LOGL_DEBUG, "| seq = %" PRIu16 " 0x%04" PRIx16 "\n", p->seq, p->seq);
339 LOG(LOGL_DEBUG, "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n", pheader->npdu, pheader->npdu);
340 LOG(LOGL_DEBUG, "| next = %" PRIu8 " 0x%02" PRIx8 "\n", pheader->next, pheader->next);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200341
342 if (p->data_len <= GTP1_HEADER_SIZE_LONG) {
343 p->rc = GTP_RC_TINY;
344 p->header_len = GTP1_HEADER_SIZE_SHORT;
345 return;
346 }
347
348 /* Check packet length field versus length of packet */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100349 int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT;
350 if (p->data_len != announced_len) {
351 LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not"
352 " match actual length (%d)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100353 GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length),
354 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200355 p->rc = GTP_RC_TOOSHORT;
356 return;
357 }
358
Neels Hofmeyre921e322015-11-11 00:45:50 +0100359 p->rc = GTP_RC_PDU_C;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200360 p->header_len = GTP1_HEADER_SIZE_LONG;
361}
362
363/* Examine whether p->data of size p->data_len has a valid GTP header. Set
364 * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum
365 * gtp_rc). p->data must point at a buffer with p->data_len set. */
366void validate_gtp_header(struct gtp_packet_desc *p)
367{
368 p->rc = GTP_RC_UNKNOWN;
369
370 /* Need at least 1 byte in order to check version */
371 if (p->data_len < 1) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100372 LOG(LOGL_ERROR, "Discarding packet - too small: %d\n",
373 p->data_len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200374 p->rc = GTP_RC_TOOSHORT;
375 return;
376 }
377
378 p->version = p->data->flags >> 5;
379
380 switch (p->version) {
381 case 0:
382 validate_gtp0_header(p);
383 break;
384 case 1:
385 validate_gtp1_header(p);
386 break;
387 default:
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100388 LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200389 p->rc = GTP_RC_UNSUPPORTED_VERSION;
390 break;
391 }
392}
393
394
395/* Return the value of the i'th IMSI IEI by copying to *imsi.
396 * The first IEI is reached by passing i = 0.
397 * imsi must point at allocated space of (at least) 8 bytes.
398 * Return 1 on success, or 0 if not found. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100399static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200400{
401 return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0;
402}
403
404/* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100405static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200406{
407 return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0;
408}
409
410static char imsi_digit_to_char(uint8_t nibble)
411{
412 nibble &= 0x0f;
413 if (nibble > 9)
414 return (nibble == 0x0f) ? '\0' : '?';
415 return '0' + nibble;
416}
417
418/* Return a human readable IMSI string, in a static buffer.
419 * imsi must point at 8 octets of IMSI IE encoded IMSI data. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100420static int imsi_to_str(uint8_t *imsi, const char **imsi_str)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200421{
422 static char str[17];
423 int i;
424
425 for (i = 0; i < 8; i++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +0100426 char c;
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100427 c = imsi_digit_to_char(imsi[i]);
428 if (c == '?')
429 return -1;
430 str[2*i] = c;
431
432 c = imsi_digit_to_char(imsi[i] >> 4);
433 if (c == '?')
434 return -1;
435 str[2*i + 1] = c;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200436 }
437 str[16] = '\0';
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100438 *imsi_str = str;
439 return 1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200440}
441
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100442/* Return 0 if not present, 1 if present and decoded successfully, -1 if
443 * present but cannot be decoded. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100444static int get_ie_imsi_str(union gtpie_member *ie[], int i,
445 const char **imsi_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100446{
447 uint8_t imsi_buf[8];
448 if (!get_ie_imsi(ie, i, imsi_buf))
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100449 return 0;
450 return imsi_to_str(imsi_buf, imsi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100451}
452
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100453/* Return 0 if not present, 1 if present and decoded successfully, -1 if
454 * present but cannot be decoded. */
455static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100456{
457 static char apn_buf[GSM_APN_LENGTH];
458 unsigned int len;
459 if (gtpie_gettlv(ie, GTPIE_APN, 0,
460 &len, apn_buf, sizeof(apn_buf)) != 0)
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100461 return 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100462
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100463 if (len < 2) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100464 LOG(LOGL_ERROR, "APN IE: invalid length: %d\n",
465 (int)len);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100466 return -1;
467 }
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100468
469 if (len > (sizeof(apn_buf) - 1))
470 len = sizeof(apn_buf) - 1;
471 apn_buf[len] = '\0';
472
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100473 *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
474 if (!(*apn_str)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100475 LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
476 osmo_hexdump((uint8_t*)apn_buf, len));
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100477 return -1;
478 }
479 return 1;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100480}
481
482
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200483/* Validate header, and index information elements. Write decoded packet
484 * information to *res. res->data will point at the given data buffer. On
485 * error, p->rc is set <= 0 (see enum gtp_rc). */
486static void gtp_decode(const uint8_t *data, int data_len,
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100487 unsigned int from_side_idx,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200488 unsigned int from_plane_idx,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100489 struct gtp_packet_desc *res,
490 time_t now)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200491{
492 ZERO_STRUCT(res);
493 res->data = (union gtp_packet*)data;
494 res->data_len = data_len;
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100495 res->side_idx = from_side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200496 res->plane_idx = from_plane_idx;
Neels Hofmeyr29d926b2015-11-24 13:27:13 +0100497 res->timestamp = now;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200498
499 validate_gtp_header(res);
500
Neels Hofmeyr87c83d02015-12-03 11:25:27 +0100501 if (res->rc <= 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200502 return;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200503
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100504 LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200505
Neels Hofmeyre921e322015-11-11 00:45:50 +0100506 if (from_plane_idx == GTPH_PLANE_USER) {
507 res->rc = GTP_RC_PDU_U;
508 return;
509 }
510
511 if (res->rc != GTP_RC_PDU_C) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100512 LOG(LOGL_DEBUG, "no IEs in this GTP packet\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200513 return;
514 }
515
516 if (gtpie_decaps(res->ie, res->version,
517 (void*)(data + res->header_len),
518 res->data_len - res->header_len) != 0) {
519 res->rc = GTP_RC_INVALID_IE;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100520 LOG(LOGL_ERROR, "INVALID: cannot decode IEs."
521 " Dropping GTP packet.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200522 return;
523 }
524
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100525#if 1
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100526 /* TODO if (<loglevel is debug>) { ...
527 (waiting for a commit from jerlbeck) */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200528 int i;
529
530 for (i = 0; i < 10; i++) {
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100531 const char *imsi;
532 if (get_ie_imsi_str(res->ie, i, &imsi) < 1)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200533 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100534 LOG(LOGL_DEBUG, "| IMSI %s\n", imsi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200535 }
536
537 for (i = 0; i < 10; i++) {
538 uint8_t nsapi;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100539 if (!get_ie_nsapi(res->ie, i, &nsapi))
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200540 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100541 LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200542 }
543
544 for (i = 0; i < 2; i++) {
545 struct gsn_addr addr;
546 if (gsn_addr_get(&addr, res, i) == 0)
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100547 LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200548 }
549
550 for (i = 0; i < 10; i++) {
551 uint32_t tei;
552 if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0)
553 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100554 LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200555 tei, tei);
556 }
557
558 for (i = 0; i < 10; i++) {
559 uint32_t tei;
560 if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0)
561 break;
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100562 LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200563 tei, tei);
564 }
565#endif
566}
567
568
569/* expiry */
570
571void expiry_init(struct expiry *exq, int expiry_in_seconds)
572{
573 ZERO_STRUCT(exq);
574 exq->expiry_in_seconds = expiry_in_seconds;
575 INIT_LLIST_HEAD(&exq->items);
576}
577
578void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now)
579{
580 item->expiry = now + exq->expiry_in_seconds;
581
Neels Hofmeyr1aa0e472015-11-24 13:32:23 +0100582 OSMO_ASSERT(llist_empty(&exq->items)
583 || (item->expiry
584 >= llist_last(&exq->items, struct expiring_item, entry)->expiry));
585
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200586 /* Add/move to the tail to always sort by expiry, ascending. */
587 llist_del(&item->entry);
588 llist_add_tail(&item->entry, &exq->items);
589}
590
591int expiry_tick(struct expiry *exq, time_t now)
592{
593 int expired = 0;
594 struct expiring_item *m, *n;
595 llist_for_each_entry_safe(m, n, &exq->items, entry) {
596 if (m->expiry <= now) {
597 expiring_item_del(m);
598 expired ++;
599 } else {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200600 /* The items are added sorted by expiry. So when we hit
601 * an unexpired entry, only more unexpired ones will
602 * follow. */
603 break;
604 }
605 }
606 return expired;
607}
608
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100609void expiry_clear(struct expiry *exq)
610{
611 struct expiring_item *m, *n;
612 llist_for_each_entry_safe(m, n, &exq->items, entry) {
613 expiring_item_del(m);
614 }
615}
616
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200617void expiring_item_init(struct expiring_item *item)
618{
619 ZERO_STRUCT(item);
620 INIT_LLIST_HEAD(&item->entry);
621}
622
623void expiring_item_del(struct expiring_item *item)
624{
625 OSMO_ASSERT(item);
626 llist_del(&item->entry);
627 INIT_LLIST_HEAD(&item->entry);
628 if (item->del_cb) {
629 /* avoid loops */
630 del_cb_t del_cb = item->del_cb;
631 item->del_cb = 0;
632 (del_cb)(item);
633 }
634}
635
636
637/* nr_map, nr_pool */
638
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100639void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200640{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100641 *pool = (struct nr_pool){
642 .nr_min = nr_min,
643 .nr_max = nr_max,
644 .last_nr = nr_max
645 };
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200646}
647
648nr_t nr_pool_next(struct nr_pool *pool)
649{
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +0100650 if (pool->last_nr >= pool->nr_max)
651 pool->last_nr = pool->nr_min;
652 else
653 pool->last_nr ++;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200654
655 return pool->last_nr;
656}
657
658void nr_map_init(struct nr_map *map, struct nr_pool *pool,
659 struct expiry *exq)
660{
661 ZERO_STRUCT(map);
662 map->pool = pool;
663 map->add_items_to_expiry = exq;
664 INIT_LLIST_HEAD(&map->mappings);
665}
666
667void nr_mapping_init(struct nr_mapping *m)
668{
669 ZERO_STRUCT(m);
670 INIT_LLIST_HEAD(&m->entry);
671 expiring_item_init(&m->expiry_entry);
672}
673
674void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now)
675{
676 /* Generate a mapped number */
677 mapping->repl = nr_pool_next(map->pool);
678
679 /* Add to the tail to always yield a list sorted by expiry, in
680 * ascending order. */
681 llist_add_tail(&mapping->entry, &map->mappings);
Neels Hofmeyr508514c2015-11-24 13:30:38 +0100682 nr_map_refresh(map, mapping, now);
683}
684
685void nr_map_refresh(struct nr_map *map, struct nr_mapping *mapping, time_t now)
686{
687 if (!map->add_items_to_expiry)
688 return;
689 expiry_add(map->add_items_to_expiry,
690 &mapping->expiry_entry,
691 now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200692}
693
694void nr_map_clear(struct nr_map *map)
695{
696 struct nr_mapping *m;
697 struct nr_mapping *n;
698 llist_for_each_entry_safe(m, n, &map->mappings, entry) {
699 nr_mapping_del(m);
700 }
701}
702
703int nr_map_empty(const struct nr_map *map)
704{
705 return llist_empty(&map->mappings);
706}
707
708struct nr_mapping *nr_map_get(const struct nr_map *map,
709 void *origin, nr_t nr_orig)
710{
711 struct nr_mapping *mapping;
712 llist_for_each_entry(mapping, &map->mappings, entry) {
713 if ((mapping->origin == origin)
714 && (mapping->orig == nr_orig))
715 return mapping;
716 }
717 /* Not found. */
718 return NULL;
719}
720
721struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl)
722{
723 struct nr_mapping *mapping;
724 llist_for_each_entry(mapping, &map->mappings, entry) {
725 if (mapping->repl == nr_repl) {
726 return mapping;
727 }
728 }
729 /* Not found. */
730 return NULL;
731}
732
733void nr_mapping_del(struct nr_mapping *mapping)
734{
735 OSMO_ASSERT(mapping);
736 llist_del(&mapping->entry);
737 INIT_LLIST_HEAD(&mapping->entry);
738 expiring_item_del(&mapping->expiry_entry);
739}
740
741
742/* gtphub */
743
744const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = {
745 "CTRL",
746 "USER",
747};
748
749const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = {
750 2123,
751 2152,
752};
753
Neels Hofmeyra9905a52015-11-29 23:49:48 +0100754const char* const gtphub_side_idx_names[GTPH_SIDE_N] = {
755 "SGSN",
756 "GGSN",
757};
758
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200759time_t gtphub_now(void)
760{
761 struct timespec now_tp;
762 OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0);
763 return now_tp.tv_sec;
764}
765
766/* Remove a gtphub_peer from its list and free it. */
767static void gtphub_peer_del(struct gtphub_peer *peer)
768{
Neels Hofmeyr1ed9a862015-11-20 00:04:41 +0100769 OSMO_ASSERT(llist_empty(&peer->addresses));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200770 nr_map_clear(&peer->seq_map);
771 llist_del(&peer->entry);
772 talloc_free(peer);
773}
774
775static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa)
776{
777 OSMO_ASSERT(llist_empty(&pa->ports));
778 llist_del(&pa->entry);
779 talloc_free(pa);
780}
781
782static void gtphub_peer_port_del(struct gtphub_peer_port *pp)
783{
784 OSMO_ASSERT(pp->ref_count == 0);
785 llist_del(&pp->entry);
786 talloc_free(pp);
787}
788
789/* From the information in the gtp_packet_desc, return the address of a GGSN.
790 * Return -1 on error. */
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100791static int gtphub_resolve_ggsn(struct gtphub *hub,
792 struct gtp_packet_desc *p,
793 struct gtphub_peer_port **pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200794
795/* See gtphub_ext.c (wrapped by unit test) */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100796struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
797 const char *imsi_str,
798 const char *apn_ni_str);
799int gtphub_ares_init(struct gtphub *hub);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200800
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200801static void gtphub_zero(struct gtphub *hub)
802{
803 ZERO_STRUCT(hub);
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100804 INIT_LLIST_HEAD(&hub->ggsn_lookups);
805 INIT_LLIST_HEAD(&hub->resolved_ggsns);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200806}
807
808static int gtphub_sock_init(struct osmo_fd *ofd,
809 const struct gtphub_cfg_addr *addr,
810 osmo_fd_cb_t cb,
811 void *data,
812 int ofd_id)
813{
814 if (!addr->addr_str) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100815 LOG(LOGL_FATAL, "Cannot bind: empty address.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200816 return -1;
817 }
818 if (!addr->port) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100819 LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200820 return -1;
821 }
822
823 ofd->when = BSC_FD_READ;
824 ofd->cb = cb;
825 ofd->data = data;
826 ofd->priv_nr = ofd_id;
827
828 int rc;
829 rc = osmo_sock_init_ofd(ofd,
830 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
831 addr->addr_str, addr->port,
832 OSMO_SOCK_F_BIND);
833 if (rc < 1) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100834 LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n",
835 addr->addr_str, (int)addr->port, rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200836 return -1;
837 }
838
839 return 0;
840}
841
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100842static void gtphub_sock_close(struct osmo_fd *ofd)
843{
844 close(ofd->fd);
845 osmo_fd_unregister(ofd);
846 ofd->cb = NULL;
847}
848
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200849static void gtphub_bind_init(struct gtphub_bind *b)
850{
851 ZERO_STRUCT(b);
852
853 INIT_LLIST_HEAD(&b->peers);
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100854
855 b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx,
856 &gtphub_ctrg_io_desc, 0);
857 OSMO_ASSERT(b->counters_io);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200858}
859
860static int gtphub_bind_start(struct gtphub_bind *b,
861 const struct gtphub_cfg_bind *cfg,
862 osmo_fd_cb_t cb, void *cb_data,
863 unsigned int ofd_id)
864{
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100865 LOG(LOGL_DEBUG, "Starting bind %s\n", b->label);
866 if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0) {
867 LOG(LOGL_FATAL, "Invalid bind address for %s: %s\n",
868 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200869 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100870 }
871 if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0) {
872 LOG(LOGL_FATAL, "Cannot bind for %s: %s\n",
873 b->label, cfg->bind.addr_str);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200874 return -1;
Neels Hofmeyr800126b2015-11-30 14:13:19 +0100875 }
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100876 b->local_port = cfg->bind.port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200877 return 0;
878}
879
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100880static void gtphub_bind_free(struct gtphub_bind *b)
881{
882 OSMO_ASSERT(llist_empty(&b->peers));
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +0100883 rate_ctr_group_free(b->counters_io);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +0100884}
885
886static void gtphub_bind_stop(struct gtphub_bind *b) {
887 gtphub_sock_close(&b->ofd);
888 gtphub_bind_free(b);
889}
890
Neels Hofmeyr40348972015-11-17 12:22:28 +0100891/* Recv datagram from from->fd, write sender's address to *from_addr.
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200892 * Return the number of bytes read, zero on error. */
893static int gtphub_read(const struct osmo_fd *from,
894 struct osmo_sockaddr *from_addr,
895 uint8_t *buf, size_t buf_len)
896{
Neels Hofmeyr40348972015-11-17 12:22:28 +0100897 OSMO_ASSERT(from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200898
Neels Hofmeyr40348972015-11-17 12:22:28 +0100899 /* recvfrom requires the available length set in *from_addr_len. */
900 from_addr->l = sizeof(from_addr->a);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200901 errno = 0;
902 ssize_t received = recvfrom(from->fd, buf, buf_len, 0,
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100903 (struct sockaddr*)&from_addr->a,
904 &from_addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200905 /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message
906 * is not truncated. Then maybe reduce buf's size. */
907
908 if (received <= 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +0100909 LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR),
910 "error: %s\n", strerror(errno));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200911 return 0;
912 }
913
Neels Hofmeyr28a70f22015-12-06 15:22:54 +0100914 LOG(LOGL_DEBUG, "Received %d bytes from %s: %s\n",
Neels Hofmeyr40348972015-11-17 12:22:28 +0100915 (int)received, osmo_sockaddr_to_str(from_addr),
916 osmo_hexdump(buf, received));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200917
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200918 return received;
919}
920
921inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp)
922{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100923 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200924 OSMO_ASSERT(pp->ref_count < UINT_MAX);
925 pp->ref_count++;
926}
927
928inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp)
929{
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100930 OSMO_ASSERT(pp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200931 OSMO_ASSERT(pp->ref_count > 0);
932 pp->ref_count--;
933}
934
935inline void set_seq(struct gtp_packet_desc *p, uint16_t seq)
936{
937 OSMO_ASSERT(p->version == 1);
938 p->data->gtp1l.h.seq = hton16(seq);
939 p->seq = seq;
940}
941
942inline void set_tei(struct gtp_packet_desc *p, uint32_t tei)
943{
944 OSMO_ASSERT(p->version == 1);
945 p->data->gtp1l.h.tei = hton32(tei);
946 p->header_tei = tei;
947}
948
949static void gtphub_mapping_del_cb(struct expiring_item *expi);
950
951static struct nr_mapping *gtphub_mapping_new()
952{
953 struct nr_mapping *nrm;
954 nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping);
955 OSMO_ASSERT(nrm);
956
957 nr_mapping_init(nrm);
958 nrm->expiry_entry.del_cb = gtphub_mapping_del_cb;
959 return nrm;
960}
961
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100962
963#define APPEND(args...) \
964 l = snprintf(pos, left, args); \
965 pos += l; \
966 left -= l
967
968static const char *gtphub_tunnel_side_str(struct gtphub_tunnel *tun,
969 int side_idx)
970{
971 static char buf[256];
972 char *pos = buf;
973 int left = sizeof(buf);
974 int l;
975
976 struct gtphub_tunnel_endpoint *c, *u;
977 c = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
978 u = &tun->endpoint[side_idx][GTPH_PLANE_USER];
979
980 /* print both only if they differ. */
981 if (!c->peer) {
982 APPEND("(uninitialized)");
983 } else {
984 APPEND("%s", gsn_addr_to_str(&c->peer->peer_addr->addr));
985 }
986
987 if (!u->peer) {
988 if (c->peer) {
Neels Hofmeyrba0525e2015-12-06 16:39:23 +0100989 APPEND("/(uninitialized)");
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100990 }
991 } else if ((!c->peer)
992 || (!gsn_addr_same(&u->peer->peer_addr->addr,
993 &c->peer->peer_addr->addr))) {
Neels Hofmeyrba0525e2015-12-06 16:39:23 +0100994 APPEND("/%s", gsn_addr_to_str(&u->peer->peer_addr->addr));
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100995 }
996
Neels Hofmeyrba0525e2015-12-06 16:39:23 +0100997 APPEND(" (TEI C %x=%x/U %x=%x)",
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100998 c->tei_orig, c->tei_repl,
999 u->tei_orig, u->tei_repl);
1000 return buf;
1001}
1002
1003const char *gtphub_tunnel_str(struct gtphub_tunnel *tun)
1004{
1005 static char buf[512];
1006 char *pos = buf;
1007 int left = sizeof(buf);
1008 int l;
1009
1010 APPEND("%s", gtphub_tunnel_side_str(tun, GTPH_SIDE_SGSN));
1011 APPEND(" <-> %s", gtphub_tunnel_side_str(tun, GTPH_SIDE_GGSN));
1012
1013 return buf;
1014}
1015
1016#undef APPEND
1017
1018void gtphub_tunnel_endpoint_set_peer(struct gtphub_tunnel_endpoint *te,
1019 struct gtphub_peer_port *pp)
1020{
1021 if (te->peer)
1022 gtphub_port_ref_count_dec(te->peer);
1023 te->peer = pp;
1024 if (te->peer)
1025 gtphub_port_ref_count_inc(te->peer);
1026}
1027
1028int gtphub_tunnel_complete(struct gtphub_tunnel *tun)
1029{
1030 if (!tun)
1031 return 0;
1032 int side_idx;
1033 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001034 for_each_side_and_plane(side_idx, plane_idx) {
1035 struct gtphub_tunnel_endpoint *te =
1036 &tun->endpoint[side_idx][plane_idx];
1037 if (!(te->peer && te->tei_orig && te->tei_repl))
1038 return 0;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001039 }
1040 return 1;
1041}
1042
1043static void gtphub_tunnel_del_cb(struct expiring_item *expi)
1044{
1045 struct gtphub_tunnel *tun = container_of(expi,
1046 struct gtphub_tunnel,
1047 expiry_entry);
1048 LOG(LOGL_DEBUG, "expired: %s\n", gtphub_tunnel_str(tun));
1049
1050 llist_del(&tun->entry);
1051 INIT_LLIST_HEAD(&tun->entry); /* mark unused */
1052
1053 expi->del_cb = 0; /* avoid recursion loops */
1054 expiring_item_del(&tun->expiry_entry); /* usually already done, but make sure. */
1055
1056 int side_idx;
1057 int plane_idx;
Neels Hofmeyrf9773202015-11-27 00:05:56 +01001058 for_each_side_and_plane(side_idx, plane_idx) {
1059 /* clear ref count */
1060 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1061 NULL);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001062 }
1063
1064 talloc_free(tun);
1065}
1066
1067static struct gtphub_tunnel *gtphub_tunnel_new()
1068{
1069 struct gtphub_tunnel *tun;
1070 tun = talloc_zero(osmo_gtphub_ctx, struct gtphub_tunnel);
1071 OSMO_ASSERT(tun);
1072
1073 INIT_LLIST_HEAD(&tun->entry);
1074 expiring_item_init(&tun->expiry_entry);
1075
1076 tun->expiry_entry.del_cb = gtphub_tunnel_del_cb;
1077 return tun;
1078}
1079
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001080static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf,
1081 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001082{
1083 if (llist_empty(&peer->addresses))
1084 return "(addressless)";
1085
1086 struct gtphub_peer_addr *a = llist_first(&peer->addresses,
1087 struct gtphub_peer_addr,
1088 entry);
1089 return gsn_addr_to_strb(&a->addr, buf, buflen);
1090}
1091
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001092static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf,
1093 int buflen)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001094{
1095 if (!port)
1096 return "(null port)";
1097
1098 snprintf(buf, buflen, "%s port %d",
1099 gsn_addr_to_str(&port->peer_addr->addr),
1100 (int)port->port);
1101 return buf;
1102}
1103
1104const char *gtphub_peer_str(struct gtphub_peer *peer)
1105{
1106 static char buf[256];
1107 return gtphub_peer_strb(peer, buf, sizeof(buf));
1108}
1109
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01001110const char *gtphub_port_str(struct gtphub_peer_port *port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001111{
1112 static char buf[256];
1113 return gtphub_port_strb(port, buf, sizeof(buf));
1114}
1115
1116static const char *gtphub_port_str2(struct gtphub_peer_port *port)
1117{
1118 static char buf[256];
1119 return gtphub_port_strb(port, buf, sizeof(buf));
1120}
1121
1122static void gtphub_mapping_del_cb(struct expiring_item *expi)
1123{
1124 expi->del_cb = 0; /* avoid recursion loops */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001125 expiring_item_del(expi); /* usually already done, but make sure. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001126
1127 struct nr_mapping *nrm = container_of(expi,
1128 struct nr_mapping,
1129 expiry_entry);
1130 llist_del(&nrm->entry);
1131 INIT_LLIST_HEAD(&nrm->entry); /* mark unused */
1132
1133 /* Just for log */
1134 struct gtphub_peer_port *from = nrm->origin;
1135 OSMO_ASSERT(from);
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001136 LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001137 (int)nrm->expiry_entry.expiry,
1138 gtphub_port_str(from),
Neels Hofmeyr334af5d2015-11-17 14:24:46 +01001139 (unsigned int)nrm->orig, (unsigned int)nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001140
1141 gtphub_port_ref_count_dec(from);
1142
1143 talloc_free(nrm);
1144}
1145
1146static struct nr_mapping *gtphub_mapping_have(struct nr_map *map,
1147 struct gtphub_peer_port *from,
1148 nr_t orig_nr,
1149 time_t now)
1150{
1151 struct nr_mapping *nrm;
1152
1153 nrm = nr_map_get(map, from, orig_nr);
1154
1155 if (!nrm) {
1156 nrm = gtphub_mapping_new();
1157 nrm->orig = orig_nr;
1158 nrm->origin = from;
1159 nr_map_add(map, nrm, now);
1160 gtphub_port_ref_count_inc(from);
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001161 LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001162 gtphub_port_str(from),
1163 (int)(nrm->orig), (int)(nrm->repl));
1164 } else {
Neels Hofmeyr508514c2015-11-24 13:30:38 +01001165 nr_map_refresh(map, nrm, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001166 }
1167
1168 OSMO_ASSERT(nrm);
1169 return nrm;
1170}
1171
Neels Hofmeyr3317c842015-11-11 17:20:42 +01001172static void gtphub_map_seq(struct gtp_packet_desc *p,
1173 struct gtphub_peer_port *from_port,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001174 struct gtphub_peer_port *to_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001175{
1176 /* Store a mapping in to_peer's map, so when we later receive a GTP
1177 * packet back from to_peer, the seq nr can be unmapped back to its
1178 * origin (from_peer here). */
1179 struct nr_mapping *nrm;
1180 nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map,
Neels Hofmeyr29d926b2015-11-24 13:27:13 +01001181 from_port, p->seq, p->timestamp);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001182
1183 /* Change the GTP packet to yield the new, mapped seq nr */
1184 set_seq(p, nrm->repl);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001185}
1186
1187static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p,
1188 struct gtphub_peer_port *responding_port)
1189{
1190 OSMO_ASSERT(p->version == 1);
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001191 struct nr_mapping *nrm =
1192 nr_map_get_inv(&responding_port->peer_addr->peer->seq_map,
1193 p->seq);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001194 if (!nrm)
1195 return NULL;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001196 LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n",
1197 nrm->origin, (int)(nrm->orig), (int)(nrm->repl));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001198 set_seq(p, nrm->orig);
1199 return nrm->origin;
1200}
1201
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001202static int gtphub_check_mapped_tei(struct gtphub_tunnel_endpoint *new_te,
1203 struct gtphub_tunnel_endpoint *iterated_te,
1204 uint32_t *tei_min,
1205 uint32_t *tei_max)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001206{
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001207 if (new_te->tei_repl != iterated_te->tei_repl)
1208 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001209
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001210 /* new_te->tei_repl is already taken. Try to find one out of the known
1211 * range. */
1212
1213 if ((*tei_max) < 0xffffffff) {
1214 (*tei_max)++;
1215 new_te->tei_repl = *tei_max;
1216 return 1;
1217 } else if ((*tei_min) > 1) {
1218 (*tei_min)--;
1219 new_te->tei_repl = *tei_min;
1220 return 1;
1221 }
1222
1223 /* None seems to be available. */
1224 return 0;
1225}
1226
1227static int gtphub_check_reused_teis(struct gtphub *hub,
1228 struct gtphub_tunnel *new_tun)
1229{
1230 uint32_t tei_min = 0xffffffff;
1231 uint32_t tei_max = 0;
1232 int side_idx;
1233 int plane_idx;
1234 int side_idx2;
1235 int plane_idx2;
1236 struct gtphub_tunnel_endpoint *te;
1237 struct gtphub_tunnel_endpoint *te2;
1238
1239 struct gtphub_tunnel *tun, *ntun;
1240
1241 llist_for_each_entry_safe(tun, ntun, &hub->tunnels, entry) {
1242 if (tun == new_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001243 continue;
1244
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001245 /* Check whether the GSN sent a TEI that it is reusing from a
1246 * previous tunnel. */
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001247 int tun_continue = 0;
1248 for_each_side(side_idx) {
1249 for_each_plane(plane_idx) {
1250 te = &tun->endpoint[side_idx][plane_idx];
1251 te2 = &new_tun->endpoint[side_idx][plane_idx];
1252 if ((te->tei_orig != te2->tei_orig)
1253 || (!te->peer)
1254 || (!te2->peer)
1255 || !gsn_addr_same(&te->peer->peer_addr->addr,
1256 &te2->peer->peer_addr->addr))
1257 continue;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001258
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001259 /* The peer is reusing a TEI that I believe to
1260 * be part of another tunnel. The other tunnel
1261 * must be stale, then. */
1262 LOG(LOGL_NOTICE,
1263 "Expiring tunnel due to reused TEI:"
1264 " peer %s sent %s TEI %x,"
1265 " previously used by tunnel %s...\n",
1266 gtphub_port_str(te->peer),
1267 gtphub_plane_idx_names[plane_idx],
1268 te->tei_orig,
1269 gtphub_tunnel_str(tun));
1270 LOG(LOGL_NOTICE, "...while establishing tunnel %s\n",
1271 gtphub_tunnel_str(new_tun));
Neels Hofmeyr52c0bd32015-12-02 14:14:32 +01001272
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001273 expiring_item_del(&tun->expiry_entry);
1274 /* continue to find more matches. There shouldn't be
1275 * any, but let's make sure. However, tun is deleted,
1276 * so we need to skip to the next tunnel. */
1277 tun_continue = 1;
1278 break;
1279 }
1280 if (tun_continue)
1281 break;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001282 }
Neels Hofmeyr18d30492015-12-02 15:00:50 +01001283 if (tun_continue)
1284 continue;
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001285
1286 /* Check whether the mapped TEIs assigned to the endpoints are
1287 * used anywhere else. */
1288 for_each_side_and_plane(side_idx, plane_idx) {
1289 te = &tun->endpoint[side_idx][plane_idx];
1290 tei_min = (tei_min < te->tei_repl)? tei_min : te->tei_repl;
1291 tei_max = (tei_max > te->tei_repl)? tei_max : te->tei_repl;
1292
1293 for_each_side_and_plane(side_idx2, plane_idx2) {
1294 te2 = &new_tun->endpoint[side_idx2][plane_idx2];
1295 if (!gtphub_check_mapped_tei(te2, te, &tei_min, &tei_max)) {
1296 LOG(LOGL_ERROR,
1297 "No mapped TEI is readily available."
1298 " Searching for holes between occupied"
1299 " TEIs not implemented.");
1300 return 0;
1301 }
1302 }
1303 }
1304
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001305 }
Neels Hofmeyr7174b162015-11-27 01:22:13 +01001306
1307 return 1;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001308}
1309
1310static void gtphub_tunnel_refresh(struct gtphub *hub,
1311 struct gtphub_tunnel *tun,
1312 time_t now)
1313{
1314 expiry_add(&hub->expire_slowly,
1315 &tun->expiry_entry,
1316 now);
1317}
1318
1319static struct gtphub_tunnel_endpoint *gtphub_unmap_tei(struct gtphub *hub,
1320 struct gtp_packet_desc *p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001321 struct gtphub_peer_port *from,
1322 struct gtphub_tunnel **unmapped_from_tun)
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001323{
1324 OSMO_ASSERT(from);
1325 int other_side = other_side_idx(p->side_idx);
1326
1327 struct gtphub_tunnel *tun;
1328 llist_for_each_entry(tun, &hub->tunnels, entry) {
1329 struct gtphub_tunnel_endpoint *te_from =
1330 &tun->endpoint[p->side_idx][p->plane_idx];
1331 struct gtphub_tunnel_endpoint *te_to =
1332 &tun->endpoint[other_side][p->plane_idx];
1333 if ((te_to->tei_repl == p->header_tei_rx)
Neels Hofmeyrfc1be3a2015-12-02 00:31:31 +01001334 && te_from->peer
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001335 && gsn_addr_same(&te_from->peer->peer_addr->addr,
1336 &from->peer_addr->addr)) {
1337 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001338 if (unmapped_from_tun)
1339 *unmapped_from_tun = tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001340 return te_to;
1341 }
1342 }
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001343
1344 if (unmapped_from_tun)
1345 *unmapped_from_tun = NULL;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001346 return NULL;
1347}
1348
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001349static void gtphub_map_restart_counter(struct gtphub *hub,
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001350 struct gtp_packet_desc *p)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001351{
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001352 if (p->rc != GTP_RC_PDU_C)
1353 return;
1354
1355 int ie_idx;
1356 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1357 if (ie_idx < 0)
1358 return;
1359
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001360 /* Always send gtphub's own restart counter */
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01001361 p->ie[ie_idx]->tv1.v = hton8(hub->restart_counter);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001362}
1363
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001364static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p,
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001365 struct gtphub_tunnel **unmapped_from_tun,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001366 struct gtphub *hub,
1367 struct gtp_packet_desc *p,
1368 struct gtphub_peer_port *from_port)
1369{
1370 OSMO_ASSERT(p->version == 1);
1371 *to_port_p = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001372 if (unmapped_from_tun)
1373 *unmapped_from_tun = NULL;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001374
1375 /* If the header's TEI is zero, no PDP context has been established
1376 * yet. If nonzero, a mapping should actually already exist for this
1377 * TEI, since it must have been announced in a PDP context creation. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001378 if (!p->header_tei_rx)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001379 return 0;
1380
1381 /* to_peer has previously announced a TEI, which was stored and
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001382 * mapped in a tunnel struct. */
1383 struct gtphub_tunnel_endpoint *to;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001384 to = gtphub_unmap_tei(hub, p, from_port, unmapped_from_tun);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001385 if (!to) {
1386 LOG(LOGL_ERROR, "Received unknown TEI %" PRIx32 " from %s\n",
1387 p->header_tei_rx, gtphub_port_str(from_port));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001388 return -1;
1389 }
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001390 OSMO_ASSERT(*unmapped_from_tun);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001391
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001392 uint32_t unmapped_tei = to->tei_orig;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001393 set_tei(p, unmapped_tei);
1394
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001395 LOG(LOGL_DEBUG, "Unmapped TEI coming from: %s\n",
1396 gtphub_tunnel_str(*unmapped_from_tun));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001397
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001398 /* May be NULL for an invalidated tunnel. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001399 *to_port_p = to->peer;
1400
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001401 return 0;
1402}
1403
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001404static int gtphub_handle_create_pdp_ctx(struct gtphub *hub,
1405 struct gtp_packet_desc *p,
1406 struct gtphub_peer_port *from_ctrl,
1407 struct gtphub_peer_port *to_ctrl)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001408{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001409 int plane_idx;
1410
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001411 osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1),
1412 plane_nrs_match_GSN_addr_IE_indices);
1413
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001414 struct gtphub_tunnel *tun = p->tun;
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001415
1416 if (p->type == GTP_CREATE_PDP_REQ) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001417 if (p->side_idx != GTPH_SIDE_SGSN) {
1418 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001419 " Request from the GGSN side: %s",
1420 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001421 return -1;
1422 }
1423
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001424 if (tun) {
1425 LOG(LOGL_ERROR, "Not implemented: Received"
1426 " Create PDP Context Request for an already"
1427 " established tunnel:"
1428 " from %s, tunnel %s\n",
1429 gtphub_port_str(from_ctrl),
1430 gtphub_tunnel_str(p->tun));
1431 return -1;
1432 }
1433
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001434 /* A new tunnel. */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001435 p->tun = tun = gtphub_tunnel_new();
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001436 llist_add(&tun->entry, &hub->tunnels);
1437 gtphub_tunnel_refresh(hub, tun, p->timestamp);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001438 /* The endpoint peers on this side (SGSN) will be set from IEs
1439 * below. Also set the GGSN Ctrl endpoint, for logging. */
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001440 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001441 to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001442 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001443 if (p->side_idx != GTPH_SIDE_GGSN) {
1444 LOG(LOGL_ERROR, "Wrong side: Create PDP Context"
Neels Hofmeyre5a07982015-12-03 13:47:05 +01001445 " Response from the SGSN side: %s",
1446 gtphub_port_str(from_ctrl));
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001447 return -1;
1448 }
1449
Neels Hofmeyr8c5b0732015-12-03 13:45:15 +01001450 /* The tunnel should already have been resolved from the header
1451 * TEI and be available in tun (== p->tun). Just fill in the
1452 * GSN Addresses below.*/
1453 OSMO_ASSERT(tun);
1454 OSMO_ASSERT(tun->endpoint[other_side_idx(p->side_idx)][GTPH_PLANE_CTRL].tei_repl
1455 == p->header_tei_rx);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001456 OSMO_ASSERT(to_ctrl);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001457 }
1458
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001459 uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI };
1460 int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001461 unsigned int side_idx = p->side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001462
1463 for (plane_idx = 0; plane_idx < 2; plane_idx++) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01001464 int rc;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001465 struct gsn_addr use_addr;
1466 uint16_t use_port;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001467 uint32_t tei_from_ie;
1468 int ie_idx;
1469
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001470 /* Fetch GSN Address and TEI from IEs. As ensured by above
1471 * static asserts, plane_idx corresponds to the GSN Address IE
1472 * index (the first one = 0 = ctrl, second one = 1 = user). */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001473 rc = gsn_addr_get(&use_addr, p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001474 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001475 LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n",
1476 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001477 return -1;
1478 }
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001479 LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001480 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001481 gsn_addr_to_str(&use_addr),
1482 use_addr.len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001483
1484 ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
1485 if (ie_idx < 0) {
1486 if (ie_mandatory) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001487 LOG(LOGL_ERROR,
1488 "Create PDP Context message invalid:"
1489 " missing IE %d\n",
1490 (int)ie_type[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001491 return -1;
1492 }
1493 tei_from_ie = 0;
1494 }
1495 else
1496 tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v);
1497
1498 /* Make sure an entry for this peer address with default port
Neels Hofmeyrca2361c2015-12-03 14:12:44 +01001499 * exists.
1500 *
1501 * Exception: if sgsn_use_sender is set, instead use the
1502 * sender's address and port for Ctrl -- the User port is not
1503 * known until the first User packet arrives.
1504 *
1505 * Note: doing this here is just an optimization, because
1506 * gtphub_handle_buf() has code to replace the tunnel
1507 * endpoints' addresses with the sender (needed for User
1508 * plane). We could just ignore sgsn_use_sender here. But if we
1509 * set up a default port here and replace it in
1510 * gtphub_handle_buf(), we'd be creating a peer port just to
1511 * expire it right away. */
1512 if (hub->sgsn_use_sender && (side_idx == GTPH_SIDE_SGSN)) {
1513 gsn_addr_from_sockaddr(&use_addr, &use_port, &from_ctrl->sa);
1514 } else {
1515 use_port = gtphub_plane_idx_default_port[plane_idx];
1516
1517 }
1518
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001519 struct gtphub_peer_port *peer_from_ie;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001520 peer_from_ie = gtphub_port_have(hub,
1521 &hub->to_gsns[side_idx][plane_idx],
1522 &use_addr, use_port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001523
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001524 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
1525 peer_from_ie);
1526
Neels Hofmeyr59c1b642015-12-03 11:30:43 +01001527 if (!tei_from_ie &&
1528 !tun->endpoint[side_idx][plane_idx].tei_orig) {
1529 LOG(LOGL_ERROR,
1530 "Create PDP Context message omits %s TEI, but the"
1531 " no TEI has been announced for this tunnel: %s",
1532 gtphub_plane_idx_names[plane_idx],
1533 gtphub_tunnel_str(tun));
1534 return -1;
1535 }
1536
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001537 if (tei_from_ie) {
1538 /* Create TEI mapping and replace in GTP packet IE */
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01001539 uint32_t mapped_tei = nr_pool_next(&hub->tei_pool);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001540
1541 tun->endpoint[side_idx][plane_idx].tei_orig = tei_from_ie;
1542 tun->endpoint[side_idx][plane_idx].tei_repl = mapped_tei;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001543 p->ie[ie_idx]->tv4.v = hton32(mapped_tei);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001544
Neels Hofmeyrcd865d62015-11-30 12:58:48 +01001545 if (!gtphub_check_reused_teis(hub, tun)) {
1546 /* It's highly unlikely that all TEIs are
1547 * taken. But the code looking for an unused
1548 * TEI is, at the time of writing this comment,
1549 * not able to find gaps in the TEI space. To
1550 * explicitly alert the user of this problem,
1551 * rather abort than carry on. */
1552 LOG(LOGL_FATAL, "TEI range exhausted. Cannot create TEI mapping, aborting.\n");
1553 abort();
1554 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001555 }
1556
1557 /* Replace the GSN address to reflect gtphub. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001558 rc = gsn_addr_put(&hub->to_gsns[other_side_idx(side_idx)][plane_idx].local_addr,
1559 p, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001560 if (rc) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001561 LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n",
1562 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001563 return -1;
1564 }
1565 }
1566
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001567 if (p->type == GTP_CREATE_PDP_REQ) {
1568 LOG(LOGL_DEBUG, "New tunnel, first half: %s\n",
1569 gtphub_tunnel_str(tun));
Neels Hofmeyr005f1752015-11-30 12:19:11 +01001570 } else if (p->type == GTP_CREATE_PDP_RSP) {
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001571 LOG(LOGL_DEBUG, "New tunnel: %s\n",
1572 gtphub_tunnel_str(tun));
1573 }
1574
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001575 return 0;
1576}
1577
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001578static void pending_delete_del_cb(struct expiring_item *expi)
1579{
1580 struct pending_delete *pd;
1581 pd = container_of(expi, struct pending_delete, expiry_entry);
1582
1583 llist_del(&pd->entry);
1584 INIT_LLIST_HEAD(&pd->entry);
1585
1586 pd->expiry_entry.del_cb = 0;
1587 expiring_item_del(&pd->expiry_entry);
1588
1589 talloc_free(pd);
1590}
1591
1592static struct pending_delete *pending_delete_new(void)
1593{
1594 struct pending_delete *pd = talloc_zero(osmo_gtphub_ctx, struct pending_delete);
1595 INIT_LLIST_HEAD(&pd->entry);
1596 expiring_item_init(&pd->expiry_entry);
1597 pd->expiry_entry.del_cb = pending_delete_del_cb;
1598 return pd;
1599}
1600
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001601static int gtphub_handle_delete_pdp_ctx(struct gtphub *hub,
1602 struct gtp_packet_desc *p,
1603 struct gtphub_peer_port *from_ctrl,
1604 struct gtphub_peer_port *to_ctrl)
1605{
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001606 struct gtphub_tunnel *known_tun = p->tun;
1607
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001608 if (p->type == GTP_DELETE_PDP_REQ) {
1609 if (!known_tun) {
1610 LOG(LOGL_ERROR, "Cannot find tunnel for Delete PDP Context Request.\n");
1611 return -1;
1612 }
1613
1614 /* Store the Delete Request until a successful Response is seen. */
1615 uint8_t teardown_ind;
1616 uint8_t nsapi;
1617
1618 if (gtpie_gettv1(p->ie, GTPIE_TEARDOWN, 0, &teardown_ind) != 0) {
1619 LOG(LOGL_ERROR, "Missing Teardown Ind IE in Delete PDP Context Request.\n");
1620 return -1;
1621 }
1622
1623 if (gtpie_gettv1(p->ie, GTPIE_NSAPI, 0, &nsapi) != 0) {
1624 LOG(LOGL_ERROR, "Missing NSAPI IE in Delete PDP Context Request.\n");
1625 return -1;
1626 }
1627
1628 struct pending_delete *pd = NULL;
1629
1630 struct pending_delete *pdi = NULL;
1631 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1632 if ((pdi->tun == known_tun)
1633 && (pdi->teardown_ind == teardown_ind)
1634 && (pdi->nsapi == nsapi)) {
1635 pd = pdi;
1636 break;
1637 }
1638 }
1639
1640 if (!pd) {
1641 pd = pending_delete_new();
1642 pd->tun = known_tun;
1643 pd->teardown_ind = teardown_ind;
1644 pd->nsapi = nsapi;
1645
1646 LOG(LOGL_DEBUG, "Tunnel delete pending: %s\n",
1647 gtphub_tunnel_str(known_tun));
1648 llist_add(&pd->entry, &hub->pending_deletes);
1649 }
1650
1651 /* Add or refresh timeout. */
1652 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1653
1654 /* If a pending_delete should expire before the response to
1655 * indicate success comes in, the responding peer will have the
1656 * tunnel deactivated, while the requesting peer gets no reply
1657 * and keeps the tunnel. The hope is that the requesting peer
1658 * will try again and get a useful response. */
1659 } else if (p->type == GTP_DELETE_PDP_RSP) {
1660 /* Find the Delete Request for this Response. */
1661 struct pending_delete *pd = NULL;
1662
1663 struct pending_delete *pdi;
1664 llist_for_each_entry(pdi, &hub->pending_deletes, entry) {
1665 if (known_tun == pdi->tun) {
1666 pd = pdi;
1667 break;
1668 }
1669 }
1670
1671 if (!pd) {
1672 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1673 " Cannot find matching request.");
1674 /* If we delete the tunnel now, anyone can send a
1675 * Delete response to kill tunnels at will. */
1676 return -1;
1677 }
1678
1679 /* TODO handle teardown_ind and nsapi */
1680
1681 expiring_item_del(&pd->expiry_entry);
1682
1683 uint8_t cause;
1684 if (gtpie_gettv1(p->ie, GTPIE_CAUSE, 0, &cause) != 0) {
1685 LOG(LOGL_ERROR, "Delete PDP Context Response:"
1686 " Missing Cause IE.");
1687 /* If we delete the tunnel now, at least one of the
1688 * peers may still think it is active. */
1689 return -1;
1690 }
1691
1692 if (cause != GTPCAUSE_ACC_REQ) {
1693 LOG(LOGL_NOTICE,
1694 "Delete PDP Context Response indicates failure;"
1695 "for %s\n",
1696 gtphub_tunnel_str(known_tun));
1697 return -1;
1698 }
1699
1700 LOG(LOGL_DEBUG, "Delete PDP Context: removing tunnel %s\n",
1701 gtphub_tunnel_str(known_tun));
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001702 p->tun = NULL;
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01001703 expiring_item_del(&known_tun->expiry_entry);
1704 }
1705
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001706 return 0;
1707}
1708
1709static int gtphub_handle_update_pdp_ctx(struct gtphub *hub,
1710 struct gtp_packet_desc *p,
1711 struct gtphub_peer_port *from_ctrl,
1712 struct gtphub_peer_port *to_ctrl)
1713{
1714 /* TODO */
1715 return 0;
1716}
1717
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001718/* Read GSN address IEs from p, and make sure these peer addresses exist in
1719 * bind[plane_idx] with default ports, in their respective planes (both Ctrl
1720 * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into
1721 * the packet p. */
1722static int gtphub_handle_pdp_ctx(struct gtphub *hub,
1723 struct gtp_packet_desc *p,
1724 struct gtphub_peer_port *from_ctrl,
1725 struct gtphub_peer_port *to_ctrl)
1726{
1727 OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL);
1728
1729 switch (p->type) {
1730 case GTP_CREATE_PDP_REQ:
1731 case GTP_CREATE_PDP_RSP:
1732 return gtphub_handle_create_pdp_ctx(hub, p,
1733 from_ctrl, to_ctrl);
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001734
1735 case GTP_DELETE_PDP_REQ:
1736 case GTP_DELETE_PDP_RSP:
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001737 return gtphub_handle_delete_pdp_ctx(hub, p,
Neels Hofmeyrff4b6302015-11-30 00:07:02 +01001738 from_ctrl, to_ctrl);
1739
1740 case GTP_UPDATE_PDP_REQ:
1741 case GTP_UPDATE_PDP_RSP:
1742 return gtphub_handle_update_pdp_ctx(hub, p,
1743 from_ctrl, to_ctrl);
1744
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001745 default:
1746 /* Nothing to do for this message type. */
1747 return 0;
1748 }
1749
1750}
1751
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001752static int gtphub_send_del_pdp_ctx(struct gtphub *hub,
1753 struct gtphub_tunnel *tun,
1754 int to_side)
1755{
1756 static uint8_t del_ctx_msg[16] = {
1757 0x32, /* GTP v1 flags */
1758 GTP_DELETE_PDP_REQ,
1759 0x00, 0x08, /* Length in network byte order */
1760 0x00, 0x00, 0x00, 0x00, /* TEI to be replaced */
1761 0, 0, /* Seq, to be replaced */
1762 0, 0, /* no extensions */
1763 0x13, 0xff, /* 19: Teardown ind = 1 */
1764 0x14, 0 /* 20: NSAPI = 0 */
1765 };
1766
1767 uint32_t *tei = (uint32_t*)&del_ctx_msg[4];
1768 uint16_t *seq = (uint16_t*)&del_ctx_msg[8];
1769
1770 struct gtphub_tunnel_endpoint *te =
1771 &tun->endpoint[to_side][GTPH_PLANE_CTRL];
1772
1773 if (! te->peer)
1774 return 0;
1775
1776 *tei = hton32(te->tei_orig);
1777 *seq = hton16(nr_pool_next(&te->peer->peer_addr->peer->seq_pool));
1778
1779 struct gtphub_bind *to_bind = &hub->to_gsns[to_side][GTPH_PLANE_CTRL];
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01001780 int rc = gtphub_write(&to_bind->ofd, &te->peer->sa,
1781 del_ctx_msg, sizeof(del_ctx_msg));
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001782 if (rc != 0) {
1783 LOG(LOGL_ERROR,
1784 "Failed to send out-of-band Delete PDP Context Request to %s\n",
1785 gtphub_port_str(te->peer));
1786 }
1787 return rc;
1788}
1789
1790/* Tell all peers on the other end of tunnels that PDP contexts are void. */
1791static void gtphub_restarted(struct gtphub *hub,
1792 struct gtp_packet_desc *p,
1793 struct gtphub_peer_port *pp)
1794{
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001795 LOG(LOGL_DEBUG, "Peer has restarted: %s\n",
1796 gtphub_port_str(pp));
1797
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001798 struct gtphub_tunnel *tun;
1799 llist_for_each_entry(tun, &hub->tunnels, entry) {
1800 int side_idx;
1801 for_each_side(side_idx) {
Neels Hofmeyrc6d51f52015-12-02 15:44:34 +01001802 struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][GTPH_PLANE_CTRL];
1803 struct gtphub_tunnel_endpoint *te2 = &tun->endpoint[other_side_idx(side_idx)][GTPH_PLANE_CTRL];
1804 if ((!te->peer)
1805 || (!te2->tei_orig)
1806 || (pp->peer_addr->peer != te->peer->peer_addr->peer))
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001807 continue;
1808
Neels Hofmeyr936b8902015-12-02 14:31:08 +01001809 LOG(LOGL_DEBUG, "Deleting tunnel due to peer restart: %s\n",
1810 gtphub_tunnel_str(tun));
1811
Neels Hofmeyrbc443302015-12-01 15:20:18 +01001812 /* Send a Delete PDP Context Request to the
1813 * peer on the other side, remember the pending
1814 * delete and wait for the response to delete
1815 * the tunnel. Clear this side of the tunnel to
1816 * make sure it isn't used.
1817 *
1818 * Should the delete message send fail, or if no
1819 * response is received, this tunnel will expire. If
1820 * its TEIs come up in a new PDP Context Request, it
1821 * will be removed. If messages for this tunnel should
1822 * come in (from the not restarted side), they will be
1823 * dropped because the tunnel is rendered unusable. */
1824 gtphub_send_del_pdp_ctx(hub, tun, other_side_idx(side_idx));
1825
1826 struct pending_delete *pd;
1827 pd = pending_delete_new();
1828 pd->tun = tun;
1829 pd->teardown_ind = 0xff;
1830 pd->nsapi = 0;
1831 llist_add(&pd->entry, &hub->pending_deletes);
1832 expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp);
1833
1834 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_CTRL],
1835 NULL);
1836 gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_USER],
1837 NULL);
1838 }
1839 }
1840}
1841
1842static int get_restart_count(struct gtp_packet_desc *p)
1843{
1844 int ie_idx;
1845 ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0);
1846 if (ie_idx < 0)
1847 return -1;
1848 return ntoh8(p->ie[ie_idx]->tv1.v);
1849}
1850
1851static void gtphub_check_restart_counter(struct gtphub *hub,
1852 struct gtp_packet_desc *p,
1853 struct gtphub_peer_port *from)
1854{
1855 /* If the peer is sending a Recovery IE (7.7.11) with a restart counter
1856 * that doesn't match the peer's previously sent restart counter, clear
1857 * that peer and cancel PDP contexts. */
1858
1859 int restart = get_restart_count(p);
1860
1861 if ((restart < 0) || (restart > 255))
1862 return;
1863
1864 if ((from->last_restart_count >= 0) && (from->last_restart_count <= 255)) {
1865 if (from->last_restart_count != restart) {
1866 gtphub_restarted(hub, p, from);
1867 }
1868 }
1869
1870 from->last_restart_count = restart;
1871}
1872
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001873static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what)
1874{
1875 unsigned int plane_idx = from_sgsns_ofd->priv_nr;
1876 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01001877 LOG(LOGL_DEBUG, "=== reading from SGSN (%s)\n",
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001878 gtphub_plane_idx_names[plane_idx]);
1879
1880 if (!(what & BSC_FD_READ))
1881 return 0;
1882
1883 struct gtphub *hub = from_sgsns_ofd->data;
1884
1885 static uint8_t buf[4096];
1886 struct osmo_sockaddr from_addr;
1887 struct osmo_sockaddr to_addr;
1888 struct osmo_fd *to_ofd;
1889 int len;
1890 uint8_t *reply_buf;
1891
1892 len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf));
1893 if (len < 1)
1894 return 0;
1895
1896 len = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, &from_addr,
1897 buf, len, gtphub_now(),
1898 &reply_buf, &to_ofd, &to_addr);
1899 if (len < 1)
1900 return 0;
1901
1902 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
1903}
1904
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001905static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what)
1906{
1907 unsigned int plane_idx = from_ggsns_ofd->priv_nr;
1908 OSMO_ASSERT(plane_idx < GTPH_PLANE_N);
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01001909 LOG(LOGL_DEBUG, "=== reading from GGSN (%s)\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001910 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001911 if (!(what & BSC_FD_READ))
1912 return 0;
1913
1914 struct gtphub *hub = from_ggsns_ofd->data;
1915
1916 static uint8_t buf[4096];
1917 struct osmo_sockaddr from_addr;
1918 struct osmo_sockaddr to_addr;
1919 struct osmo_fd *to_ofd;
Neels Hofmeyr16c3f572015-11-11 17:27:01 +01001920 int len;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001921 uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001922
1923 len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf));
1924 if (len < 1)
1925 return 0;
1926
Neels Hofmeyra9905a52015-11-29 23:49:48 +01001927 len = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, &from_addr,
1928 buf, len, gtphub_now(),
1929 &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001930 if (len < 1)
1931 return 0;
1932
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01001933 return gtphub_write(to_ofd, &to_addr, reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001934}
1935
1936static int gtphub_unmap(struct gtphub *hub,
1937 struct gtp_packet_desc *p,
1938 struct gtphub_peer_port *from,
1939 struct gtphub_peer_port *to_proxy,
1940 struct gtphub_peer_port **final_unmapped,
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001941 struct gtphub_peer_port **unmapped_from_seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001942{
1943 /* Always (try to) unmap sequence and TEI numbers, which need to be
1944 * replaced in the packet. Either way, give precedence to the proxy, if
1945 * configured. */
1946
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001947 if (unmapped_from_seq)
1948 *unmapped_from_seq = NULL;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001949 if (final_unmapped)
1950 *final_unmapped = NULL;
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001951 p->tun = NULL;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01001952
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001953 struct gtphub_peer_port *from_seq = NULL;
1954 struct gtphub_peer_port *from_tei = NULL;
1955 struct gtphub_peer_port *unmapped = NULL;
1956
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001957 from_seq = gtphub_unmap_seq(p, from);
1958
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01001959 if (gtphub_unmap_header_tei(&from_tei, &p->tun, hub, p, from) < 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001960 return -1;
1961
1962 struct gtphub_peer *from_peer = from->peer_addr->peer;
1963 if (from_seq && from_tei && (from_seq != from_tei)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001964 LOG(LOGL_DEBUG,
1965 "Seq unmap and TEI unmap yield two different peers."
1966 " Using seq unmap."
Neels Hofmeyr28a70f22015-12-06 15:22:54 +01001967 " (from %s %s: seq %d yields %s, tei %u yields %s)\n",
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001968 gtphub_plane_idx_names[p->plane_idx],
1969 gtphub_peer_str(from_peer),
1970 (int)p->seq,
1971 gtphub_port_str(from_seq),
Neels Hofmeyre54cd152015-11-24 13:31:06 +01001972 (unsigned int)p->header_tei_rx,
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001973 gtphub_port_str2(from_tei)
1974 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001975 }
1976 unmapped = (from_seq? from_seq : from_tei);
1977
1978 if (unmapped && to_proxy && (unmapped != to_proxy)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01001979 LOG(LOGL_NOTICE,
1980 "Unmap yields a different peer than the configured proxy."
1981 " Using proxy."
Neels Hofmeyr063a8022015-11-16 14:35:13 +01001982 " unmapped: %s proxy: %s\n",
1983 gtphub_port_str(unmapped),
1984 gtphub_port_str2(to_proxy)
1985 );
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001986 }
1987 unmapped = (to_proxy? to_proxy : unmapped);
1988
1989 if (!unmapped) {
1990 /* Return no error, but returned pointers are all NULL. */
1991 return 0;
1992 }
1993
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001994 if (unmapped_from_seq)
1995 *unmapped_from_seq = from_seq;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001996 if (final_unmapped)
1997 *final_unmapped = unmapped;
1998 return 0;
1999}
2000
2001static int gsn_addr_to_sockaddr(struct gsn_addr *src,
2002 uint16_t port,
2003 struct osmo_sockaddr *dst)
2004{
2005 return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port);
2006}
2007
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002008/* If p is an Echo request, replace p's data with the matching response and
2009 * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is
2010 * detected. */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002011static int gtphub_handle_echo_req(struct gtphub *hub, struct gtp_packet_desc *p,
2012 uint8_t **reply_buf)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002013{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002014 if (p->type != GTP_ECHO_REQ)
2015 return 0;
2016
2017 static uint8_t echo_response_data[14] = {
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002018 0x32, /* GTP v1 flags */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002019 GTP_ECHO_RSP,
2020 0x00, 14 - 8, /* Length in network byte order */
2021 0x00, 0x00, 0x00, 0x00, /* Zero TEI */
2022 0, 0, /* Seq, to be replaced */
2023 0, 0, /* no extensions */
2024 0x0e, /* Recovery IE */
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002025 0 /* Restart counter, to be replaced */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002026 };
2027 uint16_t *seq = (uint16_t*)&echo_response_data[8];
2028 uint8_t *recovery = &echo_response_data[13];
2029
2030 *seq = hton16(p->seq);
2031 *recovery = hub->restart_counter;
2032
2033 *reply_buf = echo_response_data;
2034
2035 return sizeof(echo_response_data);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002036}
2037
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002038struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2039 const struct osmo_sockaddr *addr);
2040
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002041/* Parse buffer as GTP packet, replace elements in-place and return the ofd and
2042 * address to forward to. Return a pointer to the osmo_fd, but copy the
2043 * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at
2044 * any moment, while the osmo_fd is guaranteed to persist. Return the number of
2045 * bytes to forward, 0 or less on failure. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002046int gtphub_handle_buf(struct gtphub *hub,
2047 unsigned int side_idx,
2048 unsigned int plane_idx,
2049 const struct osmo_sockaddr *from_addr,
2050 uint8_t *buf,
2051 size_t received,
2052 time_t now,
2053 uint8_t **reply_buf,
2054 struct osmo_fd **to_ofd,
2055 struct osmo_sockaddr *to_addr)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002056{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002057 struct gtphub_bind *from_bind = &hub->to_gsns[side_idx][plane_idx];
2058 struct gtphub_bind *to_bind = &hub->to_gsns[other_side_idx(side_idx)][plane_idx];
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002059
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002060 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN],
2061 received);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002062 LOG(LOGL_DEBUG, "%s rx %s from %s %s\n",
2063 (side_idx == GTPH_SIDE_GGSN)? "<-" : "->",
Neels Hofmeyre921e322015-11-11 00:45:50 +01002064 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002065 gtphub_side_idx_names[side_idx],
Neels Hofmeyre921e322015-11-11 00:45:50 +01002066 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002067
Neels Hofmeyrd8660ef2015-12-03 11:24:39 +01002068 struct gtp_packet_desc p;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002069 gtp_decode(buf, received, side_idx, plane_idx, &p, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002070
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002071 if (p.rc <= 0) {
2072 LOG(LOGL_ERROR, "INVALID: dropping GTP packet from %s %s %s\n",
2073 gtphub_side_idx_names[side_idx],
2074 gtphub_plane_idx_names[plane_idx],
2075 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002076 return -1;
Neels Hofmeyr87c83d02015-12-03 11:25:27 +01002077 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002078
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002079 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]);
2080
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002081 int reply_len;
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002082 reply_len = gtphub_handle_echo_req(hub, &p, reply_buf);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002083 if (reply_len > 0) {
2084 /* It was an echo. Nothing left to do. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002085 osmo_sockaddr_copy(to_addr, from_addr);
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002086 *to_ofd = &from_bind->ofd;
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002087
2088 rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2089 rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2090 reply_len);
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002091 LOG(LOGL_DEBUG, "%s Echo response to %s: %d bytes to %s\n",
2092 (side_idx == GTPH_SIDE_GGSN)? "-->" : "<--",
2093 gtphub_side_idx_names[side_idx],
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +01002094 (int)reply_len, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002095 return reply_len;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002096 }
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002097 if (reply_len < 0)
2098 return -1;
2099
Neels Hofmeyrdba6d1a2015-11-20 01:27:22 +01002100 *to_ofd = &to_bind->ofd;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002101
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002102 /* If a proxy is configured, check that it's indeed that proxy talking
2103 * to us. A proxy is a forced 1:1 connection, e.g. to another gtphub,
2104 * so no-one else is allowed to talk to us from that side. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002105 struct gtphub_peer_port *from_peer = hub->proxy[side_idx][plane_idx];
2106 if (from_peer) {
2107 if (osmo_sockaddr_cmp(&from_peer->sa, from_addr) != 0) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002108 LOG(LOGL_ERROR,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002109 "Rejecting: %s proxy configured, but GTP packet"
2110 " received on %s bind is from another sender:"
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002111 " proxy: %s sender: %s\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002112 gtphub_side_idx_names[side_idx],
2113 gtphub_side_idx_names[side_idx],
2114 gtphub_port_str(from_peer),
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002115 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002116 return -1;
2117 }
2118 }
2119
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002120 if (!from_peer) {
2121 /* Find or create a peer with a matching address. The sender's
2122 * port may in fact differ. */
2123 from_peer = gtphub_known_addr_have_port(from_bind, from_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002124 }
2125
2126 /* If any PDP context has been created, we already have an entry for
Neels Hofmeyr3fdba2e2015-11-30 14:17:21 +01002127 * this GSN. If we don't have an entry, a GGSN has nothing to tell us
2128 * about, while an SGSN may initiate a PDP context. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002129 if (!from_peer) {
2130 if (side_idx == GTPH_SIDE_GGSN) {
2131 LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n",
2132 osmo_sockaddr_to_str(from_addr));
2133 return -1;
2134 } else {
2135 /* SGSN */
2136 /* A new peer. If this is on the Ctrl plane, an SGSN
2137 * may make first contact without being known yet, so
2138 * create the peer struct for the current sender. */
2139 if (plane_idx != GTPH_PLANE_CTRL) {
2140 LOG(LOGL_ERROR,
2141 "Dropping packet: User plane peer was not"
2142 "announced by PDP Context: %s\n",
2143 osmo_sockaddr_to_str(from_addr));
2144 return -1;
2145 }
2146
2147 struct gsn_addr from_gsna;
2148 uint16_t from_port;
2149 if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0)
2150 return -1;
2151
2152 from_peer = gtphub_port_have(hub, from_bind, &from_gsna, from_port);
2153 }
2154 }
2155
2156 if (!from_peer) {
2157 /* This could theoretically happen for invalid address data or
2158 * somesuch. */
2159 LOG(LOGL_ERROR, "Dropping packet: invalid %s peer: %s\n",
2160 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002161 osmo_sockaddr_to_str(from_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002162 return -1;
2163 }
2164
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002165 LOG(LOGL_DEBUG, "from %s peer: %s\n", gtphub_side_idx_names[side_idx],
2166 gtphub_port_str(from_peer));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002167
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002168 struct gtphub_peer_port *to_peer_from_seq;
2169 struct gtphub_peer_port *to_peer;
2170 if (gtphub_unmap(hub, &p, from_peer,
2171 hub->proxy[other_side_idx(side_idx)][plane_idx],
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002172 &to_peer, &to_peer_from_seq)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002173 != 0) {
2174 return -1;
2175 }
2176
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002177 if ((!to_peer) && (side_idx == GTPH_SIDE_SGSN)) {
2178 if (gtphub_resolve_ggsn(hub, &p, &to_peer) < 0)
2179 return -1;
2180 }
2181
2182 if (!to_peer) {
2183 LOG(LOGL_ERROR, "No %s to send to. Dropping packet.\n",
2184 gtphub_side_idx_names[other_side_idx(side_idx)]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002185 return -1;
2186 }
2187
2188 if (plane_idx == GTPH_PLANE_CTRL) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002189 /* This may be a Create PDP Context response. If it is, there
2190 * are other addresses in the GTP message to set up apart from
2191 * the sender. */
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002192 if (gtphub_handle_pdp_ctx(hub, &p, from_peer, to_peer)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002193 != 0)
2194 return -1;
2195 }
Neels Hofmeyrd53c6042015-12-03 13:59:49 +01002196
2197 /* Either to_peer was resolved from an existing tunnel,
2198 * or a PDP Ctx and thus a tunnel has just been created,
2199 * or the tunnel has been deleted due to this message. */
2200 OSMO_ASSERT(p.tun || (p.type == GTP_DELETE_PDP_RSP));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002201
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002202 gtphub_check_restart_counter(hub, &p, from_peer);
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002203 gtphub_map_restart_counter(hub, &p);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002204
2205 /* If the GGSN is replying to an SGSN request, the sequence nr has
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002206 * already been unmapped above (to_peer_from_seq != NULL), and we need not
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002207 * create a new mapping. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002208 if (!to_peer_from_seq)
2209 gtphub_map_seq(&p, from_peer, to_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002210
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002211 osmo_sockaddr_copy(to_addr, &to_peer->sa);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +01002212
2213 *reply_buf = (uint8_t*)p.data;
Neels Hofmeyre921e322015-11-11 00:45:50 +01002214
Neels Hofmeyr1ba50c62015-11-20 01:28:40 +01002215 if (received) {
2216 rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]);
2217 rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT],
2218 received);
2219 }
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002220 LOG(LOGL_DEBUG, "%s Forward to %s: %d bytes to %s\n",
2221 (side_idx == GTPH_SIDE_SGSN)? "-->" : "<--",
2222 gtphub_side_idx_names[other_side_idx(side_idx)],
Neels Hofmeyre921e322015-11-11 00:45:50 +01002223 (int)received, osmo_sockaddr_to_str(to_addr));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002224 return received;
2225}
2226
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002227static void resolved_gssn_del_cb(struct expiring_item *expi)
2228{
2229 struct gtphub_resolved_ggsn *ggsn;
2230 ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry);
2231
2232 gtphub_port_ref_count_dec(ggsn->peer);
2233 llist_del(&ggsn->entry);
2234
2235 ggsn->expiry_entry.del_cb = 0;
2236 expiring_item_del(&ggsn->expiry_entry);
2237
2238 talloc_free(ggsn);
2239}
2240
2241void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
2242 struct gsn_addr *resolved_addr,
2243 time_t now)
2244{
2245 struct gtphub_peer_port *pp;
2246 struct gtphub_resolved_ggsn *ggsn;
2247
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002248 LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002249 apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr,
2250 sizeof(*resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +01002251
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002252 pp = gtphub_port_have(hub, &hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002253 resolved_addr, 2123);
2254 if (!pp) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002255 LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n",
2256 gsn_addr_to_str(resolved_addr));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002257 return;
2258 }
2259
2260 ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn);
2261 OSMO_ASSERT(ggsn);
Neels Hofmeyra4370dd2015-11-24 12:46:11 +01002262 INIT_LLIST_HEAD(&ggsn->entry);
2263 expiring_item_init(&ggsn->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002264
2265 ggsn->peer = pp;
2266 gtphub_port_ref_count_inc(pp);
2267
2268 strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str));
Neels Hofmeyr3c820ee2015-11-17 12:28:09 +01002269 ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0';
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002270
2271 ggsn->expiry_entry.del_cb = resolved_gssn_del_cb;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002272 expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002273
2274 llist_add(&ggsn->entry, &hub->resolved_ggsns);
2275}
2276
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002277static int gtphub_gc_peer_port(struct gtphub_peer_port *pp)
2278{
2279 return pp->ref_count == 0;
2280}
2281
2282static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa)
2283{
2284 struct gtphub_peer_port *pp, *npp;
2285 llist_for_each_entry_safe(pp, npp, &pa->ports, entry) {
2286 if (gtphub_gc_peer_port(pp)) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002287 LOG(LOGL_DEBUG, "expired: peer %s\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002288 gtphub_port_str(pp));
2289 gtphub_peer_port_del(pp);
2290 }
2291 }
2292 return llist_empty(&pa->ports);
2293}
2294
2295static int gtphub_gc_peer(struct gtphub_peer *p)
2296{
2297 struct gtphub_peer_addr *pa, *npa;
2298 llist_for_each_entry_safe(pa, npa, &p->addresses, entry) {
2299 if (gtphub_gc_peer_addr(pa)) {
2300 gtphub_peer_addr_del(pa);
2301 }
2302 }
2303
2304 /* Note that there's a ref_count in each gtphub_peer_port instance
2305 * listed within p->addresses, referenced by TEI mappings from
2306 * hub->tei_map. As long as those don't expire, this peer will stay. */
2307
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002308 return llist_empty(&p->addresses)
2309 && nr_map_empty(&p->seq_map);
2310}
2311
2312static void gtphub_gc_bind(struct gtphub_bind *b)
2313{
2314 struct gtphub_peer *p, *n;
2315 llist_for_each_entry_safe(p, n, &b->peers, entry) {
2316 if (gtphub_gc_peer(p)) {
2317 gtphub_peer_del(p);
2318 }
2319 }
2320}
2321
2322void gtphub_gc(struct gtphub *hub, time_t now)
2323{
2324 int expired;
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002325 expired = expiry_tick(&hub->expire_quickly, now);
2326 expired += expiry_tick(&hub->expire_slowly, now);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002327
2328 /* ... */
2329
2330 if (expired) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002331 int s, p;
2332 for_each_side_and_plane(s, p) {
2333 gtphub_gc_bind(&hub->to_gsns[s][p]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002334 }
2335 }
2336}
2337
2338static void gtphub_gc_cb(void *data)
2339{
2340 struct gtphub *hub = data;
2341 gtphub_gc(hub, gtphub_now());
2342 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2343}
2344
2345static void gtphub_gc_start(struct gtphub *hub)
2346{
2347 hub->gc_timer.cb = gtphub_gc_cb;
2348 hub->gc_timer.data = hub;
2349
2350 osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0);
2351}
2352
2353/* called by unit tests */
2354void gtphub_init(struct gtphub *hub)
2355{
2356 gtphub_zero(hub);
2357
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002358 INIT_LLIST_HEAD(&hub->tunnels);
Neels Hofmeyr10fc0242015-12-01 00:23:45 +01002359 INIT_LLIST_HEAD(&hub->pending_deletes);
Neels Hofmeyre54cd152015-11-24 13:31:06 +01002360
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002361 expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS);
2362 expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002363
Neels Hofmeyrd121ea62015-11-27 01:20:53 +01002364 nr_pool_init(&hub->tei_pool, 1, 0xffffffff);
2365
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002366 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002367 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002368 for_each_side_and_plane(side_idx, plane_idx) {
2369 gtphub_bind_init(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002370 }
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002371
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002372 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].label = "SGSN Ctrl";
2373 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].label = "GGSN Ctrl";
2374 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].label = "SGSN User";
2375 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].label = "GGSN User";
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002376}
2377
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002378/* For the test suite, this is kept separate from gtphub_stop(), which also
2379 * closes sockets. The test suite avoids using sockets and would cause
2380 * segfaults when trying to close uninitialized ofds. */
2381void gtphub_free(struct gtphub *hub)
2382{
2383 /* By expiring all mappings, a garbage collection should free
2384 * everything else. A gtphub_bind_free() will assert that everything is
2385 * indeed empty. */
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002386 expiry_clear(&hub->expire_quickly);
2387 expiry_clear(&hub->expire_slowly);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002388
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002389 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002390 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002391 for_each_side_and_plane(side_idx, plane_idx) {
2392 gtphub_gc_bind(&hub->to_gsns[side_idx][plane_idx]);
2393 gtphub_bind_free(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002394 }
2395}
2396
2397void gtphub_stop(struct gtphub *hub)
2398{
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002399 int side_idx;
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002400 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002401 for_each_side_and_plane(side_idx, plane_idx) {
2402 gtphub_bind_stop(&hub->to_gsns[side_idx][plane_idx]);
Neels Hofmeyr20bd6bf2015-11-20 00:08:28 +01002403 }
2404 gtphub_free(hub);
2405}
2406
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002407static int gtphub_make_proxy(struct gtphub *hub,
2408 struct gtphub_peer_port **pp,
2409 struct gtphub_bind *bind,
2410 const struct gtphub_cfg_addr *addr)
2411{
2412 if (!addr->addr_str)
2413 return 0;
2414
2415 struct gsn_addr gsna;
2416 if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
2417 return -1;
2418
2419 *pp = gtphub_port_have(hub, bind, &gsna, addr->port);
2420
2421 /* This is *the* proxy. Make sure it is never expired. */
2422 gtphub_port_ref_count_inc(*pp);
2423 return 0;
2424}
2425
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002426int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
2427 uint8_t restart_counter)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002428{
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002429 gtphub_init(hub);
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002430
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002431 hub->restart_counter = restart_counter;
Neels Hofmeyrca2361c2015-12-03 14:12:44 +01002432 hub->sgsn_use_sender = cfg->sgsn_use_sender? 1 : 0;
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +01002433
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002434 /* If a Ctrl plane proxy is configured, ares will never be used. */
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002435 if (!cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str) {
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +01002436 if (gtphub_ares_init(hub) != 0) {
2437 LOG(LOGL_FATAL, "Failed to initialize ares\n");
2438 return -1;
2439 }
2440 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002441
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002442 int side_idx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002443 int plane_idx;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002444 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +01002445 int rc;
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002446 rc = gtphub_bind_start(&hub->to_gsns[side_idx][plane_idx],
2447 &cfg->to_gsns[side_idx][plane_idx],
2448 (side_idx == GTPH_SIDE_SGSN)
2449 ? from_sgsns_read_cb
2450 : from_ggsns_read_cb,
2451 hub, plane_idx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002452 if (rc) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002453 LOG(LOGL_FATAL, "Failed to bind for %ss (%s)\n",
2454 gtphub_side_idx_names[side_idx],
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002455 gtphub_plane_idx_names[plane_idx]);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002456 return rc;
2457 }
2458 }
2459
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002460 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002461 if (gtphub_make_proxy(hub,
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002462 &hub->proxy[side_idx][plane_idx],
2463 &hub->to_gsns[side_idx][plane_idx],
2464 &cfg->proxy[side_idx][plane_idx])
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002465 != 0) {
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002466 LOG(LOGL_FATAL, "Cannot configure %s proxy"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002467 " %s port %d.\n",
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002468 gtphub_side_idx_names[side_idx],
2469 cfg->proxy[side_idx][plane_idx].addr_str,
2470 (int)cfg->proxy[side_idx][plane_idx].port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002471 return -1;
2472 }
2473 }
2474
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002475 for_each_side_and_plane(side_idx, plane_idx) {
2476 if (hub->proxy[side_idx][plane_idx])
2477 LOG(LOGL_NOTICE, "Using %s %s proxy %s\n",
2478 gtphub_side_idx_names[side_idx],
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002479 gtphub_plane_idx_names[plane_idx],
Neels Hofmeyra9905a52015-11-29 23:49:48 +01002480 gtphub_port_str(hub->proxy[side_idx][plane_idx]));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002481 }
2482
Neels Hofmeyre1ba7812015-12-03 14:48:22 +01002483 if (hub->sgsn_use_sender)
Neels Hofmeyr9d8f5062015-12-03 14:52:33 +01002484 LOG(LOGL_NOTICE, "Using sender address and port for SGSN instead of GSN Addr IE and default ports.\n");
Neels Hofmeyre1ba7812015-12-03 14:48:22 +01002485
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002486 gtphub_gc_start(hub);
2487 return 0;
2488}
2489
2490static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer,
2491 const struct gsn_addr *addr)
2492{
2493 struct gtphub_peer_addr *a;
2494 llist_for_each_entry(a, &peer->addresses, entry) {
2495 if (gsn_addr_same(&a->addr, addr))
2496 return a;
2497 }
2498 return NULL;
2499}
2500
2501static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a,
2502 uint16_t port)
2503{
2504 OSMO_ASSERT(port);
2505 struct gtphub_peer_port *pp;
2506 llist_for_each_entry(pp, &a->ports, entry) {
2507 if (pp->port == port)
2508 return pp;
2509 }
2510 return NULL;
2511}
2512
2513static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind,
2514 const struct gsn_addr *addr)
2515{
2516 struct gtphub_peer *peer;
2517 llist_for_each_entry(peer, &bind->peers, entry) {
2518 struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr);
2519 if (a)
2520 return a;
2521 }
2522 return NULL;
2523}
2524
2525static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind,
2526 const struct gsn_addr *addr,
2527 uint16_t port)
2528{
2529 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2530 if (!a)
2531 return NULL;
2532 return gtphub_addr_find_port(a, port);
2533}
2534
2535struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
2536 const struct osmo_sockaddr *addr)
2537{
2538 struct gsn_addr gsna;
2539 uint16_t port;
2540 gsn_addr_from_sockaddr(&gsna, &port, addr);
2541 return gtphub_port_find(bind, &gsna, port);
2542}
2543
2544static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub,
2545 struct gtphub_bind *bind)
2546{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002547 struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx,
2548 struct gtphub_peer);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002549 OSMO_ASSERT(peer);
2550
2551 INIT_LLIST_HEAD(&peer->addresses);
2552
Neels Hofmeyre2ed8e62015-11-17 14:30:37 +01002553 nr_pool_init(&peer->seq_pool, 0, 0xffff);
Neels Hofmeyr2c8b5812015-11-25 16:45:59 +01002554 nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002555
2556 /* TODO use something random to pick the initial sequence nr.
2557 0x6d31 produces the ASCII character sequence 'm1', currently used in
2558 gtphub_nc_test.sh. */
2559 peer->seq_pool.last_nr = 0x6d31 - 1;
2560
2561 llist_add(&peer->entry, &bind->peers);
2562 return peer;
2563}
2564
2565static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
2566 const struct gsn_addr *addr)
2567{
2568 struct gtphub_peer_addr *a;
2569 a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
2570 OSMO_ASSERT(a);
2571 a->peer = peer;
2572 gsn_addr_copy(&a->addr, addr);
2573 INIT_LLIST_HEAD(&a->ports);
2574 llist_add(&a->entry, &peer->addresses);
2575
2576 return a;
2577}
2578
2579static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
2580 struct gtphub_bind *bind,
2581 const struct gsn_addr *addr)
2582{
2583 struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
2584 if (a)
2585 return a;
2586
2587 /* If we haven't found an address, that means we need to create an
2588 * entirely new peer for the new address. More addresses may be added
2589 * to this peer later, but not via this function. */
2590 struct gtphub_peer *peer = gtphub_peer_new(hub, bind);
Neels Hofmeyre921e322015-11-11 00:45:50 +01002591
2592 a = gtphub_peer_add_addr(peer, addr);
2593
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002594 LOG(LOGL_DEBUG, "New peer address: %s %s\n",
Neels Hofmeyr390e9102015-11-16 13:45:13 +01002595 bind->label,
Neels Hofmeyre921e322015-11-11 00:45:50 +01002596 gsn_addr_to_str(&a->addr));
2597
2598 return a;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002599}
2600
2601static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a,
2602 uint16_t port)
2603{
2604 struct gtphub_peer_port *pp;
2605
2606 pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port);
2607 OSMO_ASSERT(pp);
2608 pp->peer_addr = a;
2609 pp->port = port;
Neels Hofmeyrbc443302015-12-01 15:20:18 +01002610 pp->last_restart_count = -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002611
2612 if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) {
2613 talloc_free(pp);
2614 return NULL;
2615 }
2616
2617 llist_add(&pp->entry, &a->ports);
2618
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002619 LOG(LOGL_DEBUG, "New peer port: %s port %d\n",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002620 gsn_addr_to_str(&a->addr),
2621 (int)port);
2622
2623 return pp;
2624}
2625
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +01002626struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
2627 struct gtphub_bind *bind,
2628 const struct gsn_addr *addr,
2629 uint16_t port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002630{
2631 struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
2632
2633 struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port);
2634 if (pp)
2635 return pp;
2636
2637 return gtphub_addr_add_port(a, port);
2638}
2639
Neels Hofmeyrc83cd892015-11-11 14:01:06 +01002640/* Find a GGSN peer with a matching address. If the address is known but the
2641 * port not, create a new port for that peer address. */
2642struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind,
2643 const struct osmo_sockaddr *addr)
2644{
2645 struct gtphub_peer_addr *pa;
2646 struct gtphub_peer_port *pp;
2647
2648 struct gsn_addr gsna;
2649 uint16_t port;
2650 gsn_addr_from_sockaddr(&gsna, &port, addr);
2651
2652 pa = gtphub_addr_find(bind, &gsna);
2653 if (!pa)
2654 return NULL;
2655
2656 pp = gtphub_addr_find_port(pa, port);
2657
2658 if (!pp)
2659 pp = gtphub_addr_add_port(pa, port);
2660
2661 return pp;
2662}
2663
2664
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002665/* Return 0 if the message in p is not applicable for GGSN resolution, -1 if
2666 * resolution should be possible but failed, and 1 if resolution was
2667 * successful. *pp will be set to NULL if <1 is returned. */
2668static int gtphub_resolve_ggsn(struct gtphub *hub,
2669 struct gtp_packet_desc *p,
2670 struct gtphub_peer_port **pp)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002671{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +01002672 *pp = NULL;
2673
2674 /* TODO determine from message type whether IEs should be present? */
2675
2676 int rc;
2677 const char *imsi_str;
2678 rc = get_ie_imsi_str(p->ie, 0, &imsi_str);
2679 if (rc < 1)
2680 return rc;
2681 OSMO_ASSERT(imsi_str);
2682
2683 const char *apn_str;
2684 rc = get_ie_apn_str(p->ie, &apn_str);
2685 if (rc < 1)
2686 return rc;
2687 OSMO_ASSERT(apn_str);
2688
2689 *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str);
2690 return (*pp)? 1 : -1;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002691}
2692
2693
2694/* TODO move to osmocom/core/socket.c ? */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002695/* use this in osmo_sock_init() to remove dup. */
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002696/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
2697 to call freeaddrinfo(*result), iff zero is returned. */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002698static int _osmo_getaddrinfo(struct addrinfo **result,
2699 uint16_t family, uint16_t type, uint8_t proto,
2700 const char *host, uint16_t port)
2701{
2702 struct addrinfo hints;
2703 char portbuf[16];
2704
2705 sprintf(portbuf, "%u", port);
2706 memset(&hints, '\0', sizeof(struct addrinfo));
2707 hints.ai_family = family;
2708 if (type == SOCK_RAW) {
2709 /* Workaround for glibc, that returns EAI_SERVICE (-8) if
2710 * SOCK_RAW and IPPROTO_GRE is used.
2711 */
2712 hints.ai_socktype = SOCK_DGRAM;
2713 hints.ai_protocol = IPPROTO_UDP;
2714 } else {
2715 hints.ai_socktype = type;
2716 hints.ai_protocol = proto;
2717 }
2718
2719 return getaddrinfo(host, portbuf, &hints, result);
2720}
2721
2722/* TODO move to osmocom/core/socket.c ? */
2723int osmo_sockaddr_init(struct osmo_sockaddr *addr,
2724 uint16_t family, uint16_t type, uint8_t proto,
2725 const char *host, uint16_t port)
2726{
2727 struct addrinfo *res;
2728 int rc;
2729 rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
2730
2731 if (rc != 0) {
Neels Hofmeyr063a8022015-11-16 14:35:13 +01002732 LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002733 return -EINVAL;
2734 }
2735
2736 OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
2737 memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
2738 addr->l = res->ai_addrlen;
2739 freeaddrinfo(res);
2740
2741 return 0;
2742}
2743
2744int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
2745 char *port_str, size_t port_str_len,
2746 const struct osmo_sockaddr *addr,
2747 int flags)
2748{
2749 int rc;
2750
2751 if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
2752 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
2753 return -1;
2754 }
2755
2756 if (addr->l > sizeof(addr->a)) {
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002757 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
2758 addr->l);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002759 return -1;
2760 }
2761
2762 rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
2763 addr_str, addr_str_len,
2764 port_str, port_str_len,
2765 flags);
2766
2767 if (rc)
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002768 LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
2769 gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
2770 addr->l));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002771
2772 return rc;
2773}
2774
2775const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
2776 char *buf, size_t buf_len)
2777{
2778 const int portbuf_len = 6;
2779 OSMO_ASSERT(buf_len > portbuf_len);
2780 char *portbuf = buf + buf_len - portbuf_len;
2781 buf_len -= portbuf_len;
2782 if (osmo_sockaddr_to_strs(buf, buf_len,
2783 portbuf, portbuf_len,
2784 addr,
2785 NI_NUMERICHOST | NI_NUMERICSERV))
2786 return NULL;
2787
2788 char *pos = buf + strnlen(buf, buf_len-1);
2789 size_t len = buf_len - (pos - buf);
2790
2791 snprintf(pos, len, " port %s", portbuf);
2792 buf[buf_len-1] = '\0';
2793
2794 return buf;
2795}
2796
2797const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
2798{
2799 static char buf[256];
2800 const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
2801 if (! result)
2802 return "(invalid)";
2803 return result;
2804}
2805
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002806int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
2807 const struct osmo_sockaddr *b)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002808{
2809 if (a == b)
2810 return 0;
2811 if (!a)
2812 return -1;
2813 if (!b)
2814 return 1;
2815 if (a->l != b->l) {
2816 /* Lengths are not the same, but determine the order. Will
2817 * anyone ever sort a list by osmo_sockaddr though...? */
2818 int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
2819 if (cmp == 0) {
2820 if (a->l < b->l)
2821 return -1;
2822 else
2823 return 1;
2824 }
2825 return cmp;
2826 }
2827 return memcmp(&a->a, &b->a, a->l);
2828}
2829
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +01002830void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
2831 const struct osmo_sockaddr *src)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02002832{
2833 OSMO_ASSERT(src->l <= sizeof(dst->a));
2834 memcpy(&dst->a, &src->a, src->l);
2835 dst->l = src->l;
2836}