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