Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1 | /* 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> |
| 27 | #include <sys/socket.h> |
| 28 | #include <netinet/in.h> |
| 29 | #include <arpa/inet.h> |
| 30 | |
| 31 | #include <gtp.h> |
| 32 | #include <gtpie.h> |
| 33 | |
| 34 | #include <openbsc/gtphub.h> |
| 35 | #include <openbsc/debug.h> |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 36 | #include <openbsc/gprs_utils.h> |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 37 | |
| 38 | #include <osmocom/core/utils.h> |
| 39 | #include <osmocom/core/logging.h> |
| 40 | #include <osmocom/core/socket.h> |
| 41 | |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 42 | |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 43 | #define GTPHUB_DEBUG 1 |
| 44 | |
| 45 | static const int GTPH_GC_TICK_SECONDS = 1; |
| 46 | |
| 47 | void *osmo_gtphub_ctx; |
| 48 | |
| 49 | #define LOGERR(fmt, args...) \ |
| 50 | LOGP(DGTPHUB, LOGL_ERROR, fmt, ##args) |
| 51 | |
| 52 | #define LOG(fmt, args...) \ |
| 53 | LOGP(DGTPHUB, LOGL_NOTICE, fmt, ##args) |
| 54 | |
| 55 | #define ZERO_STRUCT(struct_pointer) memset(struct_pointer, '\0', sizeof(*(struct_pointer))) |
| 56 | |
| 57 | /* TODO move this to osmocom/core/select.h ? */ |
| 58 | typedef 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) |
| 62 | #define llist_first(head, type, entry) llist_entry(__llist_first(head), type, entry) |
| 63 | |
| 64 | /* TODO move GTP header stuff to openggsn/gtp/ ? See gtp_decaps*() */ |
| 65 | |
| 66 | enum gtp_rc { |
| 67 | GTP_RC_UNKNOWN = 0, |
| 68 | GTP_RC_TINY = 1, /* no IEs (like ping/pong) */ |
Neels Hofmeyr | e6078bc | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 69 | GTP_RC_PDU_C = 2, /* a real packet with IEs */ |
| 70 | GTP_RC_PDU_U = 3, /* a real packet with User data */ |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 71 | |
| 72 | GTP_RC_TOOSHORT = -1, |
| 73 | GTP_RC_UNSUPPORTED_VERSION = -2, |
| 74 | GTP_RC_INVALID_IE = -3, |
| 75 | }; |
| 76 | |
| 77 | struct gtp_packet_desc { |
| 78 | union gtp_packet *data; |
| 79 | int data_len; |
| 80 | int header_len; |
| 81 | int version; |
| 82 | uint8_t type; |
| 83 | uint16_t seq; |
| 84 | uint32_t header_tei; |
| 85 | int rc; /* enum gtp_rc */ |
| 86 | unsigned int plane_idx; |
| 87 | union gtpie_member *ie[GTPIE_SIZE]; |
| 88 | }; |
| 89 | |
| 90 | void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src) |
| 91 | { |
| 92 | memcpy(gsna, src, sizeof(struct gsn_addr)); |
| 93 | } |
| 94 | |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 95 | int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port, |
| 96 | const struct osmo_sockaddr *sa) |
| 97 | { |
| 98 | char addr_str[256]; |
| 99 | char port_str[6]; |
| 100 | |
| 101 | if (osmo_sockaddr_to_strs(addr_str, sizeof(addr_str), |
| 102 | port_str, sizeof(port_str), |
| 103 | sa, (NI_NUMERICHOST | NI_NUMERICSERV)) |
| 104 | != 0) { |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | if (port) |
| 109 | *port = atoi(port_str); |
| 110 | |
| 111 | return gsn_addr_from_str(gsna, addr_str); |
| 112 | } |
| 113 | |
| 114 | int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str) |
| 115 | { |
| 116 | int af = AF_INET; |
| 117 | gsna->len = 4; |
| 118 | const char *pos = numeric_addr_str; |
| 119 | for (; *pos; pos++) { |
| 120 | if (*pos == ':') { |
| 121 | af = AF_INET6; |
| 122 | gsna->len = 16; |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | int rc = inet_pton(af, numeric_addr_str, gsna->buf); |
| 128 | if (rc != 1) { |
| 129 | LOGERR("Cannot resolve numeric address: '%s'\n", numeric_addr_str); |
| 130 | return -1; |
| 131 | } |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | const char *gsn_addr_to_str(const struct gsn_addr *gsna) |
| 136 | { |
| 137 | static char buf[INET6_ADDRSTRLEN + 1]; |
| 138 | return gsn_addr_to_strb(gsna, buf, sizeof(buf)); |
| 139 | } |
| 140 | |
| 141 | const char *gsn_addr_to_strb(const struct gsn_addr *gsna, |
| 142 | char *strbuf, |
| 143 | int strbuf_len) |
| 144 | { |
| 145 | int af; |
| 146 | switch (gsna->len) { |
| 147 | case 4: |
| 148 | af = AF_INET; |
| 149 | break; |
| 150 | case 16: |
| 151 | af = AF_INET6; |
| 152 | break; |
| 153 | default: |
| 154 | return NULL; |
| 155 | } |
| 156 | |
| 157 | const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len); |
| 158 | if (!r) { |
| 159 | LOGERR("Cannot convert gsn_addr to string: %s: len=%d, buf=%s\n", |
| 160 | strerror(errno), |
| 161 | (int)gsna->len, |
| 162 | osmo_hexdump(gsna->buf, sizeof(gsna->buf))); |
| 163 | } |
| 164 | return r; |
| 165 | } |
| 166 | |
| 167 | int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b) |
| 168 | { |
| 169 | if (a == b) |
| 170 | return 1; |
| 171 | if ((!a) || (!b)) |
| 172 | return 0; |
| 173 | if (a->len != b->len) |
| 174 | return 0; |
| 175 | return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0; |
| 176 | } |
| 177 | |
| 178 | static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p, int idx) |
| 179 | { |
Neels Hofmeyr | e6078bc | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 180 | if (p->rc != GTP_RC_PDU_C) |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 181 | return -1; |
| 182 | |
| 183 | unsigned int len; |
| 184 | /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */ |
| 185 | if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx, |
| 186 | &len, gsna->buf, sizeof(gsna->buf)) |
| 187 | != 0) |
| 188 | return -1; |
| 189 | gsna->len = len; |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p, int idx) |
| 194 | { |
Neels Hofmeyr | e6078bc | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 195 | if (p->rc != GTP_RC_PDU_C) |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 196 | return -1; |
| 197 | |
| 198 | int ie_idx; |
| 199 | ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx); |
| 200 | |
| 201 | if (ie_idx < 0) |
| 202 | return -1; |
| 203 | |
| 204 | struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv; |
| 205 | int ie_l = ntoh16(ie->l); |
| 206 | if (ie_l != gsna->len) { |
| 207 | LOG("Not implemented: replace an IE address of different size:" |
| 208 | " replace %d with %d\n", (int)ie_l, (int)gsna->len); |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | memcpy(ie->v, gsna->buf, (int)ie_l); |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | /* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there. |
| 217 | */ |
| 218 | void validate_gtp0_header(struct gtp_packet_desc *p) |
| 219 | { |
| 220 | const struct gtp0_header *pheader = &(p->data->gtp0.h); |
| 221 | p->rc = GTP_RC_UNKNOWN; |
| 222 | p->header_len = 0; |
| 223 | |
| 224 | OSMO_ASSERT(p->data_len >= 1); |
| 225 | OSMO_ASSERT(p->version == 0); |
| 226 | |
| 227 | if (p->data_len < GTP0_HEADER_SIZE) { |
| 228 | LOGERR("GTP0 packet too short: %d\n", p->data_len); |
| 229 | p->rc = GTP_RC_TOOSHORT; |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | p->type = ntoh8(pheader->type); |
| 234 | p->seq = ntoh16(pheader->seq); |
| 235 | p->header_tei = 0; /* TODO */ |
| 236 | |
| 237 | if (p->data_len == GTP0_HEADER_SIZE) { |
| 238 | p->rc = GTP_RC_TINY; |
| 239 | p->header_len = GTP0_HEADER_SIZE; |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | /* Check packet length field versus length of packet */ |
| 244 | if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) { |
| 245 | LOGERR("GTP packet length field (%d + %d) does not match" |
| 246 | " actual length (%d)\n", |
| 247 | GTP0_HEADER_SIZE, (int)ntoh16(pheader->length), |
| 248 | p->data_len); |
| 249 | p->rc = GTP_RC_TOOSHORT; |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | LOG("GTP v0 TID = %" PRIu64 "\n", pheader->tid); |
| 254 | p->header_len = GTP0_HEADER_SIZE; |
Neels Hofmeyr | e6078bc | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 255 | p->rc = GTP_RC_PDU_C; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* Validate GTP version 1 data, and update p->rc with the result, as well as |
| 259 | * p->header_len in case of a valid header. */ |
| 260 | void validate_gtp1_header(struct gtp_packet_desc *p) |
| 261 | { |
| 262 | const struct gtp1_header_long *pheader = &(p->data->gtp1l.h); |
| 263 | p->rc = GTP_RC_UNKNOWN; |
| 264 | p->header_len = 0; |
| 265 | |
| 266 | OSMO_ASSERT(p->data_len >= 1); |
| 267 | OSMO_ASSERT(p->version == 1); |
| 268 | |
| 269 | if ((p->data_len < GTP1_HEADER_SIZE_LONG) |
| 270 | && (p->data_len != GTP1_HEADER_SIZE_SHORT)){ |
| 271 | LOGERR("GTP packet too short: %d\n", p->data_len); |
| 272 | p->rc = GTP_RC_TOOSHORT; |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | p->type = ntoh8(pheader->type); |
| 277 | p->header_tei = ntoh32(pheader->tei); |
| 278 | p->seq = ntoh16(pheader->seq); |
| 279 | |
| 280 | LOG("|GTPv1\n"); |
| 281 | LOG("| type = %" PRIu8 " 0x%02" PRIx8 "\n", |
| 282 | p->type, p->type); |
| 283 | LOG("| length = %" PRIu16 " 0x%04" PRIx16 "\n", |
| 284 | ntoh16(pheader->length), ntoh16(pheader->length)); |
| 285 | LOG("| TEI = %" PRIu32 " 0x%08" PRIx32 "\n", |
| 286 | p->header_tei, p->header_tei); |
| 287 | LOG("| seq = %" PRIu16 " 0x%04" PRIx16 "\n", |
| 288 | p->seq, p->seq); |
| 289 | LOG("| npdu = %" PRIu8 " 0x%02" PRIx8 "\n", |
| 290 | pheader->npdu, pheader->npdu); |
| 291 | LOG("| next = %" PRIu8 " 0x%02" PRIx8 "\n", |
| 292 | pheader->next, pheader->next); |
| 293 | |
| 294 | if (p->data_len <= GTP1_HEADER_SIZE_LONG) { |
| 295 | p->rc = GTP_RC_TINY; |
| 296 | p->header_len = GTP1_HEADER_SIZE_SHORT; |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | /* Check packet length field versus length of packet */ |
| 301 | if (p->data_len != (ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT)) { |
| 302 | LOGERR("GTP packet length field (%d + %d) does not match" |
| 303 | " actual length (%d)\n", |
| 304 | GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length), |
| 305 | p->data_len); |
| 306 | p->rc = GTP_RC_TOOSHORT; |
| 307 | return; |
| 308 | } |
| 309 | |
Neels Hofmeyr | e6078bc | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 310 | p->rc = GTP_RC_PDU_C; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 311 | p->header_len = GTP1_HEADER_SIZE_LONG; |
| 312 | } |
| 313 | |
| 314 | /* Examine whether p->data of size p->data_len has a valid GTP header. Set |
| 315 | * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum |
| 316 | * gtp_rc). p->data must point at a buffer with p->data_len set. */ |
| 317 | void validate_gtp_header(struct gtp_packet_desc *p) |
| 318 | { |
| 319 | p->rc = GTP_RC_UNKNOWN; |
| 320 | |
| 321 | /* Need at least 1 byte in order to check version */ |
| 322 | if (p->data_len < 1) { |
| 323 | LOGERR("Discarding packet - too small: %d\n", p->data_len); |
| 324 | p->rc = GTP_RC_TOOSHORT; |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | p->version = p->data->flags >> 5; |
| 329 | |
| 330 | switch (p->version) { |
| 331 | case 0: |
| 332 | validate_gtp0_header(p); |
| 333 | break; |
| 334 | case 1: |
| 335 | validate_gtp1_header(p); |
| 336 | break; |
| 337 | default: |
| 338 | LOGERR("Unsupported GTP version: %d\n", p->version); |
| 339 | p->rc = GTP_RC_UNSUPPORTED_VERSION; |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | |
| 345 | /* Return the value of the i'th IMSI IEI by copying to *imsi. |
| 346 | * The first IEI is reached by passing i = 0. |
| 347 | * imsi must point at allocated space of (at least) 8 bytes. |
| 348 | * Return 1 on success, or 0 if not found. */ |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 349 | static int get_ie_imsi(union gtpie_member *ie[], int i, uint8_t *imsi) |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 350 | { |
| 351 | return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0; |
| 352 | } |
| 353 | |
| 354 | /* Analogous to get_ie_imsi(). nsapi must point at a single uint8_t. */ |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 355 | static int get_ie_nsapi(union gtpie_member *ie[], int i, uint8_t *nsapi) |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 356 | { |
| 357 | return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0; |
| 358 | } |
| 359 | |
| 360 | static char imsi_digit_to_char(uint8_t nibble) |
| 361 | { |
| 362 | nibble &= 0x0f; |
| 363 | if (nibble > 9) |
| 364 | return (nibble == 0x0f) ? '\0' : '?'; |
| 365 | return '0' + nibble; |
| 366 | } |
| 367 | |
| 368 | /* Return a human readable IMSI string, in a static buffer. |
| 369 | * imsi must point at 8 octets of IMSI IE encoded IMSI data. */ |
Neels Hofmeyr | ba35d0c | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 370 | static int imsi_to_str(uint8_t *imsi, const char **imsi_str) |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 371 | { |
| 372 | static char str[17]; |
| 373 | int i; |
Neels Hofmeyr | ba35d0c | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 374 | char c; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 375 | |
| 376 | for (i = 0; i < 8; i++) { |
Neels Hofmeyr | ba35d0c | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 377 | c = imsi_digit_to_char(imsi[i]); |
| 378 | if (c == '?') |
| 379 | return -1; |
| 380 | str[2*i] = c; |
| 381 | |
| 382 | c = imsi_digit_to_char(imsi[i] >> 4); |
| 383 | if (c == '?') |
| 384 | return -1; |
| 385 | str[2*i + 1] = c; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 386 | } |
| 387 | str[16] = '\0'; |
Neels Hofmeyr | ba35d0c | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 388 | *imsi_str = str; |
| 389 | return 1; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Neels Hofmeyr | ba35d0c | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 392 | /* Return 0 if not present, 1 if present and decoded successfully, -1 if |
| 393 | * present but cannot be decoded. */ |
| 394 | static int get_ie_imsi_str(union gtpie_member *ie[], int i, const char **imsi_str) |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 395 | { |
| 396 | uint8_t imsi_buf[8]; |
| 397 | if (!get_ie_imsi(ie, i, imsi_buf)) |
Neels Hofmeyr | ba35d0c | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 398 | return 0; |
| 399 | return imsi_to_str(imsi_buf, imsi_str); |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 400 | } |
| 401 | |
Neels Hofmeyr | ba35d0c | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 402 | /* Return 0 if not present, 1 if present and decoded successfully, -1 if |
| 403 | * present but cannot be decoded. */ |
| 404 | static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str) |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 405 | { |
| 406 | static char apn_buf[GSM_APN_LENGTH]; |
| 407 | unsigned int len; |
| 408 | if (gtpie_gettlv(ie, GTPIE_APN, 0, |
| 409 | &len, apn_buf, sizeof(apn_buf)) != 0) |
Neels Hofmeyr | ba35d0c | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 410 | return 0; |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 411 | |
Neels Hofmeyr | ba35d0c | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 412 | if (len < 2) { |
| 413 | LOGERR("APN IE: invalid length: %d\n", |
| 414 | (int)len); |
| 415 | return -1; |
| 416 | } |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 417 | |
| 418 | if (len > (sizeof(apn_buf) - 1)) |
| 419 | len = sizeof(apn_buf) - 1; |
| 420 | apn_buf[len] = '\0'; |
| 421 | |
Neels Hofmeyr | ba35d0c | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 422 | *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len); |
| 423 | if (!(*apn_str)) { |
| 424 | LOGERR("APN IE: present but cannot be decoded: %s\n", |
| 425 | osmo_hexdump((uint8_t*)apn_buf, len)); |
| 426 | return -1; |
| 427 | } |
| 428 | return 1; |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 432 | /* Validate header, and index information elements. Write decoded packet |
| 433 | * information to *res. res->data will point at the given data buffer. On |
| 434 | * error, p->rc is set <= 0 (see enum gtp_rc). */ |
| 435 | static void gtp_decode(const uint8_t *data, int data_len, |
| 436 | unsigned int from_plane_idx, |
| 437 | struct gtp_packet_desc *res) |
| 438 | { |
| 439 | ZERO_STRUCT(res); |
| 440 | res->data = (union gtp_packet*)data; |
| 441 | res->data_len = data_len; |
| 442 | res->plane_idx = from_plane_idx; |
| 443 | |
| 444 | validate_gtp_header(res); |
| 445 | |
| 446 | if (res->rc <= 0) { |
| 447 | LOGERR("INVALID: dropping GTP packet.\n"); |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | LOG("Valid GTP header (v%d)\n", res->version); |
| 452 | |
Neels Hofmeyr | e6078bc | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 453 | if (from_plane_idx == GTPH_PLANE_USER) { |
| 454 | res->rc = GTP_RC_PDU_U; |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | if (res->rc != GTP_RC_PDU_C) { |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 459 | LOG("no IEs in this GTP packet\n"); |
| 460 | return; |
| 461 | } |
| 462 | |
| 463 | if (gtpie_decaps(res->ie, res->version, |
| 464 | (void*)(data + res->header_len), |
| 465 | res->data_len - res->header_len) != 0) { |
| 466 | res->rc = GTP_RC_INVALID_IE; |
Neels Hofmeyr | e6078bc | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 467 | LOGERR("INVALID: cannot decode IEs. Dropping GTP packet.\n"); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 468 | return; |
| 469 | } |
| 470 | |
| 471 | #if GTPHUB_DEBUG |
| 472 | int i; |
| 473 | |
| 474 | for (i = 0; i < 10; i++) { |
Neels Hofmeyr | ba35d0c | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 475 | const char *imsi; |
| 476 | if (get_ie_imsi_str(res->ie, i, &imsi) < 1) |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 477 | break; |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 478 | LOG("| IMSI %s\n", imsi); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | for (i = 0; i < 10; i++) { |
| 482 | uint8_t nsapi; |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 483 | if (!get_ie_nsapi(res->ie, i, &nsapi)) |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 484 | break; |
| 485 | LOG("| NSAPI %d\n", (int)nsapi); |
| 486 | } |
| 487 | |
| 488 | for (i = 0; i < 2; i++) { |
| 489 | struct gsn_addr addr; |
| 490 | if (gsn_addr_get(&addr, res, i) == 0) |
| 491 | LOG("| addr %s\n", gsn_addr_to_str(&addr)); |
| 492 | } |
| 493 | |
| 494 | for (i = 0; i < 10; i++) { |
| 495 | uint32_t tei; |
| 496 | if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0) |
| 497 | break; |
| 498 | LOG("| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n", |
| 499 | tei, tei); |
| 500 | } |
| 501 | |
| 502 | for (i = 0; i < 10; i++) { |
| 503 | uint32_t tei; |
| 504 | if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0) |
| 505 | break; |
| 506 | LOG("| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n", |
| 507 | tei, tei); |
| 508 | } |
| 509 | #endif |
| 510 | } |
| 511 | |
| 512 | |
| 513 | /* expiry */ |
| 514 | |
| 515 | void expiry_init(struct expiry *exq, int expiry_in_seconds) |
| 516 | { |
| 517 | ZERO_STRUCT(exq); |
| 518 | exq->expiry_in_seconds = expiry_in_seconds; |
| 519 | INIT_LLIST_HEAD(&exq->items); |
| 520 | } |
| 521 | |
| 522 | void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now) |
| 523 | { |
| 524 | item->expiry = now + exq->expiry_in_seconds; |
| 525 | |
| 526 | /* Add/move to the tail to always sort by expiry, ascending. */ |
| 527 | llist_del(&item->entry); |
| 528 | llist_add_tail(&item->entry, &exq->items); |
| 529 | } |
| 530 | |
| 531 | int expiry_tick(struct expiry *exq, time_t now) |
| 532 | { |
| 533 | int expired = 0; |
| 534 | struct expiring_item *m, *n; |
| 535 | llist_for_each_entry_safe(m, n, &exq->items, entry) { |
| 536 | if (m->expiry <= now) { |
| 537 | expiring_item_del(m); |
| 538 | expired ++; |
| 539 | } else { |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 540 | /* The items are added sorted by expiry. So when we hit |
| 541 | * an unexpired entry, only more unexpired ones will |
| 542 | * follow. */ |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | return expired; |
| 547 | } |
| 548 | |
| 549 | void expiring_item_init(struct expiring_item *item) |
| 550 | { |
| 551 | ZERO_STRUCT(item); |
| 552 | INIT_LLIST_HEAD(&item->entry); |
| 553 | } |
| 554 | |
| 555 | void expiring_item_del(struct expiring_item *item) |
| 556 | { |
| 557 | OSMO_ASSERT(item); |
| 558 | llist_del(&item->entry); |
| 559 | INIT_LLIST_HEAD(&item->entry); |
| 560 | if (item->del_cb) { |
| 561 | /* avoid loops */ |
| 562 | del_cb_t del_cb = item->del_cb; |
| 563 | item->del_cb = 0; |
| 564 | (del_cb)(item); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | |
| 569 | /* nr_map, nr_pool */ |
| 570 | |
| 571 | void nr_pool_init(struct nr_pool *pool) |
| 572 | { |
| 573 | *pool = (struct nr_pool){}; |
| 574 | } |
| 575 | |
| 576 | nr_t nr_pool_next(struct nr_pool *pool) |
| 577 | { |
| 578 | pool->last_nr ++; |
| 579 | |
| 580 | OSMO_ASSERT(pool->last_nr > 0); |
| 581 | /* TODO: gracefully handle running out of TEIs. */ |
| 582 | /* TODO: random TEIs. */ |
| 583 | |
| 584 | return pool->last_nr; |
| 585 | } |
| 586 | |
| 587 | void nr_map_init(struct nr_map *map, struct nr_pool *pool, |
| 588 | struct expiry *exq) |
| 589 | { |
| 590 | ZERO_STRUCT(map); |
| 591 | map->pool = pool; |
| 592 | map->add_items_to_expiry = exq; |
| 593 | INIT_LLIST_HEAD(&map->mappings); |
| 594 | } |
| 595 | |
| 596 | void nr_mapping_init(struct nr_mapping *m) |
| 597 | { |
| 598 | ZERO_STRUCT(m); |
| 599 | INIT_LLIST_HEAD(&m->entry); |
| 600 | expiring_item_init(&m->expiry_entry); |
| 601 | } |
| 602 | |
| 603 | void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now) |
| 604 | { |
| 605 | /* Generate a mapped number */ |
| 606 | mapping->repl = nr_pool_next(map->pool); |
| 607 | |
| 608 | /* Add to the tail to always yield a list sorted by expiry, in |
| 609 | * ascending order. */ |
| 610 | llist_add_tail(&mapping->entry, &map->mappings); |
| 611 | if (map->add_items_to_expiry) |
| 612 | expiry_add(map->add_items_to_expiry, |
| 613 | &mapping->expiry_entry, |
| 614 | now); |
| 615 | } |
| 616 | |
| 617 | void nr_map_clear(struct nr_map *map) |
| 618 | { |
| 619 | struct nr_mapping *m; |
| 620 | struct nr_mapping *n; |
| 621 | llist_for_each_entry_safe(m, n, &map->mappings, entry) { |
| 622 | nr_mapping_del(m); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | int nr_map_empty(const struct nr_map *map) |
| 627 | { |
| 628 | return llist_empty(&map->mappings); |
| 629 | } |
| 630 | |
| 631 | struct nr_mapping *nr_map_get(const struct nr_map *map, |
| 632 | void *origin, nr_t nr_orig) |
| 633 | { |
| 634 | struct nr_mapping *mapping; |
| 635 | llist_for_each_entry(mapping, &map->mappings, entry) { |
| 636 | if ((mapping->origin == origin) |
| 637 | && (mapping->orig == nr_orig)) |
| 638 | return mapping; |
| 639 | } |
| 640 | /* Not found. */ |
| 641 | return NULL; |
| 642 | } |
| 643 | |
| 644 | struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl) |
| 645 | { |
| 646 | struct nr_mapping *mapping; |
| 647 | llist_for_each_entry(mapping, &map->mappings, entry) { |
| 648 | if (mapping->repl == nr_repl) { |
| 649 | return mapping; |
| 650 | } |
| 651 | } |
| 652 | /* Not found. */ |
| 653 | return NULL; |
| 654 | } |
| 655 | |
| 656 | void nr_mapping_del(struct nr_mapping *mapping) |
| 657 | { |
| 658 | OSMO_ASSERT(mapping); |
| 659 | llist_del(&mapping->entry); |
| 660 | INIT_LLIST_HEAD(&mapping->entry); |
| 661 | expiring_item_del(&mapping->expiry_entry); |
| 662 | } |
| 663 | |
| 664 | |
| 665 | /* gtphub */ |
| 666 | |
| 667 | const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = { |
| 668 | "CTRL", |
| 669 | "USER", |
| 670 | }; |
| 671 | |
| 672 | const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = { |
| 673 | 2123, |
| 674 | 2152, |
| 675 | }; |
| 676 | |
| 677 | time_t gtphub_now(void) |
| 678 | { |
| 679 | struct timespec now_tp; |
| 680 | OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0); |
| 681 | return now_tp.tv_sec; |
| 682 | } |
| 683 | |
| 684 | /* Remove a gtphub_peer from its list and free it. */ |
| 685 | static void gtphub_peer_del(struct gtphub_peer *peer) |
| 686 | { |
| 687 | nr_map_clear(&peer->seq_map); |
| 688 | llist_del(&peer->entry); |
| 689 | talloc_free(peer); |
| 690 | } |
| 691 | |
| 692 | static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa) |
| 693 | { |
| 694 | OSMO_ASSERT(llist_empty(&pa->ports)); |
| 695 | llist_del(&pa->entry); |
| 696 | talloc_free(pa); |
| 697 | } |
| 698 | |
| 699 | static void gtphub_peer_port_del(struct gtphub_peer_port *pp) |
| 700 | { |
| 701 | OSMO_ASSERT(pp->ref_count == 0); |
| 702 | llist_del(&pp->entry); |
| 703 | talloc_free(pp); |
| 704 | } |
| 705 | |
| 706 | /* From the information in the gtp_packet_desc, return the address of a GGSN. |
| 707 | * Return -1 on error. */ |
Neels Hofmeyr | ba35d0c | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 708 | static int gtphub_resolve_ggsn(struct gtphub *hub, |
| 709 | struct gtp_packet_desc *p, |
| 710 | struct gtphub_peer_port **pp); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 711 | |
| 712 | /* See gtphub_ext.c (wrapped by unit test) */ |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 713 | struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub, |
| 714 | const char *imsi_str, |
| 715 | const char *apn_ni_str); |
| 716 | int gtphub_ares_init(struct gtphub *hub); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 717 | |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 718 | static void gtphub_zero(struct gtphub *hub) |
| 719 | { |
| 720 | ZERO_STRUCT(hub); |
Neels Hofmeyr | 97b5f14 | 2015-11-11 17:27:01 +0100 | [diff] [blame] | 721 | INIT_LLIST_HEAD(&hub->ggsn_lookups); |
| 722 | INIT_LLIST_HEAD(&hub->resolved_ggsns); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | static int gtphub_sock_init(struct osmo_fd *ofd, |
| 726 | const struct gtphub_cfg_addr *addr, |
| 727 | osmo_fd_cb_t cb, |
| 728 | void *data, |
| 729 | int ofd_id) |
| 730 | { |
| 731 | if (!addr->addr_str) { |
| 732 | LOGERR("Cannot bind: empty address.\n"); |
| 733 | return -1; |
| 734 | } |
| 735 | if (!addr->port) { |
| 736 | LOGERR("Cannot bind: zero port not permitted.\n"); |
| 737 | return -1; |
| 738 | } |
| 739 | |
| 740 | ofd->when = BSC_FD_READ; |
| 741 | ofd->cb = cb; |
| 742 | ofd->data = data; |
| 743 | ofd->priv_nr = ofd_id; |
| 744 | |
| 745 | int rc; |
| 746 | rc = osmo_sock_init_ofd(ofd, |
| 747 | AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, |
| 748 | addr->addr_str, addr->port, |
| 749 | OSMO_SOCK_F_BIND); |
| 750 | if (rc < 1) { |
| 751 | LOGERR("Cannot bind to %s port %d (rc %d)\n", |
| 752 | addr->addr_str, (int)addr->port, rc); |
| 753 | return -1; |
| 754 | } |
| 755 | |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | static void gtphub_bind_init(struct gtphub_bind *b) |
| 760 | { |
| 761 | ZERO_STRUCT(b); |
| 762 | |
| 763 | INIT_LLIST_HEAD(&b->peers); |
| 764 | } |
| 765 | |
| 766 | static int gtphub_bind_start(struct gtphub_bind *b, |
| 767 | const struct gtphub_cfg_bind *cfg, |
| 768 | osmo_fd_cb_t cb, void *cb_data, |
| 769 | unsigned int ofd_id) |
| 770 | { |
| 771 | if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0) |
| 772 | return -1; |
| 773 | if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0) |
| 774 | return -1; |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | /* Recv datagram from from->fd, optionally write sender's address to *from_addr. |
| 779 | * Return the number of bytes read, zero on error. */ |
| 780 | static int gtphub_read(const struct osmo_fd *from, |
| 781 | struct osmo_sockaddr *from_addr, |
| 782 | uint8_t *buf, size_t buf_len) |
| 783 | { |
| 784 | /* recvfrom requires the available length to be set in *from_addr_len. */ |
| 785 | if (from_addr) |
| 786 | from_addr->l = sizeof(from_addr->a); |
| 787 | |
| 788 | errno = 0; |
| 789 | ssize_t received = recvfrom(from->fd, buf, buf_len, 0, |
| 790 | (struct sockaddr*)&from_addr->a, &from_addr->l); |
| 791 | /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message |
| 792 | * is not truncated. Then maybe reduce buf's size. */ |
| 793 | |
| 794 | if (received <= 0) { |
| 795 | if (errno != EAGAIN) |
| 796 | LOGERR("error: %s\n", strerror(errno)); |
| 797 | return 0; |
| 798 | } |
| 799 | |
| 800 | if (from_addr) { |
| 801 | LOG("from %s\n", osmo_sockaddr_to_str(from_addr)); |
| 802 | } |
| 803 | |
| 804 | if (received <= 0) { |
| 805 | LOGERR("error: %s\n", strerror(errno)); |
| 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | LOG("Received %d\n%s\n", (int)received, osmo_hexdump(buf, received)); |
| 810 | return received; |
| 811 | } |
| 812 | |
| 813 | inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp) |
| 814 | { |
| 815 | OSMO_ASSERT(pp->ref_count < UINT_MAX); |
| 816 | pp->ref_count++; |
| 817 | } |
| 818 | |
| 819 | inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp) |
| 820 | { |
| 821 | OSMO_ASSERT(pp->ref_count > 0); |
| 822 | pp->ref_count--; |
| 823 | } |
| 824 | |
| 825 | inline void set_seq(struct gtp_packet_desc *p, uint16_t seq) |
| 826 | { |
| 827 | OSMO_ASSERT(p->version == 1); |
| 828 | p->data->gtp1l.h.seq = hton16(seq); |
| 829 | p->seq = seq; |
| 830 | } |
| 831 | |
| 832 | inline void set_tei(struct gtp_packet_desc *p, uint32_t tei) |
| 833 | { |
| 834 | OSMO_ASSERT(p->version == 1); |
| 835 | p->data->gtp1l.h.tei = hton32(tei); |
| 836 | p->header_tei = tei; |
| 837 | } |
| 838 | |
| 839 | static void gtphub_mapping_del_cb(struct expiring_item *expi); |
| 840 | |
| 841 | static struct nr_mapping *gtphub_mapping_new() |
| 842 | { |
| 843 | struct nr_mapping *nrm; |
| 844 | nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping); |
| 845 | OSMO_ASSERT(nrm); |
| 846 | |
| 847 | nr_mapping_init(nrm); |
| 848 | nrm->expiry_entry.del_cb = gtphub_mapping_del_cb; |
| 849 | return nrm; |
| 850 | } |
| 851 | |
| 852 | static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf, int buflen) |
| 853 | { |
| 854 | if (llist_empty(&peer->addresses)) |
| 855 | return "(addressless)"; |
| 856 | |
| 857 | struct gtphub_peer_addr *a = llist_first(&peer->addresses, |
| 858 | struct gtphub_peer_addr, |
| 859 | entry); |
| 860 | return gsn_addr_to_strb(&a->addr, buf, buflen); |
| 861 | } |
| 862 | |
| 863 | static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf, int buflen) |
| 864 | { |
| 865 | if (!port) |
| 866 | return "(null port)"; |
| 867 | |
| 868 | snprintf(buf, buflen, "%s port %d", |
| 869 | gsn_addr_to_str(&port->peer_addr->addr), |
| 870 | (int)port->port); |
| 871 | return buf; |
| 872 | } |
| 873 | |
| 874 | const char *gtphub_peer_str(struct gtphub_peer *peer) |
| 875 | { |
| 876 | static char buf[256]; |
| 877 | return gtphub_peer_strb(peer, buf, sizeof(buf)); |
| 878 | } |
| 879 | |
| 880 | const char *gtphub_peer_str2(struct gtphub_peer *peer) |
| 881 | { |
| 882 | static char buf[256]; |
| 883 | return gtphub_peer_strb(peer, buf, sizeof(buf)); |
| 884 | } |
| 885 | |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 886 | const char *gtphub_port_str(struct gtphub_peer_port *port) |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 887 | { |
| 888 | static char buf[256]; |
| 889 | return gtphub_port_strb(port, buf, sizeof(buf)); |
| 890 | } |
| 891 | |
| 892 | static const char *gtphub_port_str2(struct gtphub_peer_port *port) |
| 893 | { |
| 894 | static char buf[256]; |
| 895 | return gtphub_port_strb(port, buf, sizeof(buf)); |
| 896 | } |
| 897 | |
| 898 | static void gtphub_mapping_del_cb(struct expiring_item *expi) |
| 899 | { |
| 900 | expi->del_cb = 0; /* avoid recursion loops */ |
| 901 | |
| 902 | struct nr_mapping *nrm = container_of(expi, |
| 903 | struct nr_mapping, |
| 904 | expiry_entry); |
| 905 | llist_del(&nrm->entry); |
| 906 | INIT_LLIST_HEAD(&nrm->entry); /* mark unused */ |
| 907 | |
| 908 | /* Just for log */ |
| 909 | struct gtphub_peer_port *from = nrm->origin; |
| 910 | OSMO_ASSERT(from); |
| 911 | LOG("expired: %d: nr mapping from %s: %d->%d\n", |
| 912 | (int)nrm->expiry_entry.expiry, |
| 913 | gtphub_port_str(from), |
| 914 | (int)nrm->orig, (int)nrm->repl); |
| 915 | |
| 916 | gtphub_port_ref_count_dec(from); |
| 917 | |
| 918 | talloc_free(nrm); |
| 919 | } |
| 920 | |
| 921 | static struct nr_mapping *gtphub_mapping_have(struct nr_map *map, |
| 922 | struct gtphub_peer_port *from, |
| 923 | nr_t orig_nr, |
| 924 | time_t now) |
| 925 | { |
| 926 | struct nr_mapping *nrm; |
| 927 | |
| 928 | nrm = nr_map_get(map, from, orig_nr); |
| 929 | |
| 930 | if (!nrm) { |
| 931 | nrm = gtphub_mapping_new(); |
| 932 | nrm->orig = orig_nr; |
| 933 | nrm->origin = from; |
| 934 | nr_map_add(map, nrm, now); |
| 935 | gtphub_port_ref_count_inc(from); |
| 936 | LOG("peer %s: MAP %d --> %d\n", |
| 937 | gtphub_port_str(from), |
| 938 | (int)(nrm->orig), (int)(nrm->repl)); |
| 939 | } else { |
| 940 | /* restart expiry timeout */ |
| 941 | expiry_add(map->add_items_to_expiry, &nrm->expiry_entry, |
| 942 | now); |
| 943 | } |
| 944 | |
| 945 | OSMO_ASSERT(nrm); |
| 946 | return nrm; |
| 947 | } |
| 948 | |
| 949 | static uint32_t gtphub_tei_mapping_have(struct gtphub *hub, |
| 950 | int plane_idx, |
| 951 | struct gtphub_peer_port *from, |
| 952 | uint32_t orig_tei, |
| 953 | time_t now) |
| 954 | { |
| 955 | struct nr_mapping *nrm = gtphub_mapping_have(&hub->tei_map[plane_idx], |
| 956 | from, orig_tei, now); |
| 957 | LOG("New %s TEI: (from %s, TEI %d) <-- TEI %d\n", |
| 958 | gtphub_plane_idx_names[plane_idx], |
| 959 | gtphub_port_str(from), |
| 960 | (int)orig_tei, (int)nrm->repl); |
| 961 | |
| 962 | return (uint32_t)nrm->repl; |
| 963 | } |
| 964 | |
Neels Hofmeyr | 05c593f | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 965 | static void gtphub_map_seq(struct gtp_packet_desc *p, |
| 966 | struct gtphub_peer_port *from_port, |
| 967 | struct gtphub_peer_port *to_port, |
| 968 | time_t now) |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 969 | { |
| 970 | /* Store a mapping in to_peer's map, so when we later receive a GTP |
| 971 | * packet back from to_peer, the seq nr can be unmapped back to its |
| 972 | * origin (from_peer here). */ |
| 973 | struct nr_mapping *nrm; |
| 974 | nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map, |
| 975 | from_port, p->seq, now); |
| 976 | |
| 977 | /* Change the GTP packet to yield the new, mapped seq nr */ |
| 978 | set_seq(p, nrm->repl); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p, |
| 982 | struct gtphub_peer_port *responding_port) |
| 983 | { |
| 984 | OSMO_ASSERT(p->version == 1); |
| 985 | struct nr_mapping *nrm = nr_map_get_inv(&responding_port->peer_addr->peer->seq_map, |
| 986 | p->seq); |
| 987 | if (!nrm) |
| 988 | return NULL; |
| 989 | LOG("peer %p: UNMAP %d <-- %d\n", nrm->origin, (int)(nrm->orig), (int)(nrm->repl)); |
| 990 | set_seq(p, nrm->orig); |
| 991 | return nrm->origin; |
| 992 | } |
| 993 | |
| 994 | static void gtphub_check_restart_counter(struct gtphub *hub, |
| 995 | struct gtp_packet_desc *p, |
| 996 | struct gtphub_peer_port *from) |
| 997 | { |
| 998 | /* TODO */ |
| 999 | /* If the peer is sending a Recovery IE (7.7.11) with a restart counter |
| 1000 | * that doesn't match the peer's previously sent restart counter, clear |
| 1001 | * that peer and cancel PDP contexts. */ |
| 1002 | } |
| 1003 | |
| 1004 | static void gtphub_map_restart_counter(struct gtphub *hub, |
| 1005 | struct gtp_packet_desc *p, |
| 1006 | struct gtphub_peer_port *from, |
| 1007 | struct gtphub_peer_port *to) |
| 1008 | { |
| 1009 | /* TODO */ |
| 1010 | } |
| 1011 | |
| 1012 | /* gtphub_map_ie_teis() and gtphub_unmap_header_tei(): |
| 1013 | * |
| 1014 | * TEI mapping must happen symmetrically. An SGSN contacts gtphub instead of N |
| 1015 | * GGSNs, and a GGSN replies to gtphub for N SGSNs. From either end, TEIs may |
| 1016 | * collide: two GGSNs picking the same TEIs, or two SGSNs picking the same |
| 1017 | * TEIs. Since the opposite side sees the sender address being gtphub's |
| 1018 | * address, TEIs among the SGSNs, and among the GGSNs, must not overlap. If a |
| 1019 | * peer sends a TEI already sent before from a peer of the same side, gtphub |
| 1020 | * replaces it with a TEI not yet seen from that side and remembers the |
| 1021 | * mapping. |
| 1022 | * |
| 1023 | * Consider two SGSNs A and B contacting two GGSNs C and D thru gtphub. |
| 1024 | * |
| 1025 | * A: Create PDP Ctx, I have TEI 1. |
| 1026 | * ---> gtphub: A has TEI 1, sending 1 for C. |
| 1027 | * ---> C: gtphub has TEI 1. |
| 1028 | * <--- C: Response to TEI 1: I have TEI 11. |
| 1029 | * <--- gtphub: ok, telling A: 11. |
| 1030 | * A: gtphub's first TEI is 11. (1) |
| 1031 | * |
| 1032 | * B: Create PDP Ctx, I have TEIs 1. |
| 1033 | * ---> gtphub: 1 already taken for C, sending 2 for B. (map) |
| 1034 | * ---> C: gtphub also has 2. |
| 1035 | * <--- C: Response to TEI 2: I have TEI 12. |
| 1036 | * <--- gtphub: ok, TEI 2 is actually B with TEI 1. (unmap) |
| 1037 | * B: gtphub's first TEI is 12, as far as I can tell. |
| 1038 | * |
| 1039 | * Now the second GGSN comes into play: |
| 1040 | * |
| 1041 | * A: Create PDP Ctx, I have TEI 2. |
| 1042 | * ---> gtphub: A also has TEI 2, but for D, sending 1. (2) |
| 1043 | * ---> D: gtphub has 1. |
| 1044 | * <--- D: Response to TEI 1: I have TEI 11. |
| 1045 | * <--- gtphub: from D, 1 is A. 11 already taken by C, sending 13. (3) |
| 1046 | * A: gtphub also has TEI 13. (4) |
| 1047 | * |
| 1048 | * And some messages routed through: |
| 1049 | * |
| 1050 | * A: message to TEI 11, see (1). |
| 1051 | * ---> gtphub: ok, telling C with TEI 11. |
| 1052 | * ---> C: I see, 11 means reply with 1. |
| 1053 | * <--- C: Response to TEI 1 |
| 1054 | * <--- gtphub: 1 from C is actually for A with TEI 1. |
| 1055 | * A: ah, my TEI 1, thanks! |
| 1056 | * |
| 1057 | * A: message to TEI 13, see (4). |
| 1058 | * ---> gtphub: ok, but not 13, D wanted TEI 11 instead, see (3). |
| 1059 | * ---> D: I see, 11 means reply with 1. |
| 1060 | * <--- D: Response to TEI 1 |
| 1061 | * <--- gtphub: 1 from D is actually for A with TEI 2, see (2). |
| 1062 | * A: ah, my TEI 2, thanks! |
| 1063 | * |
| 1064 | * What if a GGSN initiates a request: |
| 1065 | * |
| 1066 | * <--- D: Request to gtphub TEI 1 |
| 1067 | * <--- gtphub: 1 from D is for A with 2, see (2). |
| 1068 | * A: my TEI 2 means reply with 13. |
| 1069 | * ---> gtphub: 13 was D with 11, see (3). |
| 1070 | * ---> D: 11 from gtphub: a reply to my request for TEI 1. |
| 1071 | * |
| 1072 | * Note that usually, it's the sequence numbers that route a response back to |
| 1073 | * the requesting peer. Nevertheless, the TEI mappings must be carried out to |
| 1074 | * replace the TEIs in the GTP packet that is relayed. |
| 1075 | * |
| 1076 | * Also note: the TEI in the GTP header is "reversed" from the TEI in the IEs: |
| 1077 | * the TEI in the header is used to send something *to* a peer, while the TEI |
| 1078 | * in e.g. a Create PDP Context Request's IE is for routing messages *back* |
| 1079 | * later. */ |
| 1080 | |
| 1081 | static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p, |
| 1082 | struct gtphub *hub, |
| 1083 | struct gtp_packet_desc *p, |
| 1084 | struct gtphub_peer_port *from_port) |
| 1085 | { |
| 1086 | OSMO_ASSERT(p->version == 1); |
| 1087 | *to_port_p = NULL; |
| 1088 | |
| 1089 | /* If the header's TEI is zero, no PDP context has been established |
| 1090 | * yet. If nonzero, a mapping should actually already exist for this |
| 1091 | * TEI, since it must have been announced in a PDP context creation. */ |
| 1092 | uint32_t tei = p->header_tei; |
| 1093 | if (!tei) |
| 1094 | return 0; |
| 1095 | |
| 1096 | /* to_peer has previously announced a TEI, which was stored and |
| 1097 | * mapped in from_peer's tei_map. */ |
| 1098 | struct nr_mapping *nrm; |
| 1099 | nrm = nr_map_get_inv(&hub->tei_map[p->plane_idx], tei); |
| 1100 | if (!nrm) { |
| 1101 | LOGERR("Received unknown TEI %" PRIu32 " from %s\n", |
| 1102 | tei, gtphub_port_str(from_port)); |
| 1103 | return -1; |
| 1104 | } |
| 1105 | |
| 1106 | struct gtphub_peer_port *to_port = nrm->origin; |
| 1107 | uint32_t unmapped_tei = nrm->orig; |
| 1108 | set_tei(p, unmapped_tei); |
| 1109 | |
| 1110 | LOG("Unmapped TEI coming from %s: %d -> %d (to %s)\n", |
| 1111 | gtphub_port_str(from_port), tei, unmapped_tei, |
| 1112 | gtphub_port_str2(to_port)); |
| 1113 | |
| 1114 | *to_port_p = to_port; |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | /* Read GSN address IEs from p, and make sure these peer addresses exist in |
| 1119 | * bind[plane_idx] with default ports, in their respective planes (both Ctrl |
| 1120 | * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into |
| 1121 | * the packet p. */ |
| 1122 | static int gtphub_handle_pdp_ctx_ies(struct gtphub *hub, |
| 1123 | struct gtphub_bind from_bind[], |
| 1124 | struct gtphub_bind to_bind[], |
| 1125 | struct gtp_packet_desc *p, |
| 1126 | time_t now) |
| 1127 | { |
| 1128 | OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL); |
| 1129 | |
| 1130 | int rc; |
| 1131 | int plane_idx; |
| 1132 | |
| 1133 | switch (p->type) { |
| 1134 | case GTP_CREATE_PDP_REQ: |
| 1135 | case GTP_CREATE_PDP_RSP: |
| 1136 | /* Go for it below */ |
| 1137 | break; |
| 1138 | default: |
| 1139 | /* Nothing to do for this message type. */ |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
| 1143 | /* TODO enforce a Request only from SGSN, a Response only from GGSN? */ |
| 1144 | |
| 1145 | osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1), |
| 1146 | plane_nrs_match_GSN_addr_IE_indices); |
| 1147 | |
| 1148 | uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI }; |
| 1149 | int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ); |
| 1150 | |
| 1151 | for (plane_idx = 0; plane_idx < 2; plane_idx++) { |
| 1152 | struct gsn_addr addr_from_ie; |
| 1153 | uint32_t tei_from_ie; |
| 1154 | int ie_idx; |
| 1155 | |
| 1156 | /* Fetch GSN Address and TEI from IEs */ |
| 1157 | rc = gsn_addr_get(&addr_from_ie, p, plane_idx); |
| 1158 | if (rc) { |
| 1159 | LOGERR("Cannot read %s GSN Address IE\n", |
| 1160 | gtphub_plane_idx_names[plane_idx]); |
| 1161 | return -1; |
| 1162 | } |
| 1163 | LOG("Read %s GSN addr %s (%d)\n", |
| 1164 | gtphub_plane_idx_names[plane_idx], |
| 1165 | gsn_addr_to_str(&addr_from_ie), |
| 1166 | addr_from_ie.len); |
| 1167 | |
| 1168 | ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0); |
| 1169 | if (ie_idx < 0) { |
| 1170 | if (ie_mandatory) { |
| 1171 | LOGERR("Create PDP Context message invalid:" |
| 1172 | " missing IE %d\n", (int)ie_type[plane_idx]); |
| 1173 | return -1; |
| 1174 | } |
| 1175 | tei_from_ie = 0; |
| 1176 | } |
| 1177 | else |
| 1178 | tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v); |
| 1179 | |
| 1180 | /* Make sure an entry for this peer address with default port |
| 1181 | * exists */ |
| 1182 | struct gtphub_peer_port *peer_from_ie = |
| 1183 | gtphub_port_have(hub, &from_bind[plane_idx], |
| 1184 | &addr_from_ie, |
| 1185 | gtphub_plane_idx_default_port[plane_idx]); |
| 1186 | |
| 1187 | if (tei_from_ie) { |
| 1188 | /* Create TEI mapping and replace in GTP packet IE */ |
| 1189 | uint32_t mapped_tei = |
| 1190 | gtphub_tei_mapping_have(hub, plane_idx, |
| 1191 | peer_from_ie, |
| 1192 | tei_from_ie, |
| 1193 | now); |
| 1194 | p->ie[ie_idx]->tv4.v = hton32(mapped_tei); |
| 1195 | } |
| 1196 | |
| 1197 | /* Replace the GSN address to reflect gtphub. */ |
| 1198 | rc = gsn_addr_put(&to_bind[plane_idx].local_addr, p, plane_idx); |
| 1199 | if (rc) { |
| 1200 | LOGERR("Cannot write %s GSN Address IE\n", |
| 1201 | gtphub_plane_idx_names[plane_idx]); |
| 1202 | return -1; |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
| 1209 | static int gtphub_write(const struct osmo_fd *to, |
| 1210 | const struct osmo_sockaddr *to_addr, |
| 1211 | const uint8_t *buf, size_t buf_len) |
| 1212 | { |
| 1213 | errno = 0; |
| 1214 | ssize_t sent = sendto(to->fd, buf, buf_len, 0, |
| 1215 | (struct sockaddr*)&to_addr->a, to_addr->l); |
| 1216 | |
| 1217 | if (to_addr) { |
| 1218 | LOG("to %s\n", osmo_sockaddr_to_str(to_addr)); |
| 1219 | } |
| 1220 | |
| 1221 | if (sent == -1) { |
| 1222 | LOGERR("error: %s\n", strerror(errno)); |
| 1223 | return -EINVAL; |
| 1224 | } |
| 1225 | |
| 1226 | if (sent != buf_len) |
| 1227 | LOGERR("sent(%d) != data_len(%d)\n", (int)sent, (int)buf_len); |
| 1228 | else |
| 1229 | LOG("Sent %d\n%s\n", (int)sent, osmo_hexdump(buf, sent)); |
| 1230 | |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
| 1234 | static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what) |
| 1235 | { |
| 1236 | unsigned int plane_idx = from_ggsns_ofd->priv_nr; |
| 1237 | OSMO_ASSERT(plane_idx < GTPH_PLANE_N); |
| 1238 | LOG("\n\n=== reading from GGSN (%s)\n", gtphub_plane_idx_names[plane_idx]); |
| 1239 | if (!(what & BSC_FD_READ)) |
| 1240 | return 0; |
| 1241 | |
| 1242 | struct gtphub *hub = from_ggsns_ofd->data; |
| 1243 | |
| 1244 | static uint8_t buf[4096]; |
| 1245 | struct osmo_sockaddr from_addr; |
| 1246 | struct osmo_sockaddr to_addr; |
| 1247 | struct osmo_fd *to_ofd; |
Neels Hofmeyr | 97b5f14 | 2015-11-11 17:27:01 +0100 | [diff] [blame] | 1248 | int len; |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1249 | uint8_t *reply_buf; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1250 | |
| 1251 | len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf)); |
| 1252 | if (len < 1) |
| 1253 | return 0; |
| 1254 | |
| 1255 | len = gtphub_from_ggsns_handle_buf(hub, plane_idx, &from_addr, buf, len, |
| 1256 | gtphub_now(), |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1257 | &reply_buf, &to_ofd, &to_addr); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1258 | if (len < 1) |
| 1259 | return 0; |
| 1260 | |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1261 | return gtphub_write(to_ofd, &to_addr, reply_buf, len); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | static int gtphub_unmap(struct gtphub *hub, |
| 1265 | struct gtp_packet_desc *p, |
| 1266 | struct gtphub_peer_port *from, |
| 1267 | struct gtphub_peer_port *to_proxy, |
| 1268 | struct gtphub_peer_port **final_unmapped, |
| 1269 | struct gtphub_peer_port **unmapped_from_seq, |
| 1270 | struct gtphub_peer_port **unmapped_from_tei) |
| 1271 | { |
| 1272 | /* Always (try to) unmap sequence and TEI numbers, which need to be |
| 1273 | * replaced in the packet. Either way, give precedence to the proxy, if |
| 1274 | * configured. */ |
| 1275 | |
| 1276 | struct gtphub_peer_port *from_seq = NULL; |
| 1277 | struct gtphub_peer_port *from_tei = NULL; |
| 1278 | struct gtphub_peer_port *unmapped = NULL; |
| 1279 | |
| 1280 | if (unmapped_from_seq) |
| 1281 | *unmapped_from_seq = from_seq; |
| 1282 | if (unmapped_from_tei) |
| 1283 | *unmapped_from_tei = from_tei; |
| 1284 | if (final_unmapped) |
| 1285 | *final_unmapped = unmapped; |
| 1286 | |
| 1287 | from_seq = gtphub_unmap_seq(p, from); |
| 1288 | |
| 1289 | if (gtphub_unmap_header_tei(&from_tei, hub, p, from) < 0) |
| 1290 | return -1; |
| 1291 | |
| 1292 | struct gtphub_peer *from_peer = from->peer_addr->peer; |
| 1293 | if (from_seq && from_tei && (from_seq != from_tei)) { |
| 1294 | LOGERR("Seq unmap and TEI unmap yield two different peers. Using seq unmap." |
| 1295 | "(from %s %s: seq %d yields %s, tei %u yields %s)\n", |
| 1296 | gtphub_plane_idx_names[p->plane_idx], |
| 1297 | gtphub_peer_str(from_peer), |
| 1298 | (int)p->seq, |
| 1299 | gtphub_port_str(from_seq), |
| 1300 | (int)p->header_tei, |
| 1301 | gtphub_port_str2(from_tei) |
| 1302 | ); |
| 1303 | } |
| 1304 | unmapped = (from_seq? from_seq : from_tei); |
| 1305 | |
| 1306 | if (unmapped && to_proxy && (unmapped != to_proxy)) { |
| 1307 | LOGERR("Unmap yields a different peer than the configured proxy. Using proxy." |
| 1308 | " unmapped: %s proxy: %s\n", |
| 1309 | gtphub_port_str(unmapped), |
| 1310 | gtphub_port_str2(to_proxy) |
| 1311 | ); |
| 1312 | } |
| 1313 | unmapped = (to_proxy? to_proxy : unmapped); |
| 1314 | |
| 1315 | if (!unmapped) { |
| 1316 | /* Return no error, but returned pointers are all NULL. */ |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
| 1320 | LOG("from seq %p; from tei %p; unmapped => %p\n", |
| 1321 | from_seq, from_tei, unmapped); |
| 1322 | |
| 1323 | if (unmapped_from_seq) |
| 1324 | *unmapped_from_seq = from_seq; |
| 1325 | if (unmapped_from_tei) |
| 1326 | *unmapped_from_tei = from_tei; |
| 1327 | if (final_unmapped) |
| 1328 | *final_unmapped = unmapped; |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
| 1332 | static int gsn_addr_to_sockaddr(struct gsn_addr *src, |
| 1333 | uint16_t port, |
| 1334 | struct osmo_sockaddr *dst) |
| 1335 | { |
| 1336 | return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port); |
| 1337 | } |
| 1338 | |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1339 | /* If p is an Echo request, replace p's data with the matching response and |
| 1340 | * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is |
| 1341 | * detected. */ |
| 1342 | static int gtphub_handle_echo(struct gtphub *hub, struct gtp_packet_desc *p, uint8_t **reply_buf) |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1343 | { |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1344 | if (p->type != GTP_ECHO_REQ) |
| 1345 | return 0; |
| 1346 | |
| 1347 | static uint8_t echo_response_data[14] = { |
| 1348 | 0x32, /* flags */ |
| 1349 | GTP_ECHO_RSP, |
| 1350 | 0x00, 14 - 8, /* Length in network byte order */ |
| 1351 | 0x00, 0x00, 0x00, 0x00, /* Zero TEI */ |
| 1352 | 0, 0, /* Seq, to be replaced */ |
| 1353 | 0, 0, /* no extensions */ |
| 1354 | 0x0e, /* Recovery IE */ |
| 1355 | 0 /* Recovery counter, to be replaced */ |
| 1356 | }; |
| 1357 | uint16_t *seq = (uint16_t*)&echo_response_data[8]; |
| 1358 | uint8_t *recovery = &echo_response_data[13]; |
| 1359 | |
| 1360 | *seq = hton16(p->seq); |
| 1361 | *recovery = hub->restart_counter; |
| 1362 | |
| 1363 | *reply_buf = echo_response_data; |
| 1364 | |
| 1365 | return sizeof(echo_response_data); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1366 | } |
| 1367 | |
Neels Hofmeyr | 5e94753 | 2015-11-11 14:01:06 +0100 | [diff] [blame] | 1368 | struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind, |
| 1369 | const struct osmo_sockaddr *addr); |
| 1370 | |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1371 | /* Parse buffer as GTP packet, replace elements in-place and return the ofd and |
| 1372 | * address to forward to. Return a pointer to the osmo_fd, but copy the |
| 1373 | * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at |
| 1374 | * any moment, while the osmo_fd is guaranteed to persist. Return the number of |
| 1375 | * bytes to forward, 0 or less on failure. */ |
| 1376 | int gtphub_from_ggsns_handle_buf(struct gtphub *hub, |
| 1377 | unsigned int plane_idx, |
| 1378 | const struct osmo_sockaddr *from_addr, |
| 1379 | uint8_t *buf, |
| 1380 | size_t received, |
| 1381 | time_t now, |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1382 | uint8_t **reply_buf, |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1383 | struct osmo_fd **to_ofd, |
| 1384 | struct osmo_sockaddr *to_addr) |
| 1385 | { |
Neels Hofmeyr | e6078bc | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 1386 | LOG("<- rx %s from GGSN %s\n", |
| 1387 | gtphub_plane_idx_names[plane_idx], |
| 1388 | osmo_sockaddr_to_str(from_addr)); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1389 | |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1390 | static struct gtp_packet_desc p; |
| 1391 | gtp_decode(buf, received, plane_idx, &p); |
| 1392 | |
| 1393 | if (p.rc <= 0) |
| 1394 | return -1; |
| 1395 | |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1396 | int reply_len; |
| 1397 | reply_len = gtphub_handle_echo(hub, &p, reply_buf); |
| 1398 | if (reply_len > 0) { |
| 1399 | /* It was an echo. Nothing left to do. */ |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1400 | osmo_sockaddr_copy(to_addr, from_addr); |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1401 | *to_ofd = &hub->to_ggsns[plane_idx].ofd; |
| 1402 | return reply_len; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1403 | } |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1404 | if (reply_len < 0) |
| 1405 | return -1; |
| 1406 | |
| 1407 | *to_ofd = &hub->to_sgsns[plane_idx].ofd; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1408 | |
| 1409 | /* If a GGSN proxy is configured, check that it's indeed that proxy |
| 1410 | * talking to us. A proxy is a forced 1:1 connection, e.g. to another |
| 1411 | * gtphub, so no-one else is allowed to talk to us from that side. */ |
| 1412 | struct gtphub_peer_port *ggsn = hub->ggsn_proxy[plane_idx]; |
| 1413 | if (ggsn) { |
| 1414 | if (osmo_sockaddr_cmp(&ggsn->sa, from_addr) != 0) { |
| 1415 | LOGERR("Rejecting: GGSN proxy configured, but GTP packet" |
| 1416 | " received on GGSN bind is from another sender:" |
| 1417 | " proxy: %s sender: %s\n", |
| 1418 | gtphub_port_str(ggsn), |
| 1419 | osmo_sockaddr_to_str(from_addr)); |
| 1420 | return -1; |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | if (!ggsn) { |
Neels Hofmeyr | 5e94753 | 2015-11-11 14:01:06 +0100 | [diff] [blame] | 1425 | /* Find a GGSN peer with a matching address. The sender's port |
| 1426 | * may in fact differ. */ |
| 1427 | ggsn = gtphub_known_addr_have_port(&hub->to_ggsns[plane_idx], |
| 1428 | from_addr); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | /* If any PDP context has been created, we already have an entry for |
| 1432 | * this GGSN. If we don't have an entry, the GGSN has nothing to tell |
| 1433 | * us about. */ |
| 1434 | if (!ggsn) { |
Neels Hofmeyr | e6078bc | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 1435 | LOGERR("Dropping packet: unknown GGSN peer: %s\n", |
| 1436 | osmo_sockaddr_to_str(from_addr)); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1437 | return -1; |
| 1438 | } |
| 1439 | |
| 1440 | LOG("GGSN peer: %s\n", gtphub_port_str(ggsn)); |
| 1441 | |
| 1442 | struct gtphub_peer_port *sgsn_from_seq; |
| 1443 | struct gtphub_peer_port *sgsn; |
| 1444 | if (gtphub_unmap(hub, &p, ggsn, |
| 1445 | hub->sgsn_proxy[plane_idx], |
| 1446 | &sgsn, &sgsn_from_seq, |
| 1447 | NULL /* not interested, got it in &sgsn already */ |
| 1448 | ) |
| 1449 | != 0) { |
| 1450 | return -1; |
| 1451 | } |
| 1452 | |
| 1453 | if (!sgsn) { |
| 1454 | /* A GGSN initiated request would go to a known TEI. So this is |
| 1455 | * bogus. */ |
| 1456 | LOGERR("No SGSN to send to. Dropping packet.\n"); |
| 1457 | return -1; |
| 1458 | } |
| 1459 | |
| 1460 | if (plane_idx == GTPH_PLANE_CTRL) { |
| 1461 | /* This may be a Create PDP Context response. If it is, there are other |
| 1462 | * addresses in the GTP message to set up apart from the sender. */ |
| 1463 | if (gtphub_handle_pdp_ctx_ies(hub, hub->to_ggsns, |
| 1464 | hub->to_sgsns, &p, now) |
| 1465 | != 0) |
| 1466 | return -1; |
| 1467 | } |
| 1468 | |
| 1469 | gtphub_check_restart_counter(hub, &p, ggsn); |
| 1470 | gtphub_map_restart_counter(hub, &p, ggsn, sgsn); |
| 1471 | |
| 1472 | /* If the GGSN is replying to an SGSN request, the sequence nr has |
| 1473 | * already been unmapped above (sgsn_from_seq != NULL), and we need not |
| 1474 | * create a new mapping. */ |
| 1475 | if (!sgsn_from_seq) |
| 1476 | gtphub_map_seq(&p, ggsn, sgsn, now); |
| 1477 | |
| 1478 | osmo_sockaddr_copy(to_addr, &sgsn->sa); |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1479 | |
| 1480 | *reply_buf = (uint8_t*)p.data; |
Neels Hofmeyr | e6078bc | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 1481 | |
| 1482 | LOG("<-- Forward to SGSN: %d bytes to %s\n", |
| 1483 | (int)received, osmo_sockaddr_to_str(to_addr)); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1484 | return received; |
| 1485 | } |
| 1486 | |
| 1487 | static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what) |
| 1488 | { |
| 1489 | unsigned int plane_idx = from_sgsns_ofd->priv_nr; |
| 1490 | OSMO_ASSERT(plane_idx < GTPH_PLANE_N); |
| 1491 | LOG("\n\n=== reading from SGSN (%s)\n", gtphub_plane_idx_names[plane_idx]); |
| 1492 | |
| 1493 | if (!(what & BSC_FD_READ)) |
| 1494 | return 0; |
| 1495 | |
| 1496 | struct gtphub *hub = from_sgsns_ofd->data; |
| 1497 | |
| 1498 | static uint8_t buf[4096]; |
| 1499 | struct osmo_sockaddr from_addr; |
| 1500 | struct osmo_sockaddr to_addr; |
| 1501 | struct osmo_fd *to_ofd; |
Neels Hofmeyr | 97b5f14 | 2015-11-11 17:27:01 +0100 | [diff] [blame] | 1502 | int len; |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1503 | uint8_t *reply_buf; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1504 | |
| 1505 | len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf)); |
| 1506 | if (len < 1) |
| 1507 | return 0; |
| 1508 | |
| 1509 | len = gtphub_from_sgsns_handle_buf(hub, plane_idx, &from_addr, buf, len, |
| 1510 | gtphub_now(), |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1511 | &reply_buf, &to_ofd, &to_addr); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1512 | if (len < 1) |
| 1513 | return 0; |
| 1514 | |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1515 | return gtphub_write(to_ofd, &to_addr, reply_buf, len); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | /* Analogous to gtphub_from_ggsns_handle_buf(), see the comment there. */ |
| 1519 | int gtphub_from_sgsns_handle_buf(struct gtphub *hub, |
| 1520 | unsigned int plane_idx, |
| 1521 | const struct osmo_sockaddr *from_addr, |
| 1522 | uint8_t *buf, |
| 1523 | size_t received, |
| 1524 | time_t now, |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1525 | uint8_t **reply_buf, |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1526 | struct osmo_fd **to_ofd, |
| 1527 | struct osmo_sockaddr *to_addr) |
| 1528 | { |
Neels Hofmeyr | e6078bc | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 1529 | LOG("-> rx %s from SGSN %s\n", |
| 1530 | gtphub_plane_idx_names[plane_idx], |
| 1531 | osmo_sockaddr_to_str(from_addr)); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1532 | |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1533 | static struct gtp_packet_desc p; |
| 1534 | gtp_decode(buf, received, plane_idx, &p); |
| 1535 | |
| 1536 | if (p.rc <= 0) |
| 1537 | return -1; |
| 1538 | |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1539 | int reply_len; |
| 1540 | reply_len = gtphub_handle_echo(hub, &p, reply_buf); |
| 1541 | if (reply_len > 0) { |
| 1542 | /* It was an echo. Nothing left to do. */ |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1543 | osmo_sockaddr_copy(to_addr, from_addr); |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1544 | *to_ofd = &hub->to_ggsns[plane_idx].ofd; |
| 1545 | return reply_len; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1546 | } |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1547 | if (reply_len < 0) |
| 1548 | return -1; |
| 1549 | |
| 1550 | *to_ofd = &hub->to_ggsns[plane_idx].ofd; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1551 | |
| 1552 | /* If an SGSN proxy is configured, check that it's indeed that proxy |
| 1553 | * talking to us. A proxy is a forced 1:1 connection, e.g. to another |
| 1554 | * gtphub, so no-one else is allowed to talk to us from that side. */ |
| 1555 | struct gtphub_peer_port *sgsn = hub->sgsn_proxy[plane_idx]; |
| 1556 | if (sgsn) { |
| 1557 | if (osmo_sockaddr_cmp(&sgsn->sa, from_addr) != 0) { |
| 1558 | LOGERR("Rejecting: GGSN proxy configured, but GTP packet" |
| 1559 | " received on GGSN bind is from another sender:" |
| 1560 | " proxy: %s sender: %s\n", |
| 1561 | gtphub_port_str(sgsn), |
| 1562 | osmo_sockaddr_to_str(from_addr)); |
| 1563 | return -1; |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | if (!sgsn) { |
| 1568 | /* If any contact has been made before, we already have an |
Neels Hofmeyr | 5e94753 | 2015-11-11 14:01:06 +0100 | [diff] [blame] | 1569 | * entry for this SGSN. The port may differ. */ |
| 1570 | sgsn = gtphub_known_addr_have_port(&hub->to_sgsns[plane_idx], |
| 1571 | from_addr); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | if (!sgsn) { |
| 1575 | /* A new peer. If this is on the Ctrl plane, an SGSN may make |
| 1576 | * first contact without being known yet, so create the peer |
| 1577 | * struct for the current sender. */ |
| 1578 | if (plane_idx != GTPH_PLANE_CTRL) { |
| 1579 | LOGERR("User plane peer was not announced by PDP Context, discarding: %s\n", |
| 1580 | osmo_sockaddr_to_str(from_addr)); |
| 1581 | return -1; |
| 1582 | } |
| 1583 | |
| 1584 | struct gsn_addr from_gsna; |
| 1585 | uint16_t from_port; |
| 1586 | if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0) |
| 1587 | return -1; |
| 1588 | |
| 1589 | sgsn = gtphub_port_have(hub, &hub->to_sgsns[plane_idx], |
| 1590 | &from_gsna, from_port); |
| 1591 | } |
| 1592 | |
| 1593 | if (!sgsn) { |
| 1594 | /* This could theoretically happen for invalid address data or somesuch. */ |
Neels Hofmeyr | e6078bc | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 1595 | LOGERR("Dropping packet: invalid SGSN peer: %s\n", |
| 1596 | osmo_sockaddr_to_str(from_addr)); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1597 | return -1; |
| 1598 | } |
| 1599 | LOG("SGSN peer: %s\n", gtphub_port_str(sgsn)); |
| 1600 | |
| 1601 | struct gtphub_peer_port *ggsn_from_seq; |
| 1602 | struct gtphub_peer_port *ggsn; |
| 1603 | if (gtphub_unmap(hub, &p, sgsn, |
| 1604 | hub->ggsn_proxy[plane_idx], |
| 1605 | &ggsn, &ggsn_from_seq, |
| 1606 | NULL /* not interested, got it in &ggsn already */ |
| 1607 | ) |
| 1608 | != 0) { |
| 1609 | return -1; |
| 1610 | } |
| 1611 | |
Neels Hofmeyr | 86d7401 | 2015-11-11 19:26:09 +0100 | [diff] [blame^] | 1612 | if (!ggsn) { |
| 1613 | if (gtphub_resolve_ggsn(hub, &p, &ggsn) < 0) |
| 1614 | return -1; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1615 | } |
| 1616 | |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1617 | if (!ggsn) { |
| 1618 | LOGERR("No GGSN to send to. Dropping packet.\n"); |
| 1619 | return -1; |
| 1620 | } |
| 1621 | |
| 1622 | if (plane_idx == GTPH_PLANE_CTRL) { |
| 1623 | /* This may be a Create PDP Context requst. If it is, there are other |
| 1624 | * addresses in the GTP message to set up apart from the sender. */ |
| 1625 | if (gtphub_handle_pdp_ctx_ies(hub, hub->to_sgsns, |
| 1626 | hub->to_ggsns, &p, now) |
| 1627 | != 0) |
| 1628 | return -1; |
| 1629 | } |
| 1630 | |
| 1631 | gtphub_check_restart_counter(hub, &p, sgsn); |
| 1632 | gtphub_map_restart_counter(hub, &p, sgsn, ggsn); |
| 1633 | |
| 1634 | /* If the SGSN is replying to a GGSN request, the sequence nr has |
| 1635 | * already been unmapped above (unmap_ggsn != NULL), and we need not |
| 1636 | * create a new outgoing sequence map. */ |
| 1637 | if (!ggsn_from_seq) |
| 1638 | gtphub_map_seq(&p, sgsn, ggsn, now); |
| 1639 | |
| 1640 | osmo_sockaddr_copy(to_addr, &ggsn->sa); |
| 1641 | |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1642 | *reply_buf = (uint8_t*)p.data; |
Neels Hofmeyr | e6078bc | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 1643 | |
| 1644 | LOG("--> Forward to GGSN: %d bytes to %s\n", |
| 1645 | (int)received, osmo_sockaddr_to_str(to_addr)); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1646 | return received; |
| 1647 | } |
| 1648 | |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 1649 | static void resolved_gssn_del_cb(struct expiring_item *expi) |
| 1650 | { |
| 1651 | struct gtphub_resolved_ggsn *ggsn; |
| 1652 | ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry); |
| 1653 | |
| 1654 | gtphub_port_ref_count_dec(ggsn->peer); |
| 1655 | llist_del(&ggsn->entry); |
| 1656 | |
| 1657 | ggsn->expiry_entry.del_cb = 0; |
| 1658 | expiring_item_del(&ggsn->expiry_entry); |
| 1659 | |
| 1660 | talloc_free(ggsn); |
| 1661 | } |
| 1662 | |
| 1663 | void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str, |
| 1664 | struct gsn_addr *resolved_addr, |
| 1665 | time_t now) |
| 1666 | { |
| 1667 | struct gtphub_peer_port *pp; |
| 1668 | struct gtphub_resolved_ggsn *ggsn; |
| 1669 | |
Neels Hofmeyr | 05c593f | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 1670 | LOG("Resolved GGSN callback: %s %s\n", |
| 1671 | apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr, sizeof(*resolved_addr))); |
| 1672 | |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 1673 | pp = gtphub_port_have(hub, &hub->to_ggsns[GTPH_PLANE_CTRL], |
| 1674 | resolved_addr, 2123); |
| 1675 | if (!pp) { |
| 1676 | LOGERR("Internal: Cannot create/find peer '%s'\n", |
| 1677 | gsn_addr_to_str(resolved_addr)); |
| 1678 | return; |
| 1679 | } |
| 1680 | |
| 1681 | ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn); |
| 1682 | OSMO_ASSERT(ggsn); |
| 1683 | |
| 1684 | ggsn->peer = pp; |
| 1685 | gtphub_port_ref_count_inc(pp); |
| 1686 | |
| 1687 | strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str)); |
| 1688 | |
| 1689 | ggsn->expiry_entry.del_cb = resolved_gssn_del_cb; |
| 1690 | expiry_add(&hub->expire_tei_maps, &ggsn->expiry_entry, now); |
| 1691 | |
| 1692 | llist_add(&ggsn->entry, &hub->resolved_ggsns); |
| 1693 | } |
| 1694 | |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1695 | static int gtphub_gc_peer_port(struct gtphub_peer_port *pp) |
| 1696 | { |
| 1697 | return pp->ref_count == 0; |
| 1698 | } |
| 1699 | |
| 1700 | static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa) |
| 1701 | { |
| 1702 | struct gtphub_peer_port *pp, *npp; |
| 1703 | llist_for_each_entry_safe(pp, npp, &pa->ports, entry) { |
| 1704 | if (gtphub_gc_peer_port(pp)) { |
| 1705 | LOG("expired: peer %s\n", |
| 1706 | gtphub_port_str(pp)); |
| 1707 | gtphub_peer_port_del(pp); |
| 1708 | } |
| 1709 | } |
| 1710 | return llist_empty(&pa->ports); |
| 1711 | } |
| 1712 | |
| 1713 | static int gtphub_gc_peer(struct gtphub_peer *p) |
| 1714 | { |
| 1715 | struct gtphub_peer_addr *pa, *npa; |
| 1716 | llist_for_each_entry_safe(pa, npa, &p->addresses, entry) { |
| 1717 | if (gtphub_gc_peer_addr(pa)) { |
| 1718 | gtphub_peer_addr_del(pa); |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | /* Note that there's a ref_count in each gtphub_peer_port instance |
| 1723 | * listed within p->addresses, referenced by TEI mappings from |
| 1724 | * hub->tei_map. As long as those don't expire, this peer will stay. */ |
| 1725 | |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1726 | return llist_empty(&p->addresses) |
| 1727 | && nr_map_empty(&p->seq_map); |
| 1728 | } |
| 1729 | |
| 1730 | static void gtphub_gc_bind(struct gtphub_bind *b) |
| 1731 | { |
| 1732 | struct gtphub_peer *p, *n; |
| 1733 | llist_for_each_entry_safe(p, n, &b->peers, entry) { |
| 1734 | if (gtphub_gc_peer(p)) { |
| 1735 | gtphub_peer_del(p); |
| 1736 | } |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | void gtphub_gc(struct gtphub *hub, time_t now) |
| 1741 | { |
| 1742 | int expired; |
| 1743 | expired = expiry_tick(&hub->expire_seq_maps, now); |
| 1744 | expired += expiry_tick(&hub->expire_tei_maps, now); |
| 1745 | |
| 1746 | /* ... */ |
| 1747 | |
| 1748 | if (expired) { |
| 1749 | int i; |
| 1750 | for (i = 0; i < GTPH_PLANE_N; i++) { |
| 1751 | gtphub_gc_bind(&hub->to_sgsns[i]); |
| 1752 | gtphub_gc_bind(&hub->to_ggsns[i]); |
| 1753 | } |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | static void gtphub_gc_cb(void *data) |
| 1758 | { |
| 1759 | struct gtphub *hub = data; |
| 1760 | gtphub_gc(hub, gtphub_now()); |
| 1761 | osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0); |
| 1762 | } |
| 1763 | |
| 1764 | static void gtphub_gc_start(struct gtphub *hub) |
| 1765 | { |
| 1766 | hub->gc_timer.cb = gtphub_gc_cb; |
| 1767 | hub->gc_timer.data = hub; |
| 1768 | |
| 1769 | osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0); |
| 1770 | } |
| 1771 | |
| 1772 | /* called by unit tests */ |
| 1773 | void gtphub_init(struct gtphub *hub) |
| 1774 | { |
| 1775 | gtphub_zero(hub); |
| 1776 | |
| 1777 | expiry_init(&hub->expire_seq_maps, GTPH_SEQ_MAPPING_EXPIRY_SECS); |
| 1778 | expiry_init(&hub->expire_tei_maps, GTPH_TEI_MAPPING_EXPIRY_MINUTES * 60); |
| 1779 | |
| 1780 | int plane_idx; |
| 1781 | for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { |
| 1782 | nr_pool_init(&hub->tei_pool[plane_idx]); |
| 1783 | nr_map_init(&hub->tei_map[plane_idx], |
| 1784 | &hub->tei_pool[plane_idx], |
| 1785 | &hub->expire_tei_maps); |
| 1786 | |
| 1787 | gtphub_bind_init(&hub->to_ggsns[plane_idx]); |
| 1788 | gtphub_bind_init(&hub->to_sgsns[plane_idx]); |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | static int gtphub_make_proxy(struct gtphub *hub, |
| 1793 | struct gtphub_peer_port **pp, |
| 1794 | struct gtphub_bind *bind, |
| 1795 | const struct gtphub_cfg_addr *addr) |
| 1796 | { |
| 1797 | if (!addr->addr_str) |
| 1798 | return 0; |
| 1799 | |
| 1800 | struct gsn_addr gsna; |
| 1801 | if (gsn_addr_from_str(&gsna, addr->addr_str) != 0) |
| 1802 | return -1; |
| 1803 | |
| 1804 | *pp = gtphub_port_have(hub, bind, &gsna, addr->port); |
| 1805 | |
| 1806 | /* This is *the* proxy. Make sure it is never expired. */ |
| 1807 | gtphub_port_ref_count_inc(*pp); |
| 1808 | return 0; |
| 1809 | } |
| 1810 | |
| 1811 | int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg) |
| 1812 | { |
| 1813 | int rc; |
| 1814 | |
| 1815 | gtphub_init(hub); |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 1816 | gtphub_ares_init(hub); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1817 | |
Neels Hofmeyr | a7c1015 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1818 | /* TODO set hub->restart_counter from external file. */ |
| 1819 | |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1820 | int plane_idx; |
| 1821 | for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { |
| 1822 | rc = gtphub_bind_start(&hub->to_ggsns[plane_idx], |
| 1823 | &cfg->to_ggsns[plane_idx], |
| 1824 | from_ggsns_read_cb, hub, plane_idx); |
| 1825 | if (rc) { |
| 1826 | LOGERR("Failed to bind for GGSNs (%s)\n", |
| 1827 | gtphub_plane_idx_names[plane_idx]); |
| 1828 | return rc; |
| 1829 | } |
| 1830 | |
| 1831 | rc = gtphub_bind_start(&hub->to_sgsns[plane_idx], |
| 1832 | &cfg->to_sgsns[plane_idx], |
| 1833 | from_sgsns_read_cb, hub, plane_idx); |
| 1834 | if (rc) { |
| 1835 | LOGERR("Failed to bind for SGSNs (%s)\n", |
| 1836 | gtphub_plane_idx_names[plane_idx]); |
| 1837 | return rc; |
| 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | |
| 1842 | for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { |
| 1843 | if (gtphub_make_proxy(hub, |
| 1844 | &hub->sgsn_proxy[plane_idx], |
| 1845 | &hub->to_sgsns[plane_idx], |
| 1846 | &cfg->sgsn_proxy[plane_idx]) |
| 1847 | != 0) { |
| 1848 | LOGERR("Cannot configure SGSN proxy %s port %d.\n", |
| 1849 | cfg->sgsn_proxy[plane_idx].addr_str, |
| 1850 | (int)cfg->sgsn_proxy[plane_idx].port); |
| 1851 | return -1; |
| 1852 | } |
| 1853 | if (gtphub_make_proxy(hub, |
| 1854 | &hub->ggsn_proxy[plane_idx], |
| 1855 | &hub->to_ggsns[plane_idx], |
| 1856 | &cfg->ggsn_proxy[plane_idx]) |
| 1857 | != 0) { |
| 1858 | LOGERR("Cannot configure GGSN proxy.\n"); |
| 1859 | return -1; |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { |
| 1864 | if (hub->sgsn_proxy[plane_idx]) |
| 1865 | LOG("Using SGSN %s proxy %s\n", |
| 1866 | gtphub_plane_idx_names[plane_idx], |
| 1867 | gtphub_port_str(hub->sgsn_proxy[plane_idx])); |
| 1868 | } |
| 1869 | |
| 1870 | for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { |
| 1871 | if (hub->sgsn_proxy[plane_idx]) |
| 1872 | LOG("Using GGSN %s proxy %s\n", |
| 1873 | gtphub_plane_idx_names[plane_idx], |
| 1874 | gtphub_port_str(hub->ggsn_proxy[plane_idx])); |
| 1875 | } |
| 1876 | |
| 1877 | gtphub_gc_start(hub); |
| 1878 | return 0; |
| 1879 | } |
| 1880 | |
| 1881 | static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer, |
| 1882 | const struct gsn_addr *addr) |
| 1883 | { |
| 1884 | struct gtphub_peer_addr *a; |
| 1885 | llist_for_each_entry(a, &peer->addresses, entry) { |
| 1886 | if (gsn_addr_same(&a->addr, addr)) |
| 1887 | return a; |
| 1888 | } |
| 1889 | return NULL; |
| 1890 | } |
| 1891 | |
| 1892 | static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a, |
| 1893 | uint16_t port) |
| 1894 | { |
| 1895 | OSMO_ASSERT(port); |
| 1896 | struct gtphub_peer_port *pp; |
| 1897 | llist_for_each_entry(pp, &a->ports, entry) { |
| 1898 | if (pp->port == port) |
| 1899 | return pp; |
| 1900 | } |
| 1901 | return NULL; |
| 1902 | } |
| 1903 | |
| 1904 | static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind, |
| 1905 | const struct gsn_addr *addr) |
| 1906 | { |
| 1907 | struct gtphub_peer *peer; |
| 1908 | llist_for_each_entry(peer, &bind->peers, entry) { |
| 1909 | struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr); |
| 1910 | if (a) |
| 1911 | return a; |
| 1912 | } |
| 1913 | return NULL; |
| 1914 | } |
| 1915 | |
| 1916 | static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind, |
| 1917 | const struct gsn_addr *addr, |
| 1918 | uint16_t port) |
| 1919 | { |
| 1920 | struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr); |
| 1921 | if (!a) |
| 1922 | return NULL; |
| 1923 | return gtphub_addr_find_port(a, port); |
| 1924 | } |
| 1925 | |
| 1926 | struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind, |
| 1927 | const struct osmo_sockaddr *addr) |
| 1928 | { |
| 1929 | struct gsn_addr gsna; |
| 1930 | uint16_t port; |
| 1931 | gsn_addr_from_sockaddr(&gsna, &port, addr); |
| 1932 | return gtphub_port_find(bind, &gsna, port); |
| 1933 | } |
| 1934 | |
| 1935 | static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub, |
| 1936 | struct gtphub_bind *bind) |
| 1937 | { |
| 1938 | struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer); |
| 1939 | OSMO_ASSERT(peer); |
| 1940 | |
| 1941 | INIT_LLIST_HEAD(&peer->addresses); |
| 1942 | |
| 1943 | nr_pool_init(&peer->seq_pool); |
| 1944 | nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_seq_maps); |
| 1945 | |
| 1946 | /* TODO use something random to pick the initial sequence nr. |
| 1947 | 0x6d31 produces the ASCII character sequence 'm1', currently used in |
| 1948 | gtphub_nc_test.sh. */ |
| 1949 | peer->seq_pool.last_nr = 0x6d31 - 1; |
| 1950 | |
| 1951 | llist_add(&peer->entry, &bind->peers); |
| 1952 | return peer; |
| 1953 | } |
| 1954 | |
| 1955 | static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer, |
| 1956 | const struct gsn_addr *addr) |
| 1957 | { |
| 1958 | struct gtphub_peer_addr *a; |
| 1959 | a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr); |
| 1960 | OSMO_ASSERT(a); |
| 1961 | a->peer = peer; |
| 1962 | gsn_addr_copy(&a->addr, addr); |
| 1963 | INIT_LLIST_HEAD(&a->ports); |
| 1964 | llist_add(&a->entry, &peer->addresses); |
| 1965 | |
| 1966 | return a; |
| 1967 | } |
| 1968 | |
| 1969 | static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub, |
| 1970 | struct gtphub_bind *bind, |
| 1971 | const struct gsn_addr *addr) |
| 1972 | { |
| 1973 | struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr); |
| 1974 | if (a) |
| 1975 | return a; |
| 1976 | |
| 1977 | /* If we haven't found an address, that means we need to create an |
| 1978 | * entirely new peer for the new address. More addresses may be added |
| 1979 | * to this peer later, but not via this function. */ |
| 1980 | struct gtphub_peer *peer = gtphub_peer_new(hub, bind); |
Neels Hofmeyr | e6078bc | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 1981 | |
| 1982 | a = gtphub_peer_add_addr(peer, addr); |
| 1983 | |
| 1984 | LOG("New peer address: %s\n", |
| 1985 | gsn_addr_to_str(&a->addr)); |
| 1986 | |
| 1987 | return a; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a, |
| 1991 | uint16_t port) |
| 1992 | { |
| 1993 | struct gtphub_peer_port *pp; |
| 1994 | |
| 1995 | pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port); |
| 1996 | OSMO_ASSERT(pp); |
| 1997 | pp->peer_addr = a; |
| 1998 | pp->port = port; |
| 1999 | |
| 2000 | if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) { |
| 2001 | talloc_free(pp); |
| 2002 | return NULL; |
| 2003 | } |
| 2004 | |
| 2005 | llist_add(&pp->entry, &a->ports); |
| 2006 | |
Neels Hofmeyr | e6078bc | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 2007 | LOG("New peer port: %s port %d\n", |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2008 | gsn_addr_to_str(&a->addr), |
| 2009 | (int)port); |
| 2010 | |
| 2011 | return pp; |
| 2012 | } |
| 2013 | |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 2014 | struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub, |
| 2015 | struct gtphub_bind *bind, |
| 2016 | const struct gsn_addr *addr, |
| 2017 | uint16_t port) |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2018 | { |
| 2019 | struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr); |
| 2020 | |
| 2021 | struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port); |
| 2022 | if (pp) |
| 2023 | return pp; |
| 2024 | |
| 2025 | return gtphub_addr_add_port(a, port); |
| 2026 | } |
| 2027 | |
Neels Hofmeyr | 5e94753 | 2015-11-11 14:01:06 +0100 | [diff] [blame] | 2028 | /* Find a GGSN peer with a matching address. If the address is known but the |
| 2029 | * port not, create a new port for that peer address. */ |
| 2030 | struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind, |
| 2031 | const struct osmo_sockaddr *addr) |
| 2032 | { |
| 2033 | struct gtphub_peer_addr *pa; |
| 2034 | struct gtphub_peer_port *pp; |
| 2035 | |
| 2036 | struct gsn_addr gsna; |
| 2037 | uint16_t port; |
| 2038 | gsn_addr_from_sockaddr(&gsna, &port, addr); |
| 2039 | |
| 2040 | pa = gtphub_addr_find(bind, &gsna); |
| 2041 | if (!pa) |
| 2042 | return NULL; |
| 2043 | |
| 2044 | pp = gtphub_addr_find_port(pa, port); |
| 2045 | |
| 2046 | if (!pp) |
| 2047 | pp = gtphub_addr_add_port(pa, port); |
| 2048 | |
| 2049 | return pp; |
| 2050 | } |
| 2051 | |
| 2052 | |
Neels Hofmeyr | ba35d0c | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 2053 | /* Return 0 if the message in p is not applicable for GGSN resolution, -1 if |
| 2054 | * resolution should be possible but failed, and 1 if resolution was |
| 2055 | * successful. *pp will be set to NULL if <1 is returned. */ |
| 2056 | static int gtphub_resolve_ggsn(struct gtphub *hub, |
| 2057 | struct gtp_packet_desc *p, |
| 2058 | struct gtphub_peer_port **pp) |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2059 | { |
Neels Hofmeyr | ba35d0c | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 2060 | *pp = NULL; |
| 2061 | |
| 2062 | /* TODO determine from message type whether IEs should be present? */ |
| 2063 | |
| 2064 | int rc; |
| 2065 | const char *imsi_str; |
| 2066 | rc = get_ie_imsi_str(p->ie, 0, &imsi_str); |
| 2067 | if (rc < 1) |
| 2068 | return rc; |
| 2069 | OSMO_ASSERT(imsi_str); |
| 2070 | |
| 2071 | const char *apn_str; |
| 2072 | rc = get_ie_apn_str(p->ie, &apn_str); |
| 2073 | if (rc < 1) |
| 2074 | return rc; |
| 2075 | OSMO_ASSERT(apn_str); |
| 2076 | |
| 2077 | *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str); |
| 2078 | return (*pp)? 1 : -1; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | |
| 2082 | /* TODO move to osmocom/core/socket.c ? */ |
| 2083 | /* The caller is required to call freeaddrinfo(*result), iff zero is returned. */ |
| 2084 | /* use this in osmo_sock_init() to remove dup. */ |
| 2085 | static int _osmo_getaddrinfo(struct addrinfo **result, |
| 2086 | uint16_t family, uint16_t type, uint8_t proto, |
| 2087 | const char *host, uint16_t port) |
| 2088 | { |
| 2089 | struct addrinfo hints; |
| 2090 | char portbuf[16]; |
| 2091 | |
| 2092 | sprintf(portbuf, "%u", port); |
| 2093 | memset(&hints, '\0', sizeof(struct addrinfo)); |
| 2094 | hints.ai_family = family; |
| 2095 | if (type == SOCK_RAW) { |
| 2096 | /* Workaround for glibc, that returns EAI_SERVICE (-8) if |
| 2097 | * SOCK_RAW and IPPROTO_GRE is used. |
| 2098 | */ |
| 2099 | hints.ai_socktype = SOCK_DGRAM; |
| 2100 | hints.ai_protocol = IPPROTO_UDP; |
| 2101 | } else { |
| 2102 | hints.ai_socktype = type; |
| 2103 | hints.ai_protocol = proto; |
| 2104 | } |
| 2105 | |
| 2106 | return getaddrinfo(host, portbuf, &hints, result); |
| 2107 | } |
| 2108 | |
| 2109 | /* TODO move to osmocom/core/socket.c ? */ |
| 2110 | int osmo_sockaddr_init(struct osmo_sockaddr *addr, |
| 2111 | uint16_t family, uint16_t type, uint8_t proto, |
| 2112 | const char *host, uint16_t port) |
| 2113 | { |
| 2114 | struct addrinfo *res; |
| 2115 | int rc; |
| 2116 | rc = _osmo_getaddrinfo(&res, family, type, proto, host, port); |
| 2117 | |
| 2118 | if (rc != 0) { |
| 2119 | LOGERR("getaddrinfo returned error %d\n", (int)rc); |
| 2120 | return -EINVAL; |
| 2121 | } |
| 2122 | |
| 2123 | OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a)); |
| 2124 | memcpy(&addr->a, res->ai_addr, res->ai_addrlen); |
| 2125 | addr->l = res->ai_addrlen; |
| 2126 | freeaddrinfo(res); |
| 2127 | |
| 2128 | return 0; |
| 2129 | } |
| 2130 | |
| 2131 | int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len, |
| 2132 | char *port_str, size_t port_str_len, |
| 2133 | const struct osmo_sockaddr *addr, |
| 2134 | int flags) |
| 2135 | { |
| 2136 | int rc; |
| 2137 | |
| 2138 | if ((addr->l < 1) || (addr->l > sizeof(addr->a))) { |
| 2139 | LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l); |
| 2140 | return -1; |
| 2141 | } |
| 2142 | |
| 2143 | if (addr->l > sizeof(addr->a)) { |
| 2144 | LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n", addr->l); |
| 2145 | return -1; |
| 2146 | } |
| 2147 | |
| 2148 | rc = getnameinfo((struct sockaddr*)&addr->a, addr->l, |
| 2149 | addr_str, addr_str_len, |
| 2150 | port_str, port_str_len, |
| 2151 | flags); |
| 2152 | |
| 2153 | if (rc) |
| 2154 | LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n", gai_strerror(rc), |
| 2155 | osmo_hexdump((uint8_t*)&addr->a, addr->l)); |
| 2156 | |
| 2157 | return rc; |
| 2158 | } |
| 2159 | |
| 2160 | const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr, |
| 2161 | char *buf, size_t buf_len) |
| 2162 | { |
| 2163 | const int portbuf_len = 6; |
| 2164 | OSMO_ASSERT(buf_len > portbuf_len); |
| 2165 | char *portbuf = buf + buf_len - portbuf_len; |
| 2166 | buf_len -= portbuf_len; |
| 2167 | if (osmo_sockaddr_to_strs(buf, buf_len, |
| 2168 | portbuf, portbuf_len, |
| 2169 | addr, |
| 2170 | NI_NUMERICHOST | NI_NUMERICSERV)) |
| 2171 | return NULL; |
| 2172 | |
| 2173 | char *pos = buf + strnlen(buf, buf_len-1); |
| 2174 | size_t len = buf_len - (pos - buf); |
| 2175 | |
| 2176 | snprintf(pos, len, " port %s", portbuf); |
| 2177 | buf[buf_len-1] = '\0'; |
| 2178 | |
| 2179 | return buf; |
| 2180 | } |
| 2181 | |
| 2182 | const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr) |
| 2183 | { |
| 2184 | static char buf[256]; |
| 2185 | const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf)); |
| 2186 | if (! result) |
| 2187 | return "(invalid)"; |
| 2188 | return result; |
| 2189 | } |
| 2190 | |
| 2191 | int osmo_sockaddr_cmp(const struct osmo_sockaddr *a, const struct osmo_sockaddr *b) |
| 2192 | { |
| 2193 | if (a == b) |
| 2194 | return 0; |
| 2195 | if (!a) |
| 2196 | return -1; |
| 2197 | if (!b) |
| 2198 | return 1; |
| 2199 | if (a->l != b->l) { |
| 2200 | /* Lengths are not the same, but determine the order. Will |
| 2201 | * anyone ever sort a list by osmo_sockaddr though...? */ |
| 2202 | int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l); |
| 2203 | if (cmp == 0) { |
| 2204 | if (a->l < b->l) |
| 2205 | return -1; |
| 2206 | else |
| 2207 | return 1; |
| 2208 | } |
| 2209 | return cmp; |
| 2210 | } |
| 2211 | return memcmp(&a->a, &b->a, a->l); |
| 2212 | } |
| 2213 | |
| 2214 | void osmo_sockaddr_copy(struct osmo_sockaddr *dst, const struct osmo_sockaddr *src) |
| 2215 | { |
| 2216 | OSMO_ASSERT(src->l <= sizeof(dst->a)); |
| 2217 | memcpy(&dst->a, &src->a, src->l); |
| 2218 | dst->l = src->l; |
| 2219 | } |