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 | |
Neels Hofmeyr | e2ed8e6 | 2015-11-17 14:30:37 +0100 | [diff] [blame] | 580 | void nr_pool_init(struct nr_pool *pool, nr_t nr_min, nr_t nr_max) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 581 | { |
Neels Hofmeyr | e2ed8e6 | 2015-11-17 14:30:37 +0100 | [diff] [blame] | 582 | *pool = (struct nr_pool){ |
| 583 | .nr_min = nr_min, |
| 584 | .nr_max = nr_max, |
| 585 | .last_nr = nr_max |
| 586 | }; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | nr_t nr_pool_next(struct nr_pool *pool) |
| 590 | { |
Neels Hofmeyr | e2ed8e6 | 2015-11-17 14:30:37 +0100 | [diff] [blame] | 591 | if (pool->last_nr >= pool->nr_max) |
| 592 | pool->last_nr = pool->nr_min; |
| 593 | else |
| 594 | pool->last_nr ++; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 595 | |
| 596 | return pool->last_nr; |
| 597 | } |
| 598 | |
| 599 | void nr_map_init(struct nr_map *map, struct nr_pool *pool, |
| 600 | struct expiry *exq) |
| 601 | { |
| 602 | ZERO_STRUCT(map); |
| 603 | map->pool = pool; |
| 604 | map->add_items_to_expiry = exq; |
| 605 | INIT_LLIST_HEAD(&map->mappings); |
| 606 | } |
| 607 | |
| 608 | void nr_mapping_init(struct nr_mapping *m) |
| 609 | { |
| 610 | ZERO_STRUCT(m); |
| 611 | INIT_LLIST_HEAD(&m->entry); |
| 612 | expiring_item_init(&m->expiry_entry); |
| 613 | } |
| 614 | |
| 615 | void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now) |
| 616 | { |
| 617 | /* Generate a mapped number */ |
| 618 | mapping->repl = nr_pool_next(map->pool); |
| 619 | |
| 620 | /* Add to the tail to always yield a list sorted by expiry, in |
| 621 | * ascending order. */ |
| 622 | llist_add_tail(&mapping->entry, &map->mappings); |
| 623 | if (map->add_items_to_expiry) |
| 624 | expiry_add(map->add_items_to_expiry, |
| 625 | &mapping->expiry_entry, |
| 626 | now); |
| 627 | } |
| 628 | |
| 629 | void nr_map_clear(struct nr_map *map) |
| 630 | { |
| 631 | struct nr_mapping *m; |
| 632 | struct nr_mapping *n; |
| 633 | llist_for_each_entry_safe(m, n, &map->mappings, entry) { |
| 634 | nr_mapping_del(m); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | int nr_map_empty(const struct nr_map *map) |
| 639 | { |
| 640 | return llist_empty(&map->mappings); |
| 641 | } |
| 642 | |
| 643 | struct nr_mapping *nr_map_get(const struct nr_map *map, |
| 644 | void *origin, nr_t nr_orig) |
| 645 | { |
| 646 | struct nr_mapping *mapping; |
| 647 | llist_for_each_entry(mapping, &map->mappings, entry) { |
| 648 | if ((mapping->origin == origin) |
| 649 | && (mapping->orig == nr_orig)) |
| 650 | return mapping; |
| 651 | } |
| 652 | /* Not found. */ |
| 653 | return NULL; |
| 654 | } |
| 655 | |
| 656 | struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl) |
| 657 | { |
| 658 | struct nr_mapping *mapping; |
| 659 | llist_for_each_entry(mapping, &map->mappings, entry) { |
| 660 | if (mapping->repl == nr_repl) { |
| 661 | return mapping; |
| 662 | } |
| 663 | } |
| 664 | /* Not found. */ |
| 665 | return NULL; |
| 666 | } |
| 667 | |
| 668 | void nr_mapping_del(struct nr_mapping *mapping) |
| 669 | { |
| 670 | OSMO_ASSERT(mapping); |
| 671 | llist_del(&mapping->entry); |
| 672 | INIT_LLIST_HEAD(&mapping->entry); |
| 673 | expiring_item_del(&mapping->expiry_entry); |
| 674 | } |
| 675 | |
| 676 | |
| 677 | /* gtphub */ |
| 678 | |
| 679 | const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = { |
| 680 | "CTRL", |
| 681 | "USER", |
| 682 | }; |
| 683 | |
| 684 | const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = { |
| 685 | 2123, |
| 686 | 2152, |
| 687 | }; |
| 688 | |
| 689 | time_t gtphub_now(void) |
| 690 | { |
| 691 | struct timespec now_tp; |
| 692 | OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0); |
| 693 | return now_tp.tv_sec; |
| 694 | } |
| 695 | |
| 696 | /* Remove a gtphub_peer from its list and free it. */ |
| 697 | static void gtphub_peer_del(struct gtphub_peer *peer) |
| 698 | { |
| 699 | nr_map_clear(&peer->seq_map); |
| 700 | llist_del(&peer->entry); |
| 701 | talloc_free(peer); |
| 702 | } |
| 703 | |
| 704 | static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa) |
| 705 | { |
| 706 | OSMO_ASSERT(llist_empty(&pa->ports)); |
| 707 | llist_del(&pa->entry); |
| 708 | talloc_free(pa); |
| 709 | } |
| 710 | |
| 711 | static void gtphub_peer_port_del(struct gtphub_peer_port *pp) |
| 712 | { |
| 713 | OSMO_ASSERT(pp->ref_count == 0); |
| 714 | llist_del(&pp->entry); |
| 715 | talloc_free(pp); |
| 716 | } |
| 717 | |
| 718 | /* From the information in the gtp_packet_desc, return the address of a GGSN. |
| 719 | * Return -1 on error. */ |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 720 | static int gtphub_resolve_ggsn(struct gtphub *hub, |
| 721 | struct gtp_packet_desc *p, |
| 722 | struct gtphub_peer_port **pp); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 723 | |
| 724 | /* See gtphub_ext.c (wrapped by unit test) */ |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 725 | struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub, |
| 726 | const char *imsi_str, |
| 727 | const char *apn_ni_str); |
| 728 | int gtphub_ares_init(struct gtphub *hub); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 729 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 730 | static void gtphub_zero(struct gtphub *hub) |
| 731 | { |
| 732 | ZERO_STRUCT(hub); |
Neels Hofmeyr | 16c3f57 | 2015-11-11 17:27:01 +0100 | [diff] [blame] | 733 | INIT_LLIST_HEAD(&hub->ggsn_lookups); |
| 734 | INIT_LLIST_HEAD(&hub->resolved_ggsns); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | static int gtphub_sock_init(struct osmo_fd *ofd, |
| 738 | const struct gtphub_cfg_addr *addr, |
| 739 | osmo_fd_cb_t cb, |
| 740 | void *data, |
| 741 | int ofd_id) |
| 742 | { |
| 743 | if (!addr->addr_str) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 744 | LOG(LOGL_FATAL, "Cannot bind: empty address.\n"); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 745 | return -1; |
| 746 | } |
| 747 | if (!addr->port) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 748 | LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n"); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 749 | return -1; |
| 750 | } |
| 751 | |
| 752 | ofd->when = BSC_FD_READ; |
| 753 | ofd->cb = cb; |
| 754 | ofd->data = data; |
| 755 | ofd->priv_nr = ofd_id; |
| 756 | |
| 757 | int rc; |
| 758 | rc = osmo_sock_init_ofd(ofd, |
| 759 | AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, |
| 760 | addr->addr_str, addr->port, |
| 761 | OSMO_SOCK_F_BIND); |
| 762 | if (rc < 1) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 763 | LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n", |
| 764 | addr->addr_str, (int)addr->port, rc); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 765 | return -1; |
| 766 | } |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | static void gtphub_bind_init(struct gtphub_bind *b) |
| 772 | { |
| 773 | ZERO_STRUCT(b); |
| 774 | |
| 775 | INIT_LLIST_HEAD(&b->peers); |
| 776 | } |
| 777 | |
| 778 | static int gtphub_bind_start(struct gtphub_bind *b, |
| 779 | const struct gtphub_cfg_bind *cfg, |
| 780 | osmo_fd_cb_t cb, void *cb_data, |
| 781 | unsigned int ofd_id) |
| 782 | { |
| 783 | if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0) |
| 784 | return -1; |
| 785 | if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0) |
| 786 | return -1; |
| 787 | return 0; |
| 788 | } |
| 789 | |
Neels Hofmeyr | 4034897 | 2015-11-17 12:22:28 +0100 | [diff] [blame] | 790 | /* Recv datagram from from->fd, write sender's address to *from_addr. |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 791 | * Return the number of bytes read, zero on error. */ |
| 792 | static int gtphub_read(const struct osmo_fd *from, |
| 793 | struct osmo_sockaddr *from_addr, |
| 794 | uint8_t *buf, size_t buf_len) |
| 795 | { |
Neels Hofmeyr | 4034897 | 2015-11-17 12:22:28 +0100 | [diff] [blame] | 796 | OSMO_ASSERT(from_addr); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 797 | |
Neels Hofmeyr | 4034897 | 2015-11-17 12:22:28 +0100 | [diff] [blame] | 798 | /* recvfrom requires the available length set in *from_addr_len. */ |
| 799 | from_addr->l = sizeof(from_addr->a); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 800 | errno = 0; |
| 801 | ssize_t received = recvfrom(from->fd, buf, buf_len, 0, |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 802 | (struct sockaddr*)&from_addr->a, |
| 803 | &from_addr->l); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 804 | /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message |
| 805 | * is not truncated. Then maybe reduce buf's size. */ |
| 806 | |
| 807 | if (received <= 0) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 808 | LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR), |
| 809 | "error: %s\n", strerror(errno)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 810 | return 0; |
| 811 | } |
| 812 | |
Neels Hofmeyr | 4034897 | 2015-11-17 12:22:28 +0100 | [diff] [blame] | 813 | LOG(LOGL_DEBUG, "Received %d bytes from %s\n%s\n", |
| 814 | (int)received, osmo_sockaddr_to_str(from_addr), |
| 815 | osmo_hexdump(buf, received)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 816 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 817 | return received; |
| 818 | } |
| 819 | |
| 820 | inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp) |
| 821 | { |
| 822 | OSMO_ASSERT(pp->ref_count < UINT_MAX); |
| 823 | pp->ref_count++; |
| 824 | } |
| 825 | |
| 826 | inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp) |
| 827 | { |
| 828 | OSMO_ASSERT(pp->ref_count > 0); |
| 829 | pp->ref_count--; |
| 830 | } |
| 831 | |
| 832 | inline void set_seq(struct gtp_packet_desc *p, uint16_t seq) |
| 833 | { |
| 834 | OSMO_ASSERT(p->version == 1); |
| 835 | p->data->gtp1l.h.seq = hton16(seq); |
| 836 | p->seq = seq; |
| 837 | } |
| 838 | |
| 839 | inline void set_tei(struct gtp_packet_desc *p, uint32_t tei) |
| 840 | { |
| 841 | OSMO_ASSERT(p->version == 1); |
| 842 | p->data->gtp1l.h.tei = hton32(tei); |
| 843 | p->header_tei = tei; |
| 844 | } |
| 845 | |
| 846 | static void gtphub_mapping_del_cb(struct expiring_item *expi); |
| 847 | |
| 848 | static struct nr_mapping *gtphub_mapping_new() |
| 849 | { |
| 850 | struct nr_mapping *nrm; |
| 851 | nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping); |
| 852 | OSMO_ASSERT(nrm); |
| 853 | |
| 854 | nr_mapping_init(nrm); |
| 855 | nrm->expiry_entry.del_cb = gtphub_mapping_del_cb; |
| 856 | return nrm; |
| 857 | } |
| 858 | |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 859 | static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf, |
| 860 | int buflen) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 861 | { |
| 862 | if (llist_empty(&peer->addresses)) |
| 863 | return "(addressless)"; |
| 864 | |
| 865 | struct gtphub_peer_addr *a = llist_first(&peer->addresses, |
| 866 | struct gtphub_peer_addr, |
| 867 | entry); |
| 868 | return gsn_addr_to_strb(&a->addr, buf, buflen); |
| 869 | } |
| 870 | |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 871 | static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf, |
| 872 | int buflen) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 873 | { |
| 874 | if (!port) |
| 875 | return "(null port)"; |
| 876 | |
| 877 | snprintf(buf, buflen, "%s port %d", |
| 878 | gsn_addr_to_str(&port->peer_addr->addr), |
| 879 | (int)port->port); |
| 880 | return buf; |
| 881 | } |
| 882 | |
| 883 | const char *gtphub_peer_str(struct gtphub_peer *peer) |
| 884 | { |
| 885 | static char buf[256]; |
| 886 | return gtphub_peer_strb(peer, buf, sizeof(buf)); |
| 887 | } |
| 888 | |
| 889 | const char *gtphub_peer_str2(struct gtphub_peer *peer) |
| 890 | { |
| 891 | static char buf[256]; |
| 892 | return gtphub_peer_strb(peer, buf, sizeof(buf)); |
| 893 | } |
| 894 | |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 895 | const char *gtphub_port_str(struct gtphub_peer_port *port) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 896 | { |
| 897 | static char buf[256]; |
| 898 | return gtphub_port_strb(port, buf, sizeof(buf)); |
| 899 | } |
| 900 | |
| 901 | static const char *gtphub_port_str2(struct gtphub_peer_port *port) |
| 902 | { |
| 903 | static char buf[256]; |
| 904 | return gtphub_port_strb(port, buf, sizeof(buf)); |
| 905 | } |
| 906 | |
| 907 | static void gtphub_mapping_del_cb(struct expiring_item *expi) |
| 908 | { |
| 909 | expi->del_cb = 0; /* avoid recursion loops */ |
| 910 | |
| 911 | struct nr_mapping *nrm = container_of(expi, |
| 912 | struct nr_mapping, |
| 913 | expiry_entry); |
| 914 | llist_del(&nrm->entry); |
| 915 | INIT_LLIST_HEAD(&nrm->entry); /* mark unused */ |
| 916 | |
| 917 | /* Just for log */ |
| 918 | struct gtphub_peer_port *from = nrm->origin; |
| 919 | OSMO_ASSERT(from); |
Neels Hofmeyr | 334af5d | 2015-11-17 14:24:46 +0100 | [diff] [blame] | 920 | LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 921 | (int)nrm->expiry_entry.expiry, |
| 922 | gtphub_port_str(from), |
Neels Hofmeyr | 334af5d | 2015-11-17 14:24:46 +0100 | [diff] [blame] | 923 | (unsigned int)nrm->orig, (unsigned int)nrm->repl); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 924 | |
| 925 | gtphub_port_ref_count_dec(from); |
| 926 | |
| 927 | talloc_free(nrm); |
| 928 | } |
| 929 | |
| 930 | static struct nr_mapping *gtphub_mapping_have(struct nr_map *map, |
| 931 | struct gtphub_peer_port *from, |
| 932 | nr_t orig_nr, |
| 933 | time_t now) |
| 934 | { |
| 935 | struct nr_mapping *nrm; |
| 936 | |
| 937 | nrm = nr_map_get(map, from, orig_nr); |
| 938 | |
| 939 | if (!nrm) { |
| 940 | nrm = gtphub_mapping_new(); |
| 941 | nrm->orig = orig_nr; |
| 942 | nrm->origin = from; |
| 943 | nr_map_add(map, nrm, now); |
| 944 | gtphub_port_ref_count_inc(from); |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 945 | LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 946 | gtphub_port_str(from), |
| 947 | (int)(nrm->orig), (int)(nrm->repl)); |
| 948 | } else { |
| 949 | /* restart expiry timeout */ |
| 950 | expiry_add(map->add_items_to_expiry, &nrm->expiry_entry, |
| 951 | now); |
| 952 | } |
| 953 | |
| 954 | OSMO_ASSERT(nrm); |
| 955 | return nrm; |
| 956 | } |
| 957 | |
| 958 | static uint32_t gtphub_tei_mapping_have(struct gtphub *hub, |
| 959 | int plane_idx, |
| 960 | struct gtphub_peer_port *from, |
| 961 | uint32_t orig_tei, |
| 962 | time_t now) |
| 963 | { |
| 964 | struct nr_mapping *nrm = gtphub_mapping_have(&hub->tei_map[plane_idx], |
| 965 | from, orig_tei, now); |
Neels Hofmeyr | 334af5d | 2015-11-17 14:24:46 +0100 | [diff] [blame] | 966 | LOG(LOGL_DEBUG, "New %s TEI: (from %s, TEI %u) <-- TEI %u\n", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 967 | gtphub_plane_idx_names[plane_idx], |
| 968 | gtphub_port_str(from), |
Neels Hofmeyr | 334af5d | 2015-11-17 14:24:46 +0100 | [diff] [blame] | 969 | (unsigned int)orig_tei, (unsigned int)nrm->repl); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 970 | |
| 971 | return (uint32_t)nrm->repl; |
| 972 | } |
| 973 | |
Neels Hofmeyr | 3317c84 | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 974 | static void gtphub_map_seq(struct gtp_packet_desc *p, |
| 975 | struct gtphub_peer_port *from_port, |
| 976 | struct gtphub_peer_port *to_port, |
| 977 | time_t now) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 978 | { |
| 979 | /* Store a mapping in to_peer's map, so when we later receive a GTP |
| 980 | * packet back from to_peer, the seq nr can be unmapped back to its |
| 981 | * origin (from_peer here). */ |
| 982 | struct nr_mapping *nrm; |
| 983 | nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map, |
| 984 | from_port, p->seq, now); |
| 985 | |
| 986 | /* Change the GTP packet to yield the new, mapped seq nr */ |
| 987 | set_seq(p, nrm->repl); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p, |
| 991 | struct gtphub_peer_port *responding_port) |
| 992 | { |
| 993 | OSMO_ASSERT(p->version == 1); |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 994 | struct nr_mapping *nrm = |
| 995 | nr_map_get_inv(&responding_port->peer_addr->peer->seq_map, |
| 996 | p->seq); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 997 | if (!nrm) |
| 998 | return NULL; |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 999 | LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n", |
| 1000 | nrm->origin, (int)(nrm->orig), (int)(nrm->repl)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1001 | set_seq(p, nrm->orig); |
| 1002 | return nrm->origin; |
| 1003 | } |
| 1004 | |
| 1005 | static void gtphub_check_restart_counter(struct gtphub *hub, |
| 1006 | struct gtp_packet_desc *p, |
| 1007 | struct gtphub_peer_port *from) |
| 1008 | { |
| 1009 | /* TODO */ |
| 1010 | /* If the peer is sending a Recovery IE (7.7.11) with a restart counter |
| 1011 | * that doesn't match the peer's previously sent restart counter, clear |
| 1012 | * that peer and cancel PDP contexts. */ |
| 1013 | } |
| 1014 | |
| 1015 | static void gtphub_map_restart_counter(struct gtphub *hub, |
| 1016 | struct gtp_packet_desc *p, |
| 1017 | struct gtphub_peer_port *from, |
| 1018 | struct gtphub_peer_port *to) |
| 1019 | { |
| 1020 | /* TODO */ |
| 1021 | } |
| 1022 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1023 | static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p, |
| 1024 | struct gtphub *hub, |
| 1025 | struct gtp_packet_desc *p, |
| 1026 | struct gtphub_peer_port *from_port) |
| 1027 | { |
| 1028 | OSMO_ASSERT(p->version == 1); |
| 1029 | *to_port_p = NULL; |
| 1030 | |
| 1031 | /* If the header's TEI is zero, no PDP context has been established |
| 1032 | * yet. If nonzero, a mapping should actually already exist for this |
| 1033 | * TEI, since it must have been announced in a PDP context creation. */ |
| 1034 | uint32_t tei = p->header_tei; |
| 1035 | if (!tei) |
| 1036 | return 0; |
| 1037 | |
| 1038 | /* to_peer has previously announced a TEI, which was stored and |
| 1039 | * mapped in from_peer's tei_map. */ |
| 1040 | struct nr_mapping *nrm; |
| 1041 | nrm = nr_map_get_inv(&hub->tei_map[p->plane_idx], tei); |
| 1042 | if (!nrm) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1043 | LOG(LOGL_ERROR, "Received unknown TEI %" PRIu32 " from %s\n", |
| 1044 | tei, gtphub_port_str(from_port)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1045 | return -1; |
| 1046 | } |
| 1047 | |
| 1048 | struct gtphub_peer_port *to_port = nrm->origin; |
| 1049 | uint32_t unmapped_tei = nrm->orig; |
| 1050 | set_tei(p, unmapped_tei); |
| 1051 | |
Neels Hofmeyr | 334af5d | 2015-11-17 14:24:46 +0100 | [diff] [blame] | 1052 | LOG(LOGL_DEBUG, "Unmapped TEI coming from %s: %u -> %u (to %s)\n", |
| 1053 | gtphub_port_str(from_port), |
| 1054 | (unsigned int)tei, (unsigned int)unmapped_tei, |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1055 | gtphub_port_str2(to_port)); |
| 1056 | |
| 1057 | *to_port_p = to_port; |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | /* Read GSN address IEs from p, and make sure these peer addresses exist in |
| 1062 | * bind[plane_idx] with default ports, in their respective planes (both Ctrl |
| 1063 | * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into |
| 1064 | * the packet p. */ |
| 1065 | static int gtphub_handle_pdp_ctx_ies(struct gtphub *hub, |
| 1066 | struct gtphub_bind from_bind[], |
| 1067 | struct gtphub_bind to_bind[], |
| 1068 | struct gtp_packet_desc *p, |
| 1069 | time_t now) |
| 1070 | { |
| 1071 | OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL); |
| 1072 | |
| 1073 | int rc; |
| 1074 | int plane_idx; |
| 1075 | |
| 1076 | switch (p->type) { |
| 1077 | case GTP_CREATE_PDP_REQ: |
| 1078 | case GTP_CREATE_PDP_RSP: |
| 1079 | /* Go for it below */ |
| 1080 | break; |
| 1081 | default: |
| 1082 | /* Nothing to do for this message type. */ |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | /* TODO enforce a Request only from SGSN, a Response only from GGSN? */ |
| 1087 | |
| 1088 | osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1), |
| 1089 | plane_nrs_match_GSN_addr_IE_indices); |
| 1090 | |
| 1091 | uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI }; |
| 1092 | int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ); |
| 1093 | |
| 1094 | for (plane_idx = 0; plane_idx < 2; plane_idx++) { |
| 1095 | struct gsn_addr addr_from_ie; |
| 1096 | uint32_t tei_from_ie; |
| 1097 | int ie_idx; |
| 1098 | |
| 1099 | /* Fetch GSN Address and TEI from IEs */ |
| 1100 | rc = gsn_addr_get(&addr_from_ie, p, plane_idx); |
| 1101 | if (rc) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1102 | LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n", |
| 1103 | gtphub_plane_idx_names[plane_idx]); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1104 | return -1; |
| 1105 | } |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1106 | LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1107 | gtphub_plane_idx_names[plane_idx], |
| 1108 | gsn_addr_to_str(&addr_from_ie), |
| 1109 | addr_from_ie.len); |
| 1110 | |
| 1111 | ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0); |
| 1112 | if (ie_idx < 0) { |
| 1113 | if (ie_mandatory) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1114 | LOG(LOGL_ERROR, |
| 1115 | "Create PDP Context message invalid:" |
| 1116 | " missing IE %d\n", |
| 1117 | (int)ie_type[plane_idx]); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1118 | return -1; |
| 1119 | } |
| 1120 | tei_from_ie = 0; |
| 1121 | } |
| 1122 | else |
| 1123 | tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v); |
| 1124 | |
| 1125 | /* Make sure an entry for this peer address with default port |
| 1126 | * exists */ |
| 1127 | struct gtphub_peer_port *peer_from_ie = |
| 1128 | gtphub_port_have(hub, &from_bind[plane_idx], |
| 1129 | &addr_from_ie, |
| 1130 | gtphub_plane_idx_default_port[plane_idx]); |
| 1131 | |
| 1132 | if (tei_from_ie) { |
| 1133 | /* Create TEI mapping and replace in GTP packet IE */ |
| 1134 | uint32_t mapped_tei = |
| 1135 | gtphub_tei_mapping_have(hub, plane_idx, |
| 1136 | peer_from_ie, |
| 1137 | tei_from_ie, |
| 1138 | now); |
| 1139 | p->ie[ie_idx]->tv4.v = hton32(mapped_tei); |
| 1140 | } |
| 1141 | |
| 1142 | /* Replace the GSN address to reflect gtphub. */ |
| 1143 | rc = gsn_addr_put(&to_bind[plane_idx].local_addr, p, plane_idx); |
| 1144 | if (rc) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1145 | LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n", |
| 1146 | gtphub_plane_idx_names[plane_idx]); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1147 | return -1; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | static int gtphub_write(const struct osmo_fd *to, |
| 1155 | const struct osmo_sockaddr *to_addr, |
| 1156 | const uint8_t *buf, size_t buf_len) |
| 1157 | { |
| 1158 | errno = 0; |
| 1159 | ssize_t sent = sendto(to->fd, buf, buf_len, 0, |
| 1160 | (struct sockaddr*)&to_addr->a, to_addr->l); |
Neels Hofmeyr | 3c820ee | 2015-11-17 12:28:09 +0100 | [diff] [blame] | 1161 | LOG(LOGL_DEBUG, "to %s\n", osmo_sockaddr_to_str(to_addr)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1162 | |
| 1163 | if (sent == -1) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1164 | LOG(LOGL_ERROR, "error: %s\n", strerror(errno)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1165 | return -EINVAL; |
| 1166 | } |
| 1167 | |
| 1168 | if (sent != buf_len) |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1169 | LOG(LOGL_ERROR, "sent(%d) != data_len(%d)\n", |
| 1170 | (int)sent, (int)buf_len); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1171 | else |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1172 | LOG(LOGL_DEBUG, "Sent %d\n%s\n", |
| 1173 | (int)sent, osmo_hexdump(buf, sent)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1174 | |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what) |
| 1179 | { |
| 1180 | unsigned int plane_idx = from_ggsns_ofd->priv_nr; |
| 1181 | OSMO_ASSERT(plane_idx < GTPH_PLANE_N); |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1182 | LOG(LOGL_DEBUG, "\n\n=== reading from GGSN (%s)\n", |
| 1183 | gtphub_plane_idx_names[plane_idx]); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1184 | if (!(what & BSC_FD_READ)) |
| 1185 | return 0; |
| 1186 | |
| 1187 | struct gtphub *hub = from_ggsns_ofd->data; |
| 1188 | |
| 1189 | static uint8_t buf[4096]; |
| 1190 | struct osmo_sockaddr from_addr; |
| 1191 | struct osmo_sockaddr to_addr; |
| 1192 | struct osmo_fd *to_ofd; |
Neels Hofmeyr | 16c3f57 | 2015-11-11 17:27:01 +0100 | [diff] [blame] | 1193 | int len; |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1194 | uint8_t *reply_buf; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1195 | |
| 1196 | len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf)); |
| 1197 | if (len < 1) |
| 1198 | return 0; |
| 1199 | |
| 1200 | len = gtphub_from_ggsns_handle_buf(hub, plane_idx, &from_addr, buf, len, |
| 1201 | gtphub_now(), |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1202 | &reply_buf, &to_ofd, &to_addr); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1203 | if (len < 1) |
| 1204 | return 0; |
| 1205 | |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1206 | return gtphub_write(to_ofd, &to_addr, reply_buf, len); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | static int gtphub_unmap(struct gtphub *hub, |
| 1210 | struct gtp_packet_desc *p, |
| 1211 | struct gtphub_peer_port *from, |
| 1212 | struct gtphub_peer_port *to_proxy, |
| 1213 | struct gtphub_peer_port **final_unmapped, |
| 1214 | struct gtphub_peer_port **unmapped_from_seq, |
| 1215 | struct gtphub_peer_port **unmapped_from_tei) |
| 1216 | { |
| 1217 | /* Always (try to) unmap sequence and TEI numbers, which need to be |
| 1218 | * replaced in the packet. Either way, give precedence to the proxy, if |
| 1219 | * configured. */ |
| 1220 | |
| 1221 | struct gtphub_peer_port *from_seq = NULL; |
| 1222 | struct gtphub_peer_port *from_tei = NULL; |
| 1223 | struct gtphub_peer_port *unmapped = NULL; |
| 1224 | |
| 1225 | if (unmapped_from_seq) |
| 1226 | *unmapped_from_seq = from_seq; |
| 1227 | if (unmapped_from_tei) |
| 1228 | *unmapped_from_tei = from_tei; |
| 1229 | if (final_unmapped) |
| 1230 | *final_unmapped = unmapped; |
| 1231 | |
| 1232 | from_seq = gtphub_unmap_seq(p, from); |
| 1233 | |
| 1234 | if (gtphub_unmap_header_tei(&from_tei, hub, p, from) < 0) |
| 1235 | return -1; |
| 1236 | |
| 1237 | struct gtphub_peer *from_peer = from->peer_addr->peer; |
| 1238 | if (from_seq && from_tei && (from_seq != from_tei)) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1239 | LOG(LOGL_DEBUG, |
| 1240 | "Seq unmap and TEI unmap yield two different peers." |
| 1241 | " Using seq unmap." |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1242 | "(from %s %s: seq %d yields %s, tei %u yields %s)\n", |
| 1243 | gtphub_plane_idx_names[p->plane_idx], |
| 1244 | gtphub_peer_str(from_peer), |
| 1245 | (int)p->seq, |
| 1246 | gtphub_port_str(from_seq), |
Neels Hofmeyr | 334af5d | 2015-11-17 14:24:46 +0100 | [diff] [blame] | 1247 | (unsigned int)p->header_tei, |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1248 | gtphub_port_str2(from_tei) |
| 1249 | ); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1250 | } |
| 1251 | unmapped = (from_seq? from_seq : from_tei); |
| 1252 | |
| 1253 | if (unmapped && to_proxy && (unmapped != to_proxy)) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1254 | LOG(LOGL_NOTICE, |
| 1255 | "Unmap yields a different peer than the configured proxy." |
| 1256 | " Using proxy." |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1257 | " unmapped: %s proxy: %s\n", |
| 1258 | gtphub_port_str(unmapped), |
| 1259 | gtphub_port_str2(to_proxy) |
| 1260 | ); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1261 | } |
| 1262 | unmapped = (to_proxy? to_proxy : unmapped); |
| 1263 | |
| 1264 | if (!unmapped) { |
| 1265 | /* Return no error, but returned pointers are all NULL. */ |
| 1266 | return 0; |
| 1267 | } |
| 1268 | |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1269 | LOG(LOGL_DEBUG, "from seq %p; from tei %p; unmapped => %p\n", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1270 | from_seq, from_tei, unmapped); |
| 1271 | |
| 1272 | if (unmapped_from_seq) |
| 1273 | *unmapped_from_seq = from_seq; |
| 1274 | if (unmapped_from_tei) |
| 1275 | *unmapped_from_tei = from_tei; |
| 1276 | if (final_unmapped) |
| 1277 | *final_unmapped = unmapped; |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
| 1281 | static int gsn_addr_to_sockaddr(struct gsn_addr *src, |
| 1282 | uint16_t port, |
| 1283 | struct osmo_sockaddr *dst) |
| 1284 | { |
| 1285 | return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port); |
| 1286 | } |
| 1287 | |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1288 | /* If p is an Echo request, replace p's data with the matching response and |
| 1289 | * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is |
| 1290 | * detected. */ |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1291 | static int gtphub_handle_echo(struct gtphub *hub, struct gtp_packet_desc *p, |
| 1292 | uint8_t **reply_buf) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1293 | { |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1294 | if (p->type != GTP_ECHO_REQ) |
| 1295 | return 0; |
| 1296 | |
| 1297 | static uint8_t echo_response_data[14] = { |
| 1298 | 0x32, /* flags */ |
| 1299 | GTP_ECHO_RSP, |
| 1300 | 0x00, 14 - 8, /* Length in network byte order */ |
| 1301 | 0x00, 0x00, 0x00, 0x00, /* Zero TEI */ |
| 1302 | 0, 0, /* Seq, to be replaced */ |
| 1303 | 0, 0, /* no extensions */ |
| 1304 | 0x0e, /* Recovery IE */ |
| 1305 | 0 /* Recovery counter, to be replaced */ |
| 1306 | }; |
| 1307 | uint16_t *seq = (uint16_t*)&echo_response_data[8]; |
| 1308 | uint8_t *recovery = &echo_response_data[13]; |
| 1309 | |
| 1310 | *seq = hton16(p->seq); |
| 1311 | *recovery = hub->restart_counter; |
| 1312 | |
| 1313 | *reply_buf = echo_response_data; |
| 1314 | |
| 1315 | return sizeof(echo_response_data); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1316 | } |
| 1317 | |
Neels Hofmeyr | c83cd89 | 2015-11-11 14:01:06 +0100 | [diff] [blame] | 1318 | struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind, |
| 1319 | const struct osmo_sockaddr *addr); |
| 1320 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1321 | /* Parse buffer as GTP packet, replace elements in-place and return the ofd and |
| 1322 | * address to forward to. Return a pointer to the osmo_fd, but copy the |
| 1323 | * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at |
| 1324 | * any moment, while the osmo_fd is guaranteed to persist. Return the number of |
| 1325 | * bytes to forward, 0 or less on failure. */ |
| 1326 | int gtphub_from_ggsns_handle_buf(struct gtphub *hub, |
| 1327 | unsigned int plane_idx, |
| 1328 | const struct osmo_sockaddr *from_addr, |
| 1329 | uint8_t *buf, |
| 1330 | size_t received, |
| 1331 | time_t now, |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1332 | uint8_t **reply_buf, |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1333 | struct osmo_fd **to_ofd, |
| 1334 | struct osmo_sockaddr *to_addr) |
| 1335 | { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1336 | LOG(LOGL_DEBUG, "<- rx %s from GGSN %s\n", |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 1337 | gtphub_plane_idx_names[plane_idx], |
| 1338 | osmo_sockaddr_to_str(from_addr)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1339 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1340 | static struct gtp_packet_desc p; |
| 1341 | gtp_decode(buf, received, plane_idx, &p); |
| 1342 | |
| 1343 | if (p.rc <= 0) |
| 1344 | return -1; |
| 1345 | |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1346 | int reply_len; |
| 1347 | reply_len = gtphub_handle_echo(hub, &p, reply_buf); |
| 1348 | if (reply_len > 0) { |
| 1349 | /* It was an echo. Nothing left to do. */ |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1350 | osmo_sockaddr_copy(to_addr, from_addr); |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1351 | *to_ofd = &hub->to_ggsns[plane_idx].ofd; |
| 1352 | return reply_len; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1353 | } |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1354 | if (reply_len < 0) |
| 1355 | return -1; |
| 1356 | |
| 1357 | *to_ofd = &hub->to_sgsns[plane_idx].ofd; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1358 | |
| 1359 | /* If a GGSN proxy is configured, check that it's indeed that proxy |
| 1360 | * talking to us. A proxy is a forced 1:1 connection, e.g. to another |
| 1361 | * gtphub, so no-one else is allowed to talk to us from that side. */ |
| 1362 | struct gtphub_peer_port *ggsn = hub->ggsn_proxy[plane_idx]; |
| 1363 | if (ggsn) { |
| 1364 | if (osmo_sockaddr_cmp(&ggsn->sa, from_addr) != 0) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1365 | LOG(LOGL_ERROR, |
| 1366 | "Rejecting: GGSN proxy configured, but GTP packet" |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1367 | " received on GGSN bind is from another sender:" |
| 1368 | " proxy: %s sender: %s\n", |
| 1369 | gtphub_port_str(ggsn), |
| 1370 | osmo_sockaddr_to_str(from_addr)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1371 | return -1; |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | if (!ggsn) { |
Neels Hofmeyr | c83cd89 | 2015-11-11 14:01:06 +0100 | [diff] [blame] | 1376 | /* Find a GGSN peer with a matching address. The sender's port |
| 1377 | * may in fact differ. */ |
| 1378 | ggsn = gtphub_known_addr_have_port(&hub->to_ggsns[plane_idx], |
| 1379 | from_addr); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | /* If any PDP context has been created, we already have an entry for |
| 1383 | * this GGSN. If we don't have an entry, the GGSN has nothing to tell |
| 1384 | * us about. */ |
| 1385 | if (!ggsn) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1386 | LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n", |
| 1387 | osmo_sockaddr_to_str(from_addr)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1388 | return -1; |
| 1389 | } |
| 1390 | |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1391 | LOG(LOGL_DEBUG, "GGSN peer: %s\n", gtphub_port_str(ggsn)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1392 | |
| 1393 | struct gtphub_peer_port *sgsn_from_seq; |
| 1394 | struct gtphub_peer_port *sgsn; |
| 1395 | if (gtphub_unmap(hub, &p, ggsn, |
| 1396 | hub->sgsn_proxy[plane_idx], |
| 1397 | &sgsn, &sgsn_from_seq, |
| 1398 | NULL /* not interested, got it in &sgsn already */ |
| 1399 | ) |
| 1400 | != 0) { |
| 1401 | return -1; |
| 1402 | } |
| 1403 | |
| 1404 | if (!sgsn) { |
| 1405 | /* A GGSN initiated request would go to a known TEI. So this is |
| 1406 | * bogus. */ |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1407 | LOG(LOGL_ERROR, "No SGSN to send to. Dropping packet.\n"); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1408 | return -1; |
| 1409 | } |
| 1410 | |
| 1411 | if (plane_idx == GTPH_PLANE_CTRL) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1412 | /* This may be a Create PDP Context response. If it is, there |
| 1413 | * are other addresses in the GTP message to set up apart from |
| 1414 | * the sender. */ |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1415 | if (gtphub_handle_pdp_ctx_ies(hub, hub->to_ggsns, |
| 1416 | hub->to_sgsns, &p, now) |
| 1417 | != 0) |
| 1418 | return -1; |
| 1419 | } |
| 1420 | |
| 1421 | gtphub_check_restart_counter(hub, &p, ggsn); |
| 1422 | gtphub_map_restart_counter(hub, &p, ggsn, sgsn); |
| 1423 | |
| 1424 | /* If the GGSN is replying to an SGSN request, the sequence nr has |
| 1425 | * already been unmapped above (sgsn_from_seq != NULL), and we need not |
| 1426 | * create a new mapping. */ |
| 1427 | if (!sgsn_from_seq) |
| 1428 | gtphub_map_seq(&p, ggsn, sgsn, now); |
| 1429 | |
| 1430 | osmo_sockaddr_copy(to_addr, &sgsn->sa); |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1431 | |
| 1432 | *reply_buf = (uint8_t*)p.data; |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 1433 | |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1434 | LOG(LOGL_DEBUG, "<-- Forward to SGSN: %d bytes to %s\n", |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 1435 | (int)received, osmo_sockaddr_to_str(to_addr)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1436 | return received; |
| 1437 | } |
| 1438 | |
| 1439 | static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what) |
| 1440 | { |
| 1441 | unsigned int plane_idx = from_sgsns_ofd->priv_nr; |
| 1442 | OSMO_ASSERT(plane_idx < GTPH_PLANE_N); |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1443 | LOG(LOGL_DEBUG, "\n\n=== reading from SGSN (%s)\n", |
| 1444 | gtphub_plane_idx_names[plane_idx]); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1445 | |
| 1446 | if (!(what & BSC_FD_READ)) |
| 1447 | return 0; |
| 1448 | |
| 1449 | struct gtphub *hub = from_sgsns_ofd->data; |
| 1450 | |
| 1451 | static uint8_t buf[4096]; |
| 1452 | struct osmo_sockaddr from_addr; |
| 1453 | struct osmo_sockaddr to_addr; |
| 1454 | struct osmo_fd *to_ofd; |
Neels Hofmeyr | 16c3f57 | 2015-11-11 17:27:01 +0100 | [diff] [blame] | 1455 | int len; |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1456 | uint8_t *reply_buf; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1457 | |
| 1458 | len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf)); |
| 1459 | if (len < 1) |
| 1460 | return 0; |
| 1461 | |
| 1462 | len = gtphub_from_sgsns_handle_buf(hub, plane_idx, &from_addr, buf, len, |
| 1463 | gtphub_now(), |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1464 | &reply_buf, &to_ofd, &to_addr); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1465 | if (len < 1) |
| 1466 | return 0; |
| 1467 | |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1468 | return gtphub_write(to_ofd, &to_addr, reply_buf, len); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | /* Analogous to gtphub_from_ggsns_handle_buf(), see the comment there. */ |
| 1472 | int gtphub_from_sgsns_handle_buf(struct gtphub *hub, |
| 1473 | unsigned int plane_idx, |
| 1474 | const struct osmo_sockaddr *from_addr, |
| 1475 | uint8_t *buf, |
| 1476 | size_t received, |
| 1477 | time_t now, |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1478 | uint8_t **reply_buf, |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1479 | struct osmo_fd **to_ofd, |
| 1480 | struct osmo_sockaddr *to_addr) |
| 1481 | { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1482 | LOG(LOGL_DEBUG, "-> rx %s from SGSN %s\n", |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 1483 | gtphub_plane_idx_names[plane_idx], |
| 1484 | osmo_sockaddr_to_str(from_addr)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1485 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1486 | static struct gtp_packet_desc p; |
| 1487 | gtp_decode(buf, received, plane_idx, &p); |
| 1488 | |
| 1489 | if (p.rc <= 0) |
| 1490 | return -1; |
| 1491 | |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1492 | int reply_len; |
| 1493 | reply_len = gtphub_handle_echo(hub, &p, reply_buf); |
| 1494 | if (reply_len > 0) { |
| 1495 | /* It was an echo. Nothing left to do. */ |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1496 | osmo_sockaddr_copy(to_addr, from_addr); |
Neels Hofmeyr | 6187e01 | 2015-11-20 00:57:05 +0100 | [diff] [blame] | 1497 | *to_ofd = &hub->to_sgsns[plane_idx].ofd; |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1498 | return reply_len; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1499 | } |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1500 | if (reply_len < 0) |
| 1501 | return -1; |
| 1502 | |
| 1503 | *to_ofd = &hub->to_ggsns[plane_idx].ofd; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1504 | |
| 1505 | /* If an SGSN proxy is configured, check that it's indeed that proxy |
| 1506 | * talking to us. A proxy is a forced 1:1 connection, e.g. to another |
| 1507 | * gtphub, so no-one else is allowed to talk to us from that side. */ |
| 1508 | struct gtphub_peer_port *sgsn = hub->sgsn_proxy[plane_idx]; |
| 1509 | if (sgsn) { |
| 1510 | if (osmo_sockaddr_cmp(&sgsn->sa, from_addr) != 0) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1511 | LOG(LOGL_ERROR, |
| 1512 | "Rejecting: GGSN proxy configured, but GTP packet" |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1513 | " received on GGSN bind is from another sender:" |
| 1514 | " proxy: %s sender: %s\n", |
| 1515 | gtphub_port_str(sgsn), |
| 1516 | osmo_sockaddr_to_str(from_addr)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1517 | return -1; |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | if (!sgsn) { |
| 1522 | /* If any contact has been made before, we already have an |
Neels Hofmeyr | c83cd89 | 2015-11-11 14:01:06 +0100 | [diff] [blame] | 1523 | * entry for this SGSN. The port may differ. */ |
| 1524 | sgsn = gtphub_known_addr_have_port(&hub->to_sgsns[plane_idx], |
| 1525 | from_addr); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | if (!sgsn) { |
| 1529 | /* A new peer. If this is on the Ctrl plane, an SGSN may make |
| 1530 | * first contact without being known yet, so create the peer |
| 1531 | * struct for the current sender. */ |
| 1532 | if (plane_idx != GTPH_PLANE_CTRL) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1533 | LOG(LOGL_ERROR, |
| 1534 | "User plane peer was not announced by PDP Context," |
| 1535 | " discarding: %s\n", |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1536 | osmo_sockaddr_to_str(from_addr)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1537 | return -1; |
| 1538 | } |
| 1539 | |
| 1540 | struct gsn_addr from_gsna; |
| 1541 | uint16_t from_port; |
| 1542 | if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0) |
| 1543 | return -1; |
| 1544 | |
| 1545 | sgsn = gtphub_port_have(hub, &hub->to_sgsns[plane_idx], |
| 1546 | &from_gsna, from_port); |
| 1547 | } |
| 1548 | |
| 1549 | if (!sgsn) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1550 | /* This could theoretically happen for invalid address data or |
| 1551 | * somesuch. */ |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1552 | LOG(LOGL_ERROR, "Dropping packet: invalid SGSN peer: %s\n", |
| 1553 | osmo_sockaddr_to_str(from_addr)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1554 | return -1; |
| 1555 | } |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1556 | LOG(LOGL_DEBUG, "SGSN peer: %s\n", gtphub_port_str(sgsn)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1557 | |
| 1558 | struct gtphub_peer_port *ggsn_from_seq; |
| 1559 | struct gtphub_peer_port *ggsn; |
| 1560 | if (gtphub_unmap(hub, &p, sgsn, |
| 1561 | hub->ggsn_proxy[plane_idx], |
| 1562 | &ggsn, &ggsn_from_seq, |
| 1563 | NULL /* not interested, got it in &ggsn already */ |
| 1564 | ) |
| 1565 | != 0) { |
| 1566 | return -1; |
| 1567 | } |
| 1568 | |
Neels Hofmeyr | a208c73 | 2015-11-11 19:26:09 +0100 | [diff] [blame] | 1569 | if (!ggsn) { |
| 1570 | if (gtphub_resolve_ggsn(hub, &p, &ggsn) < 0) |
| 1571 | return -1; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1572 | } |
| 1573 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1574 | if (!ggsn) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1575 | LOG(LOGL_ERROR, "No GGSN to send to. Dropping packet.\n"); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1576 | return -1; |
| 1577 | } |
| 1578 | |
| 1579 | if (plane_idx == GTPH_PLANE_CTRL) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1580 | /* This may be a Create PDP Context requst. If it is, there are |
| 1581 | * other addresses in the GTP message to set up apart from the |
| 1582 | * sender. */ |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1583 | if (gtphub_handle_pdp_ctx_ies(hub, hub->to_sgsns, |
| 1584 | hub->to_ggsns, &p, now) |
| 1585 | != 0) |
| 1586 | return -1; |
| 1587 | } |
| 1588 | |
| 1589 | gtphub_check_restart_counter(hub, &p, sgsn); |
| 1590 | gtphub_map_restart_counter(hub, &p, sgsn, ggsn); |
| 1591 | |
| 1592 | /* If the SGSN is replying to a GGSN request, the sequence nr has |
| 1593 | * already been unmapped above (unmap_ggsn != NULL), and we need not |
| 1594 | * create a new outgoing sequence map. */ |
| 1595 | if (!ggsn_from_seq) |
| 1596 | gtphub_map_seq(&p, sgsn, ggsn, now); |
| 1597 | |
| 1598 | osmo_sockaddr_copy(to_addr, &ggsn->sa); |
| 1599 | |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1600 | *reply_buf = (uint8_t*)p.data; |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 1601 | |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1602 | LOG(LOGL_DEBUG, "--> Forward to GGSN: %d bytes to %s\n", |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 1603 | (int)received, osmo_sockaddr_to_str(to_addr)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1604 | return received; |
| 1605 | } |
| 1606 | |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 1607 | static void resolved_gssn_del_cb(struct expiring_item *expi) |
| 1608 | { |
| 1609 | struct gtphub_resolved_ggsn *ggsn; |
| 1610 | ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry); |
| 1611 | |
| 1612 | gtphub_port_ref_count_dec(ggsn->peer); |
| 1613 | llist_del(&ggsn->entry); |
| 1614 | |
| 1615 | ggsn->expiry_entry.del_cb = 0; |
| 1616 | expiring_item_del(&ggsn->expiry_entry); |
| 1617 | |
| 1618 | talloc_free(ggsn); |
| 1619 | } |
| 1620 | |
| 1621 | void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str, |
| 1622 | struct gsn_addr *resolved_addr, |
| 1623 | time_t now) |
| 1624 | { |
| 1625 | struct gtphub_peer_port *pp; |
| 1626 | struct gtphub_resolved_ggsn *ggsn; |
| 1627 | |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1628 | LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n", |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1629 | apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr, |
| 1630 | sizeof(*resolved_addr))); |
Neels Hofmeyr | 3317c84 | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 1631 | |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 1632 | pp = gtphub_port_have(hub, &hub->to_ggsns[GTPH_PLANE_CTRL], |
| 1633 | resolved_addr, 2123); |
| 1634 | if (!pp) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1635 | LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n", |
| 1636 | gsn_addr_to_str(resolved_addr)); |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 1637 | return; |
| 1638 | } |
| 1639 | |
| 1640 | ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn); |
| 1641 | OSMO_ASSERT(ggsn); |
Neels Hofmeyr | a4370dd | 2015-11-24 12:46:11 +0100 | [diff] [blame] | 1642 | INIT_LLIST_HEAD(&ggsn->entry); |
| 1643 | expiring_item_init(&ggsn->expiry_entry); |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 1644 | |
| 1645 | ggsn->peer = pp; |
| 1646 | gtphub_port_ref_count_inc(pp); |
| 1647 | |
| 1648 | strncpy(ggsn->apn_oi_str, apn_oi_str, sizeof(ggsn->apn_oi_str)); |
Neels Hofmeyr | 3c820ee | 2015-11-17 12:28:09 +0100 | [diff] [blame] | 1649 | ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0'; |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 1650 | |
| 1651 | ggsn->expiry_entry.del_cb = resolved_gssn_del_cb; |
| 1652 | expiry_add(&hub->expire_tei_maps, &ggsn->expiry_entry, now); |
| 1653 | |
| 1654 | llist_add(&ggsn->entry, &hub->resolved_ggsns); |
| 1655 | } |
| 1656 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1657 | static int gtphub_gc_peer_port(struct gtphub_peer_port *pp) |
| 1658 | { |
| 1659 | return pp->ref_count == 0; |
| 1660 | } |
| 1661 | |
| 1662 | static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa) |
| 1663 | { |
| 1664 | struct gtphub_peer_port *pp, *npp; |
| 1665 | llist_for_each_entry_safe(pp, npp, &pa->ports, entry) { |
| 1666 | if (gtphub_gc_peer_port(pp)) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1667 | LOG(LOGL_DEBUG, "expired: peer %s\n", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1668 | gtphub_port_str(pp)); |
| 1669 | gtphub_peer_port_del(pp); |
| 1670 | } |
| 1671 | } |
| 1672 | return llist_empty(&pa->ports); |
| 1673 | } |
| 1674 | |
| 1675 | static int gtphub_gc_peer(struct gtphub_peer *p) |
| 1676 | { |
| 1677 | struct gtphub_peer_addr *pa, *npa; |
| 1678 | llist_for_each_entry_safe(pa, npa, &p->addresses, entry) { |
| 1679 | if (gtphub_gc_peer_addr(pa)) { |
| 1680 | gtphub_peer_addr_del(pa); |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | /* Note that there's a ref_count in each gtphub_peer_port instance |
| 1685 | * listed within p->addresses, referenced by TEI mappings from |
| 1686 | * hub->tei_map. As long as those don't expire, this peer will stay. */ |
| 1687 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1688 | return llist_empty(&p->addresses) |
| 1689 | && nr_map_empty(&p->seq_map); |
| 1690 | } |
| 1691 | |
| 1692 | static void gtphub_gc_bind(struct gtphub_bind *b) |
| 1693 | { |
| 1694 | struct gtphub_peer *p, *n; |
| 1695 | llist_for_each_entry_safe(p, n, &b->peers, entry) { |
| 1696 | if (gtphub_gc_peer(p)) { |
| 1697 | gtphub_peer_del(p); |
| 1698 | } |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | void gtphub_gc(struct gtphub *hub, time_t now) |
| 1703 | { |
| 1704 | int expired; |
| 1705 | expired = expiry_tick(&hub->expire_seq_maps, now); |
| 1706 | expired += expiry_tick(&hub->expire_tei_maps, now); |
| 1707 | |
| 1708 | /* ... */ |
| 1709 | |
| 1710 | if (expired) { |
| 1711 | int i; |
| 1712 | for (i = 0; i < GTPH_PLANE_N; i++) { |
| 1713 | gtphub_gc_bind(&hub->to_sgsns[i]); |
| 1714 | gtphub_gc_bind(&hub->to_ggsns[i]); |
| 1715 | } |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | static void gtphub_gc_cb(void *data) |
| 1720 | { |
| 1721 | struct gtphub *hub = data; |
| 1722 | gtphub_gc(hub, gtphub_now()); |
| 1723 | osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0); |
| 1724 | } |
| 1725 | |
| 1726 | static void gtphub_gc_start(struct gtphub *hub) |
| 1727 | { |
| 1728 | hub->gc_timer.cb = gtphub_gc_cb; |
| 1729 | hub->gc_timer.data = hub; |
| 1730 | |
| 1731 | osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0); |
| 1732 | } |
| 1733 | |
| 1734 | /* called by unit tests */ |
| 1735 | void gtphub_init(struct gtphub *hub) |
| 1736 | { |
| 1737 | gtphub_zero(hub); |
| 1738 | |
| 1739 | expiry_init(&hub->expire_seq_maps, GTPH_SEQ_MAPPING_EXPIRY_SECS); |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1740 | expiry_init(&hub->expire_tei_maps, |
| 1741 | GTPH_TEI_MAPPING_EXPIRY_MINUTES * 60); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1742 | |
| 1743 | int plane_idx; |
| 1744 | for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { |
Neels Hofmeyr | e2ed8e6 | 2015-11-17 14:30:37 +0100 | [diff] [blame] | 1745 | nr_pool_init(&hub->tei_pool[plane_idx], 1, 0xffffffff); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1746 | nr_map_init(&hub->tei_map[plane_idx], |
| 1747 | &hub->tei_pool[plane_idx], |
| 1748 | &hub->expire_tei_maps); |
| 1749 | |
| 1750 | gtphub_bind_init(&hub->to_ggsns[plane_idx]); |
| 1751 | gtphub_bind_init(&hub->to_sgsns[plane_idx]); |
| 1752 | } |
Neels Hofmeyr | 390e910 | 2015-11-16 13:45:13 +0100 | [diff] [blame] | 1753 | |
| 1754 | hub->to_sgsns[GTPH_PLANE_CTRL].label = "SGSN Ctrl"; |
| 1755 | hub->to_ggsns[GTPH_PLANE_CTRL].label = "GGSN Ctrl"; |
| 1756 | hub->to_sgsns[GTPH_PLANE_USER].label = "SGSN User"; |
| 1757 | hub->to_ggsns[GTPH_PLANE_USER].label = "GGSN User"; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | static int gtphub_make_proxy(struct gtphub *hub, |
| 1761 | struct gtphub_peer_port **pp, |
| 1762 | struct gtphub_bind *bind, |
| 1763 | const struct gtphub_cfg_addr *addr) |
| 1764 | { |
| 1765 | if (!addr->addr_str) |
| 1766 | return 0; |
| 1767 | |
| 1768 | struct gsn_addr gsna; |
| 1769 | if (gsn_addr_from_str(&gsna, addr->addr_str) != 0) |
| 1770 | return -1; |
| 1771 | |
| 1772 | *pp = gtphub_port_have(hub, bind, &gsna, addr->port); |
| 1773 | |
| 1774 | /* This is *the* proxy. Make sure it is never expired. */ |
| 1775 | gtphub_port_ref_count_inc(*pp); |
| 1776 | return 0; |
| 1777 | } |
| 1778 | |
| 1779 | int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg) |
| 1780 | { |
| 1781 | int rc; |
| 1782 | |
| 1783 | gtphub_init(hub); |
Neels Hofmeyr | b6c2db5 | 2015-11-18 18:11:32 +0100 | [diff] [blame^] | 1784 | |
| 1785 | /* If a Ctrl plane proxy is configured, ares will never be used. */ |
| 1786 | if (!cfg->ggsn_proxy[GTPH_PLANE_CTRL].addr_str) { |
| 1787 | if (gtphub_ares_init(hub) != 0) { |
| 1788 | LOG(LOGL_FATAL, "Failed to initialize ares\n"); |
| 1789 | return -1; |
| 1790 | } |
| 1791 | } |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1792 | |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1793 | /* TODO set hub->restart_counter from external file. */ |
| 1794 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1795 | int plane_idx; |
| 1796 | for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { |
| 1797 | rc = gtphub_bind_start(&hub->to_ggsns[plane_idx], |
| 1798 | &cfg->to_ggsns[plane_idx], |
| 1799 | from_ggsns_read_cb, hub, plane_idx); |
| 1800 | if (rc) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1801 | LOG(LOGL_FATAL, "Failed to bind for GGSNs (%s)\n", |
| 1802 | gtphub_plane_idx_names[plane_idx]); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1803 | return rc; |
| 1804 | } |
| 1805 | |
| 1806 | rc = gtphub_bind_start(&hub->to_sgsns[plane_idx], |
| 1807 | &cfg->to_sgsns[plane_idx], |
| 1808 | from_sgsns_read_cb, hub, plane_idx); |
| 1809 | if (rc) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1810 | LOG(LOGL_FATAL, "Failed to bind for SGSNs (%s)\n", |
| 1811 | gtphub_plane_idx_names[plane_idx]); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1812 | return rc; |
| 1813 | } |
| 1814 | } |
| 1815 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1816 | for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { |
| 1817 | if (gtphub_make_proxy(hub, |
| 1818 | &hub->sgsn_proxy[plane_idx], |
| 1819 | &hub->to_sgsns[plane_idx], |
| 1820 | &cfg->sgsn_proxy[plane_idx]) |
| 1821 | != 0) { |
Neels Hofmeyr | 3d3aa8f | 2015-11-17 12:26:02 +0100 | [diff] [blame] | 1822 | LOG(LOGL_FATAL, "Cannot configure SGSN proxy" |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1823 | " %s port %d.\n", |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1824 | cfg->sgsn_proxy[plane_idx].addr_str, |
| 1825 | (int)cfg->sgsn_proxy[plane_idx].port); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1826 | return -1; |
| 1827 | } |
| 1828 | if (gtphub_make_proxy(hub, |
| 1829 | &hub->ggsn_proxy[plane_idx], |
| 1830 | &hub->to_ggsns[plane_idx], |
| 1831 | &cfg->ggsn_proxy[plane_idx]) |
| 1832 | != 0) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1833 | LOG(LOGL_ERROR, "Cannot configure GGSN proxy.\n"); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1834 | return -1; |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { |
| 1839 | if (hub->sgsn_proxy[plane_idx]) |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1840 | LOG(LOGL_NOTICE, "Using SGSN %s proxy %s\n", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1841 | gtphub_plane_idx_names[plane_idx], |
| 1842 | gtphub_port_str(hub->sgsn_proxy[plane_idx])); |
| 1843 | } |
| 1844 | |
| 1845 | for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { |
Neels Hofmeyr | 3c820ee | 2015-11-17 12:28:09 +0100 | [diff] [blame] | 1846 | if (hub->ggsn_proxy[plane_idx]) |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1847 | LOG(LOGL_NOTICE, "Using GGSN %s proxy %s\n", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1848 | gtphub_plane_idx_names[plane_idx], |
| 1849 | gtphub_port_str(hub->ggsn_proxy[plane_idx])); |
| 1850 | } |
| 1851 | |
| 1852 | gtphub_gc_start(hub); |
| 1853 | return 0; |
| 1854 | } |
| 1855 | |
| 1856 | static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer, |
| 1857 | const struct gsn_addr *addr) |
| 1858 | { |
| 1859 | struct gtphub_peer_addr *a; |
| 1860 | llist_for_each_entry(a, &peer->addresses, entry) { |
| 1861 | if (gsn_addr_same(&a->addr, addr)) |
| 1862 | return a; |
| 1863 | } |
| 1864 | return NULL; |
| 1865 | } |
| 1866 | |
| 1867 | static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a, |
| 1868 | uint16_t port) |
| 1869 | { |
| 1870 | OSMO_ASSERT(port); |
| 1871 | struct gtphub_peer_port *pp; |
| 1872 | llist_for_each_entry(pp, &a->ports, entry) { |
| 1873 | if (pp->port == port) |
| 1874 | return pp; |
| 1875 | } |
| 1876 | return NULL; |
| 1877 | } |
| 1878 | |
| 1879 | static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind, |
| 1880 | const struct gsn_addr *addr) |
| 1881 | { |
| 1882 | struct gtphub_peer *peer; |
| 1883 | llist_for_each_entry(peer, &bind->peers, entry) { |
| 1884 | struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr); |
| 1885 | if (a) |
| 1886 | return a; |
| 1887 | } |
| 1888 | return NULL; |
| 1889 | } |
| 1890 | |
| 1891 | static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind, |
| 1892 | const struct gsn_addr *addr, |
| 1893 | uint16_t port) |
| 1894 | { |
| 1895 | struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr); |
| 1896 | if (!a) |
| 1897 | return NULL; |
| 1898 | return gtphub_addr_find_port(a, port); |
| 1899 | } |
| 1900 | |
| 1901 | struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind, |
| 1902 | const struct osmo_sockaddr *addr) |
| 1903 | { |
| 1904 | struct gsn_addr gsna; |
| 1905 | uint16_t port; |
| 1906 | gsn_addr_from_sockaddr(&gsna, &port, addr); |
| 1907 | return gtphub_port_find(bind, &gsna, port); |
| 1908 | } |
| 1909 | |
| 1910 | static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub, |
| 1911 | struct gtphub_bind *bind) |
| 1912 | { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1913 | struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx, |
| 1914 | struct gtphub_peer); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1915 | OSMO_ASSERT(peer); |
| 1916 | |
| 1917 | INIT_LLIST_HEAD(&peer->addresses); |
| 1918 | |
Neels Hofmeyr | e2ed8e6 | 2015-11-17 14:30:37 +0100 | [diff] [blame] | 1919 | nr_pool_init(&peer->seq_pool, 0, 0xffff); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1920 | nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_seq_maps); |
| 1921 | |
| 1922 | /* TODO use something random to pick the initial sequence nr. |
| 1923 | 0x6d31 produces the ASCII character sequence 'm1', currently used in |
| 1924 | gtphub_nc_test.sh. */ |
| 1925 | peer->seq_pool.last_nr = 0x6d31 - 1; |
| 1926 | |
| 1927 | llist_add(&peer->entry, &bind->peers); |
| 1928 | return peer; |
| 1929 | } |
| 1930 | |
| 1931 | static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer, |
| 1932 | const struct gsn_addr *addr) |
| 1933 | { |
| 1934 | struct gtphub_peer_addr *a; |
| 1935 | a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr); |
| 1936 | OSMO_ASSERT(a); |
| 1937 | a->peer = peer; |
| 1938 | gsn_addr_copy(&a->addr, addr); |
| 1939 | INIT_LLIST_HEAD(&a->ports); |
| 1940 | llist_add(&a->entry, &peer->addresses); |
| 1941 | |
| 1942 | return a; |
| 1943 | } |
| 1944 | |
| 1945 | static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub, |
| 1946 | struct gtphub_bind *bind, |
| 1947 | const struct gsn_addr *addr) |
| 1948 | { |
| 1949 | struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr); |
| 1950 | if (a) |
| 1951 | return a; |
| 1952 | |
| 1953 | /* If we haven't found an address, that means we need to create an |
| 1954 | * entirely new peer for the new address. More addresses may be added |
| 1955 | * to this peer later, but not via this function. */ |
| 1956 | struct gtphub_peer *peer = gtphub_peer_new(hub, bind); |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 1957 | |
| 1958 | a = gtphub_peer_add_addr(peer, addr); |
| 1959 | |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1960 | LOG(LOGL_DEBUG, "New peer address: %s %s\n", |
Neels Hofmeyr | 390e910 | 2015-11-16 13:45:13 +0100 | [diff] [blame] | 1961 | bind->label, |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 1962 | gsn_addr_to_str(&a->addr)); |
| 1963 | |
| 1964 | return a; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a, |
| 1968 | uint16_t port) |
| 1969 | { |
| 1970 | struct gtphub_peer_port *pp; |
| 1971 | |
| 1972 | pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port); |
| 1973 | OSMO_ASSERT(pp); |
| 1974 | pp->peer_addr = a; |
| 1975 | pp->port = port; |
| 1976 | |
| 1977 | if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) { |
| 1978 | talloc_free(pp); |
| 1979 | return NULL; |
| 1980 | } |
| 1981 | |
| 1982 | llist_add(&pp->entry, &a->ports); |
| 1983 | |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1984 | LOG(LOGL_DEBUG, "New peer port: %s port %d\n", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1985 | gsn_addr_to_str(&a->addr), |
| 1986 | (int)port); |
| 1987 | |
| 1988 | return pp; |
| 1989 | } |
| 1990 | |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 1991 | struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub, |
| 1992 | struct gtphub_bind *bind, |
| 1993 | const struct gsn_addr *addr, |
| 1994 | uint16_t port) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1995 | { |
| 1996 | struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr); |
| 1997 | |
| 1998 | struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port); |
| 1999 | if (pp) |
| 2000 | return pp; |
| 2001 | |
| 2002 | return gtphub_addr_add_port(a, port); |
| 2003 | } |
| 2004 | |
Neels Hofmeyr | c83cd89 | 2015-11-11 14:01:06 +0100 | [diff] [blame] | 2005 | /* Find a GGSN peer with a matching address. If the address is known but the |
| 2006 | * port not, create a new port for that peer address. */ |
| 2007 | struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind, |
| 2008 | const struct osmo_sockaddr *addr) |
| 2009 | { |
| 2010 | struct gtphub_peer_addr *pa; |
| 2011 | struct gtphub_peer_port *pp; |
| 2012 | |
| 2013 | struct gsn_addr gsna; |
| 2014 | uint16_t port; |
| 2015 | gsn_addr_from_sockaddr(&gsna, &port, addr); |
| 2016 | |
| 2017 | pa = gtphub_addr_find(bind, &gsna); |
| 2018 | if (!pa) |
| 2019 | return NULL; |
| 2020 | |
| 2021 | pp = gtphub_addr_find_port(pa, port); |
| 2022 | |
| 2023 | if (!pp) |
| 2024 | pp = gtphub_addr_add_port(pa, port); |
| 2025 | |
| 2026 | return pp; |
| 2027 | } |
| 2028 | |
| 2029 | |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 2030 | /* Return 0 if the message in p is not applicable for GGSN resolution, -1 if |
| 2031 | * resolution should be possible but failed, and 1 if resolution was |
| 2032 | * successful. *pp will be set to NULL if <1 is returned. */ |
| 2033 | static int gtphub_resolve_ggsn(struct gtphub *hub, |
| 2034 | struct gtp_packet_desc *p, |
| 2035 | struct gtphub_peer_port **pp) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2036 | { |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 2037 | *pp = NULL; |
| 2038 | |
| 2039 | /* TODO determine from message type whether IEs should be present? */ |
| 2040 | |
| 2041 | int rc; |
| 2042 | const char *imsi_str; |
| 2043 | rc = get_ie_imsi_str(p->ie, 0, &imsi_str); |
| 2044 | if (rc < 1) |
| 2045 | return rc; |
| 2046 | OSMO_ASSERT(imsi_str); |
| 2047 | |
| 2048 | const char *apn_str; |
| 2049 | rc = get_ie_apn_str(p->ie, &apn_str); |
| 2050 | if (rc < 1) |
| 2051 | return rc; |
| 2052 | OSMO_ASSERT(apn_str); |
| 2053 | |
| 2054 | *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str); |
| 2055 | return (*pp)? 1 : -1; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | |
| 2059 | /* TODO move to osmocom/core/socket.c ? */ |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2060 | /* use this in osmo_sock_init() to remove dup. */ |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 2061 | /* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required |
| 2062 | to call freeaddrinfo(*result), iff zero is returned. */ |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2063 | static int _osmo_getaddrinfo(struct addrinfo **result, |
| 2064 | uint16_t family, uint16_t type, uint8_t proto, |
| 2065 | const char *host, uint16_t port) |
| 2066 | { |
| 2067 | struct addrinfo hints; |
| 2068 | char portbuf[16]; |
| 2069 | |
| 2070 | sprintf(portbuf, "%u", port); |
| 2071 | memset(&hints, '\0', sizeof(struct addrinfo)); |
| 2072 | hints.ai_family = family; |
| 2073 | if (type == SOCK_RAW) { |
| 2074 | /* Workaround for glibc, that returns EAI_SERVICE (-8) if |
| 2075 | * SOCK_RAW and IPPROTO_GRE is used. |
| 2076 | */ |
| 2077 | hints.ai_socktype = SOCK_DGRAM; |
| 2078 | hints.ai_protocol = IPPROTO_UDP; |
| 2079 | } else { |
| 2080 | hints.ai_socktype = type; |
| 2081 | hints.ai_protocol = proto; |
| 2082 | } |
| 2083 | |
| 2084 | return getaddrinfo(host, portbuf, &hints, result); |
| 2085 | } |
| 2086 | |
| 2087 | /* TODO move to osmocom/core/socket.c ? */ |
| 2088 | int osmo_sockaddr_init(struct osmo_sockaddr *addr, |
| 2089 | uint16_t family, uint16_t type, uint8_t proto, |
| 2090 | const char *host, uint16_t port) |
| 2091 | { |
| 2092 | struct addrinfo *res; |
| 2093 | int rc; |
| 2094 | rc = _osmo_getaddrinfo(&res, family, type, proto, host, port); |
| 2095 | |
| 2096 | if (rc != 0) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 2097 | LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2098 | return -EINVAL; |
| 2099 | } |
| 2100 | |
| 2101 | OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a)); |
| 2102 | memcpy(&addr->a, res->ai_addr, res->ai_addrlen); |
| 2103 | addr->l = res->ai_addrlen; |
| 2104 | freeaddrinfo(res); |
| 2105 | |
| 2106 | return 0; |
| 2107 | } |
| 2108 | |
| 2109 | int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len, |
| 2110 | char *port_str, size_t port_str_len, |
| 2111 | const struct osmo_sockaddr *addr, |
| 2112 | int flags) |
| 2113 | { |
| 2114 | int rc; |
| 2115 | |
| 2116 | if ((addr->l < 1) || (addr->l > sizeof(addr->a))) { |
| 2117 | LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l); |
| 2118 | return -1; |
| 2119 | } |
| 2120 | |
| 2121 | if (addr->l > sizeof(addr->a)) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 2122 | LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n", |
| 2123 | addr->l); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2124 | return -1; |
| 2125 | } |
| 2126 | |
| 2127 | rc = getnameinfo((struct sockaddr*)&addr->a, addr->l, |
| 2128 | addr_str, addr_str_len, |
| 2129 | port_str, port_str_len, |
| 2130 | flags); |
| 2131 | |
| 2132 | if (rc) |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 2133 | LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n", |
| 2134 | gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a, |
| 2135 | addr->l)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2136 | |
| 2137 | return rc; |
| 2138 | } |
| 2139 | |
| 2140 | const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr, |
| 2141 | char *buf, size_t buf_len) |
| 2142 | { |
| 2143 | const int portbuf_len = 6; |
| 2144 | OSMO_ASSERT(buf_len > portbuf_len); |
| 2145 | char *portbuf = buf + buf_len - portbuf_len; |
| 2146 | buf_len -= portbuf_len; |
| 2147 | if (osmo_sockaddr_to_strs(buf, buf_len, |
| 2148 | portbuf, portbuf_len, |
| 2149 | addr, |
| 2150 | NI_NUMERICHOST | NI_NUMERICSERV)) |
| 2151 | return NULL; |
| 2152 | |
| 2153 | char *pos = buf + strnlen(buf, buf_len-1); |
| 2154 | size_t len = buf_len - (pos - buf); |
| 2155 | |
| 2156 | snprintf(pos, len, " port %s", portbuf); |
| 2157 | buf[buf_len-1] = '\0'; |
| 2158 | |
| 2159 | return buf; |
| 2160 | } |
| 2161 | |
| 2162 | const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr) |
| 2163 | { |
| 2164 | static char buf[256]; |
| 2165 | const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf)); |
| 2166 | if (! result) |
| 2167 | return "(invalid)"; |
| 2168 | return result; |
| 2169 | } |
| 2170 | |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 2171 | int osmo_sockaddr_cmp(const struct osmo_sockaddr *a, |
| 2172 | const struct osmo_sockaddr *b) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2173 | { |
| 2174 | if (a == b) |
| 2175 | return 0; |
| 2176 | if (!a) |
| 2177 | return -1; |
| 2178 | if (!b) |
| 2179 | return 1; |
| 2180 | if (a->l != b->l) { |
| 2181 | /* Lengths are not the same, but determine the order. Will |
| 2182 | * anyone ever sort a list by osmo_sockaddr though...? */ |
| 2183 | int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l); |
| 2184 | if (cmp == 0) { |
| 2185 | if (a->l < b->l) |
| 2186 | return -1; |
| 2187 | else |
| 2188 | return 1; |
| 2189 | } |
| 2190 | return cmp; |
| 2191 | } |
| 2192 | return memcmp(&a->a, &b->a, a->l); |
| 2193 | } |
| 2194 | |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 2195 | void osmo_sockaddr_copy(struct osmo_sockaddr *dst, |
| 2196 | const struct osmo_sockaddr *src) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2197 | { |
| 2198 | OSMO_ASSERT(src->l <= sizeof(dst->a)); |
| 2199 | memcpy(&dst->a, &src->a, src->l); |
| 2200 | dst->l = src->l; |
| 2201 | } |