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