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