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; |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 89 | uint32_t header_tei_rx; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 90 | uint32_t header_tei; |
| 91 | int rc; /* enum gtp_rc */ |
| 92 | unsigned int plane_idx; |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 93 | unsigned int side_idx; |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 94 | struct gtphub_tunnel *tun; |
Neels Hofmeyr | 29d926b | 2015-11-24 13:27:13 +0100 | [diff] [blame] | 95 | time_t timestamp; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 96 | union gtpie_member *ie[GTPIE_SIZE]; |
| 97 | }; |
| 98 | |
Neels Hofmeyr | 10fc024 | 2015-12-01 00:23:45 +0100 | [diff] [blame] | 99 | struct pending_delete { |
| 100 | struct llist_head entry; |
| 101 | struct expiring_item expiry_entry; |
| 102 | |
| 103 | struct gtphub_tunnel *tun; |
| 104 | uint8_t teardown_ind; |
| 105 | uint8_t nsapi; |
| 106 | }; |
| 107 | |
Neels Hofmeyr | 1ba50c6 | 2015-11-20 01:28:40 +0100 | [diff] [blame] | 108 | |
| 109 | /* counters */ |
| 110 | |
| 111 | enum gtphub_counters_io { |
| 112 | GTPH_CTR_PKTS_IN = 0, |
| 113 | GTPH_CTR_PKTS_OUT, |
| 114 | GTPH_CTR_BYTES_IN, |
| 115 | GTPH_CTR_BYTES_OUT |
| 116 | }; |
| 117 | |
| 118 | static const struct rate_ctr_desc gtphub_counters_io_desc[] = { |
| 119 | { "packets.in", "Packets ( In)" }, |
| 120 | { "packets.out", "Packets (Out)" }, |
Neels Hofmeyr | 4b2cbda | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 121 | { "bytes.in", "Bytes ( In)" }, |
| 122 | { "bytes.out", "Bytes (Out)" }, |
Neels Hofmeyr | 1ba50c6 | 2015-11-20 01:28:40 +0100 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | static const struct rate_ctr_group_desc gtphub_ctrg_io_desc = { |
| 126 | .group_name_prefix = "gtphub.bind", |
Neels Hofmeyr | 4b2cbda | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 127 | .group_description = "I/O Statistics", |
Neels Hofmeyr | 1ba50c6 | 2015-11-20 01:28:40 +0100 | [diff] [blame] | 128 | .num_ctr = ARRAY_SIZE(gtphub_counters_io_desc), |
| 129 | .ctr_desc = gtphub_counters_io_desc, |
| 130 | .class_id = OSMO_STATS_CLASS_GLOBAL, |
| 131 | }; |
| 132 | |
| 133 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 134 | void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src) |
| 135 | { |
Neels Hofmeyr | d8660ef | 2015-12-03 11:24:39 +0100 | [diff] [blame] | 136 | *gsna = *src; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 139 | int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port, |
| 140 | const struct osmo_sockaddr *sa) |
| 141 | { |
| 142 | char addr_str[256]; |
| 143 | char port_str[6]; |
| 144 | |
| 145 | if (osmo_sockaddr_to_strs(addr_str, sizeof(addr_str), |
| 146 | port_str, sizeof(port_str), |
| 147 | sa, (NI_NUMERICHOST | NI_NUMERICSERV)) |
| 148 | != 0) { |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | if (port) |
| 153 | *port = atoi(port_str); |
| 154 | |
| 155 | return gsn_addr_from_str(gsna, addr_str); |
| 156 | } |
| 157 | |
| 158 | int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str) |
| 159 | { |
Neels Hofmeyr | 800126b | 2015-11-30 14:13:19 +0100 | [diff] [blame] | 160 | if ((!gsna) || (!numeric_addr_str)) |
| 161 | return -1; |
| 162 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 163 | int af = AF_INET; |
| 164 | gsna->len = 4; |
| 165 | const char *pos = numeric_addr_str; |
| 166 | for (; *pos; pos++) { |
| 167 | if (*pos == ':') { |
| 168 | af = AF_INET6; |
| 169 | gsna->len = 16; |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | int rc = inet_pton(af, numeric_addr_str, gsna->buf); |
| 175 | if (rc != 1) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 176 | LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n", |
| 177 | numeric_addr_str); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 178 | return -1; |
| 179 | } |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | const char *gsn_addr_to_str(const struct gsn_addr *gsna) |
| 184 | { |
| 185 | static char buf[INET6_ADDRSTRLEN + 1]; |
| 186 | return gsn_addr_to_strb(gsna, buf, sizeof(buf)); |
| 187 | } |
| 188 | |
| 189 | const char *gsn_addr_to_strb(const struct gsn_addr *gsna, |
| 190 | char *strbuf, |
| 191 | int strbuf_len) |
| 192 | { |
| 193 | int af; |
| 194 | switch (gsna->len) { |
| 195 | case 4: |
| 196 | af = AF_INET; |
| 197 | break; |
| 198 | case 16: |
| 199 | af = AF_INET6; |
| 200 | break; |
| 201 | default: |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len); |
| 206 | if (!r) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 207 | LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:" |
| 208 | " %s: len=%d, buf=%s\n", |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 209 | strerror(errno), |
| 210 | (int)gsna->len, |
| 211 | osmo_hexdump(gsna->buf, sizeof(gsna->buf))); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 212 | } |
| 213 | return r; |
| 214 | } |
| 215 | |
| 216 | int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b) |
| 217 | { |
| 218 | if (a == b) |
| 219 | return 1; |
| 220 | if ((!a) || (!b)) |
| 221 | return 0; |
| 222 | if (a->len != b->len) |
| 223 | return 0; |
| 224 | return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0; |
| 225 | } |
| 226 | |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 227 | static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p, |
| 228 | int idx) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 229 | { |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 230 | if (p->rc != GTP_RC_PDU_C) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 231 | return -1; |
| 232 | |
| 233 | unsigned int len; |
| 234 | /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */ |
| 235 | if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx, |
| 236 | &len, gsna->buf, sizeof(gsna->buf)) |
| 237 | != 0) |
| 238 | return -1; |
| 239 | gsna->len = len; |
| 240 | return 0; |
| 241 | } |
| 242 | |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 243 | static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p, |
| 244 | int idx) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 245 | { |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 246 | if (p->rc != GTP_RC_PDU_C) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 247 | return -1; |
| 248 | |
| 249 | int ie_idx; |
| 250 | ie_idx = gtpie_getie(p->ie, GTPIE_GSN_ADDR, idx); |
| 251 | |
| 252 | if (ie_idx < 0) |
| 253 | return -1; |
| 254 | |
| 255 | struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv; |
| 256 | int ie_l = ntoh16(ie->l); |
| 257 | if (ie_l != gsna->len) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 258 | LOG(LOGL_ERROR, "Not implemented:" |
| 259 | " replace an IE address of different size:" |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 260 | " replace %d with %d\n", (int)ie_l, (int)gsna->len); |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | memcpy(ie->v, gsna->buf, (int)ie_l); |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | /* Validate GTP version 0 data; analogous to validate_gtp1_header(), see there. |
| 269 | */ |
| 270 | void validate_gtp0_header(struct gtp_packet_desc *p) |
| 271 | { |
| 272 | const struct gtp0_header *pheader = &(p->data->gtp0.h); |
| 273 | p->rc = GTP_RC_UNKNOWN; |
| 274 | p->header_len = 0; |
| 275 | |
| 276 | OSMO_ASSERT(p->data_len >= 1); |
| 277 | OSMO_ASSERT(p->version == 0); |
| 278 | |
| 279 | if (p->data_len < GTP0_HEADER_SIZE) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 280 | LOG(LOGL_ERROR, "GTP0 packet too short: %d\n", p->data_len); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 281 | p->rc = GTP_RC_TOOSHORT; |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | p->type = ntoh8(pheader->type); |
| 286 | p->seq = ntoh16(pheader->seq); |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 287 | p->header_tei_rx = 0; /* TODO */ |
| 288 | p->header_tei = p->header_tei_rx; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 289 | |
| 290 | if (p->data_len == GTP0_HEADER_SIZE) { |
| 291 | p->rc = GTP_RC_TINY; |
| 292 | p->header_len = GTP0_HEADER_SIZE; |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | /* Check packet length field versus length of packet */ |
| 297 | if (p->data_len != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 298 | LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not" |
| 299 | " match actual length (%d)\n", |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 300 | GTP0_HEADER_SIZE, (int)ntoh16(pheader->length), |
| 301 | p->data_len); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 302 | p->rc = GTP_RC_TOOSHORT; |
| 303 | return; |
| 304 | } |
| 305 | |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 306 | LOG(LOGL_DEBUG, "GTP v0 TID = %" PRIu64 "\n", pheader->tid); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 307 | p->header_len = GTP0_HEADER_SIZE; |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 308 | p->rc = GTP_RC_PDU_C; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* Validate GTP version 1 data, and update p->rc with the result, as well as |
| 312 | * p->header_len in case of a valid header. */ |
| 313 | void validate_gtp1_header(struct gtp_packet_desc *p) |
| 314 | { |
| 315 | const struct gtp1_header_long *pheader = &(p->data->gtp1l.h); |
| 316 | p->rc = GTP_RC_UNKNOWN; |
| 317 | p->header_len = 0; |
| 318 | |
| 319 | OSMO_ASSERT(p->data_len >= 1); |
| 320 | OSMO_ASSERT(p->version == 1); |
| 321 | |
| 322 | if ((p->data_len < GTP1_HEADER_SIZE_LONG) |
| 323 | && (p->data_len != GTP1_HEADER_SIZE_SHORT)){ |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 324 | LOG(LOGL_ERROR, "GTP packet too short: %d\n", p->data_len); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 325 | p->rc = GTP_RC_TOOSHORT; |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | p->type = ntoh8(pheader->type); |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 330 | p->header_tei_rx = ntoh32(pheader->tei); |
| 331 | p->header_tei = p->header_tei_rx; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 332 | p->seq = ntoh16(pheader->seq); |
| 333 | |
Neels Hofmeyr | 28a70f2 | 2015-12-06 15:22:54 +0100 | [diff] [blame] | 334 | LOG(LOGL_DEBUG, "| GTPv1\n"); |
| 335 | LOG(LOGL_DEBUG, "| type = %" PRIu8 " 0x%02" PRIx8 "\n", p->type, p->type); |
| 336 | LOG(LOGL_DEBUG, "| length = %" PRIu16 " 0x%04" PRIx16 "\n", ntoh16(pheader->length), ntoh16(pheader->length)); |
| 337 | LOG(LOGL_DEBUG, "| TEI = %" PRIu32 " 0x%08" PRIx32 "\n", p->header_tei_rx, p->header_tei_rx); |
| 338 | LOG(LOGL_DEBUG, "| seq = %" PRIu16 " 0x%04" PRIx16 "\n", p->seq, p->seq); |
| 339 | LOG(LOGL_DEBUG, "| npdu = %" PRIu8 " 0x%02" PRIx8 "\n", pheader->npdu, pheader->npdu); |
| 340 | LOG(LOGL_DEBUG, "| next = %" PRIu8 " 0x%02" PRIx8 "\n", pheader->next, pheader->next); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 341 | |
| 342 | if (p->data_len <= GTP1_HEADER_SIZE_LONG) { |
| 343 | p->rc = GTP_RC_TINY; |
| 344 | p->header_len = GTP1_HEADER_SIZE_SHORT; |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | /* Check packet length field versus length of packet */ |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 349 | int announced_len = ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT; |
| 350 | if (p->data_len != announced_len) { |
| 351 | LOG(LOGL_ERROR, "GTP packet length field (%d + %d) does not" |
| 352 | " match actual length (%d)\n", |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 353 | GTP1_HEADER_SIZE_SHORT, (int)ntoh16(pheader->length), |
| 354 | p->data_len); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 355 | p->rc = GTP_RC_TOOSHORT; |
| 356 | return; |
| 357 | } |
| 358 | |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 359 | p->rc = GTP_RC_PDU_C; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 360 | p->header_len = GTP1_HEADER_SIZE_LONG; |
| 361 | } |
| 362 | |
| 363 | /* Examine whether p->data of size p->data_len has a valid GTP header. Set |
| 364 | * p->version, p->rc and p->header_len. On error, p->rc <= 0 (see enum |
| 365 | * gtp_rc). p->data must point at a buffer with p->data_len set. */ |
| 366 | void validate_gtp_header(struct gtp_packet_desc *p) |
| 367 | { |
| 368 | p->rc = GTP_RC_UNKNOWN; |
| 369 | |
| 370 | /* Need at least 1 byte in order to check version */ |
| 371 | if (p->data_len < 1) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 372 | LOG(LOGL_ERROR, "Discarding packet - too small: %d\n", |
| 373 | p->data_len); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 374 | p->rc = GTP_RC_TOOSHORT; |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | p->version = p->data->flags >> 5; |
| 379 | |
| 380 | switch (p->version) { |
| 381 | case 0: |
| 382 | validate_gtp0_header(p); |
| 383 | break; |
| 384 | case 1: |
| 385 | validate_gtp1_header(p); |
| 386 | break; |
| 387 | default: |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 388 | LOG(LOGL_ERROR, "Unsupported GTP version: %d\n", p->version); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 389 | p->rc = GTP_RC_UNSUPPORTED_VERSION; |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | |
| 395 | /* Return the value of the i'th IMSI IEI by copying to *imsi. |
| 396 | * The first IEI is reached by passing i = 0. |
| 397 | * imsi must point at allocated space of (at least) 8 bytes. |
| 398 | * Return 1 on success, or 0 if not found. */ |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 399 | 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] | 400 | { |
| 401 | return gtpie_gettv0(ie, GTPIE_IMSI, i, imsi, 8) == 0; |
| 402 | } |
| 403 | |
| 404 | /* 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] | 405 | 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] | 406 | { |
| 407 | return gtpie_gettv1(ie, GTPIE_NSAPI, i, nsapi) == 0; |
| 408 | } |
| 409 | |
| 410 | static char imsi_digit_to_char(uint8_t nibble) |
| 411 | { |
| 412 | nibble &= 0x0f; |
| 413 | if (nibble > 9) |
| 414 | return (nibble == 0x0f) ? '\0' : '?'; |
| 415 | return '0' + nibble; |
| 416 | } |
| 417 | |
| 418 | /* Return a human readable IMSI string, in a static buffer. |
| 419 | * imsi must point at 8 octets of IMSI IE encoded IMSI data. */ |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 420 | static int imsi_to_str(uint8_t *imsi, const char **imsi_str) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 421 | { |
| 422 | static char str[17]; |
| 423 | int i; |
| 424 | |
| 425 | for (i = 0; i < 8; i++) { |
Neels Hofmeyr | 0855008 | 2015-11-30 12:19:50 +0100 | [diff] [blame] | 426 | char c; |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 427 | c = imsi_digit_to_char(imsi[i]); |
| 428 | if (c == '?') |
| 429 | return -1; |
| 430 | str[2*i] = c; |
| 431 | |
| 432 | c = imsi_digit_to_char(imsi[i] >> 4); |
| 433 | if (c == '?') |
| 434 | return -1; |
| 435 | str[2*i + 1] = c; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 436 | } |
| 437 | str[16] = '\0'; |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 438 | *imsi_str = str; |
| 439 | return 1; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [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. */ |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 444 | static int get_ie_imsi_str(union gtpie_member *ie[], int i, |
| 445 | const char **imsi_str) |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 446 | { |
| 447 | uint8_t imsi_buf[8]; |
| 448 | if (!get_ie_imsi(ie, i, imsi_buf)) |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 449 | return 0; |
| 450 | return imsi_to_str(imsi_buf, imsi_str); |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 451 | } |
| 452 | |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 453 | /* Return 0 if not present, 1 if present and decoded successfully, -1 if |
| 454 | * present but cannot be decoded. */ |
| 455 | 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] | 456 | { |
| 457 | static char apn_buf[GSM_APN_LENGTH]; |
| 458 | unsigned int len; |
| 459 | if (gtpie_gettlv(ie, GTPIE_APN, 0, |
| 460 | &len, apn_buf, sizeof(apn_buf)) != 0) |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 461 | return 0; |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 462 | |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 463 | if (len < 2) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 464 | LOG(LOGL_ERROR, "APN IE: invalid length: %d\n", |
| 465 | (int)len); |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 466 | return -1; |
| 467 | } |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 468 | |
| 469 | if (len > (sizeof(apn_buf) - 1)) |
| 470 | len = sizeof(apn_buf) - 1; |
| 471 | apn_buf[len] = '\0'; |
| 472 | |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 473 | *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len); |
| 474 | if (!(*apn_str)) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 475 | LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n", |
| 476 | osmo_hexdump((uint8_t*)apn_buf, len)); |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 477 | return -1; |
| 478 | } |
| 479 | return 1; |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 483 | /* Validate header, and index information elements. Write decoded packet |
| 484 | * information to *res. res->data will point at the given data buffer. On |
| 485 | * error, p->rc is set <= 0 (see enum gtp_rc). */ |
| 486 | static void gtp_decode(const uint8_t *data, int data_len, |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 487 | unsigned int from_side_idx, |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 488 | unsigned int from_plane_idx, |
Neels Hofmeyr | 29d926b | 2015-11-24 13:27:13 +0100 | [diff] [blame] | 489 | struct gtp_packet_desc *res, |
| 490 | time_t now) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 491 | { |
| 492 | ZERO_STRUCT(res); |
| 493 | res->data = (union gtp_packet*)data; |
| 494 | res->data_len = data_len; |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 495 | res->side_idx = from_side_idx; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 496 | res->plane_idx = from_plane_idx; |
Neels Hofmeyr | 29d926b | 2015-11-24 13:27:13 +0100 | [diff] [blame] | 497 | res->timestamp = now; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 498 | |
| 499 | validate_gtp_header(res); |
| 500 | |
Neels Hofmeyr | 87c83d0 | 2015-12-03 11:25:27 +0100 | [diff] [blame] | 501 | if (res->rc <= 0) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 502 | return; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 503 | |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 504 | LOG(LOGL_DEBUG, "Valid GTP header (v%d)\n", res->version); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 505 | |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 506 | if (from_plane_idx == GTPH_PLANE_USER) { |
| 507 | res->rc = GTP_RC_PDU_U; |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | if (res->rc != GTP_RC_PDU_C) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 512 | LOG(LOGL_DEBUG, "no IEs in this GTP packet\n"); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 513 | return; |
| 514 | } |
| 515 | |
| 516 | if (gtpie_decaps(res->ie, res->version, |
| 517 | (void*)(data + res->header_len), |
| 518 | res->data_len - res->header_len) != 0) { |
| 519 | res->rc = GTP_RC_INVALID_IE; |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 520 | LOG(LOGL_ERROR, "INVALID: cannot decode IEs." |
| 521 | " Dropping GTP packet.\n"); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 522 | return; |
| 523 | } |
| 524 | |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 525 | #if 1 |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 526 | /* TODO if (<loglevel is debug>) { ... |
| 527 | (waiting for a commit from jerlbeck) */ |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 528 | int i; |
| 529 | |
| 530 | for (i = 0; i < 10; i++) { |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 531 | const char *imsi; |
| 532 | if (get_ie_imsi_str(res->ie, i, &imsi) < 1) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 533 | break; |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 534 | LOG(LOGL_DEBUG, "| IMSI %s\n", imsi); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | for (i = 0; i < 10; i++) { |
| 538 | uint8_t nsapi; |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 539 | if (!get_ie_nsapi(res->ie, i, &nsapi)) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 540 | break; |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 541 | LOG(LOGL_DEBUG, "| NSAPI %d\n", (int)nsapi); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | for (i = 0; i < 2; i++) { |
| 545 | struct gsn_addr addr; |
| 546 | if (gsn_addr_get(&addr, res, i) == 0) |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 547 | LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | for (i = 0; i < 10; i++) { |
| 551 | uint32_t tei; |
| 552 | if (gtpie_gettv4(res->ie, GTPIE_TEI_DI, i, &tei) != 0) |
| 553 | break; |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 554 | LOG(LOGL_DEBUG, "| TEI DI (USER) %" PRIu32 " 0x%08" PRIx32 "\n", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 555 | tei, tei); |
| 556 | } |
| 557 | |
| 558 | for (i = 0; i < 10; i++) { |
| 559 | uint32_t tei; |
| 560 | if (gtpie_gettv4(res->ie, GTPIE_TEI_C, i, &tei) != 0) |
| 561 | break; |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 562 | LOG(LOGL_DEBUG, "| TEI (CTRL) %" PRIu32 " 0x%08" PRIx32 "\n", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 563 | tei, tei); |
| 564 | } |
| 565 | #endif |
| 566 | } |
| 567 | |
| 568 | |
| 569 | /* expiry */ |
| 570 | |
| 571 | void expiry_init(struct expiry *exq, int expiry_in_seconds) |
| 572 | { |
| 573 | ZERO_STRUCT(exq); |
| 574 | exq->expiry_in_seconds = expiry_in_seconds; |
| 575 | INIT_LLIST_HEAD(&exq->items); |
| 576 | } |
| 577 | |
| 578 | void expiry_add(struct expiry *exq, struct expiring_item *item, time_t now) |
| 579 | { |
| 580 | item->expiry = now + exq->expiry_in_seconds; |
| 581 | |
Neels Hofmeyr | 1aa0e47 | 2015-11-24 13:32:23 +0100 | [diff] [blame] | 582 | OSMO_ASSERT(llist_empty(&exq->items) |
| 583 | || (item->expiry |
| 584 | >= llist_last(&exq->items, struct expiring_item, entry)->expiry)); |
| 585 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 586 | /* Add/move to the tail to always sort by expiry, ascending. */ |
| 587 | llist_del(&item->entry); |
| 588 | llist_add_tail(&item->entry, &exq->items); |
| 589 | } |
| 590 | |
| 591 | int expiry_tick(struct expiry *exq, time_t now) |
| 592 | { |
| 593 | int expired = 0; |
| 594 | struct expiring_item *m, *n; |
| 595 | llist_for_each_entry_safe(m, n, &exq->items, entry) { |
| 596 | if (m->expiry <= now) { |
| 597 | expiring_item_del(m); |
| 598 | expired ++; |
| 599 | } else { |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 600 | /* The items are added sorted by expiry. So when we hit |
| 601 | * an unexpired entry, only more unexpired ones will |
| 602 | * follow. */ |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | return expired; |
| 607 | } |
| 608 | |
Neels Hofmeyr | 20bd6bf | 2015-11-20 00:08:28 +0100 | [diff] [blame] | 609 | void expiry_clear(struct expiry *exq) |
| 610 | { |
| 611 | struct expiring_item *m, *n; |
| 612 | llist_for_each_entry_safe(m, n, &exq->items, entry) { |
| 613 | expiring_item_del(m); |
| 614 | } |
| 615 | } |
| 616 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 617 | void expiring_item_init(struct expiring_item *item) |
| 618 | { |
| 619 | ZERO_STRUCT(item); |
| 620 | INIT_LLIST_HEAD(&item->entry); |
| 621 | } |
| 622 | |
| 623 | void expiring_item_del(struct expiring_item *item) |
| 624 | { |
| 625 | OSMO_ASSERT(item); |
| 626 | llist_del(&item->entry); |
| 627 | INIT_LLIST_HEAD(&item->entry); |
| 628 | if (item->del_cb) { |
| 629 | /* avoid loops */ |
| 630 | del_cb_t del_cb = item->del_cb; |
| 631 | item->del_cb = 0; |
| 632 | (del_cb)(item); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | |
| 637 | /* nr_map, nr_pool */ |
| 638 | |
Neels Hofmeyr | e2ed8e6 | 2015-11-17 14:30:37 +0100 | [diff] [blame] | 639 | 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] | 640 | { |
Neels Hofmeyr | e2ed8e6 | 2015-11-17 14:30:37 +0100 | [diff] [blame] | 641 | *pool = (struct nr_pool){ |
| 642 | .nr_min = nr_min, |
| 643 | .nr_max = nr_max, |
| 644 | .last_nr = nr_max |
| 645 | }; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | nr_t nr_pool_next(struct nr_pool *pool) |
| 649 | { |
Neels Hofmeyr | e2ed8e6 | 2015-11-17 14:30:37 +0100 | [diff] [blame] | 650 | if (pool->last_nr >= pool->nr_max) |
| 651 | pool->last_nr = pool->nr_min; |
| 652 | else |
| 653 | pool->last_nr ++; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 654 | |
| 655 | return pool->last_nr; |
| 656 | } |
| 657 | |
| 658 | void nr_map_init(struct nr_map *map, struct nr_pool *pool, |
| 659 | struct expiry *exq) |
| 660 | { |
| 661 | ZERO_STRUCT(map); |
| 662 | map->pool = pool; |
| 663 | map->add_items_to_expiry = exq; |
| 664 | INIT_LLIST_HEAD(&map->mappings); |
| 665 | } |
| 666 | |
| 667 | void nr_mapping_init(struct nr_mapping *m) |
| 668 | { |
| 669 | ZERO_STRUCT(m); |
| 670 | INIT_LLIST_HEAD(&m->entry); |
| 671 | expiring_item_init(&m->expiry_entry); |
| 672 | } |
| 673 | |
| 674 | void nr_map_add(struct nr_map *map, struct nr_mapping *mapping, time_t now) |
| 675 | { |
| 676 | /* Generate a mapped number */ |
| 677 | mapping->repl = nr_pool_next(map->pool); |
| 678 | |
| 679 | /* Add to the tail to always yield a list sorted by expiry, in |
| 680 | * ascending order. */ |
| 681 | llist_add_tail(&mapping->entry, &map->mappings); |
Neels Hofmeyr | 508514c | 2015-11-24 13:30:38 +0100 | [diff] [blame] | 682 | nr_map_refresh(map, mapping, now); |
| 683 | } |
| 684 | |
| 685 | void nr_map_refresh(struct nr_map *map, struct nr_mapping *mapping, time_t now) |
| 686 | { |
| 687 | if (!map->add_items_to_expiry) |
| 688 | return; |
| 689 | expiry_add(map->add_items_to_expiry, |
| 690 | &mapping->expiry_entry, |
| 691 | now); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | void nr_map_clear(struct nr_map *map) |
| 695 | { |
| 696 | struct nr_mapping *m; |
| 697 | struct nr_mapping *n; |
| 698 | llist_for_each_entry_safe(m, n, &map->mappings, entry) { |
| 699 | nr_mapping_del(m); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | int nr_map_empty(const struct nr_map *map) |
| 704 | { |
| 705 | return llist_empty(&map->mappings); |
| 706 | } |
| 707 | |
| 708 | struct nr_mapping *nr_map_get(const struct nr_map *map, |
| 709 | void *origin, nr_t nr_orig) |
| 710 | { |
| 711 | struct nr_mapping *mapping; |
| 712 | llist_for_each_entry(mapping, &map->mappings, entry) { |
| 713 | if ((mapping->origin == origin) |
| 714 | && (mapping->orig == nr_orig)) |
| 715 | return mapping; |
| 716 | } |
| 717 | /* Not found. */ |
| 718 | return NULL; |
| 719 | } |
| 720 | |
| 721 | struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl) |
| 722 | { |
| 723 | struct nr_mapping *mapping; |
| 724 | llist_for_each_entry(mapping, &map->mappings, entry) { |
| 725 | if (mapping->repl == nr_repl) { |
| 726 | return mapping; |
| 727 | } |
| 728 | } |
| 729 | /* Not found. */ |
| 730 | return NULL; |
| 731 | } |
| 732 | |
| 733 | void nr_mapping_del(struct nr_mapping *mapping) |
| 734 | { |
| 735 | OSMO_ASSERT(mapping); |
| 736 | llist_del(&mapping->entry); |
| 737 | INIT_LLIST_HEAD(&mapping->entry); |
| 738 | expiring_item_del(&mapping->expiry_entry); |
| 739 | } |
| 740 | |
| 741 | |
| 742 | /* gtphub */ |
| 743 | |
| 744 | const char* const gtphub_plane_idx_names[GTPH_PLANE_N] = { |
| 745 | "CTRL", |
| 746 | "USER", |
| 747 | }; |
| 748 | |
| 749 | const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N] = { |
| 750 | 2123, |
| 751 | 2152, |
| 752 | }; |
| 753 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 754 | const char* const gtphub_side_idx_names[GTPH_SIDE_N] = { |
| 755 | "SGSN", |
| 756 | "GGSN", |
| 757 | }; |
| 758 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 759 | time_t gtphub_now(void) |
| 760 | { |
| 761 | struct timespec now_tp; |
| 762 | OSMO_ASSERT(clock_gettime(CLOCK_MONOTONIC, &now_tp) >= 0); |
| 763 | return now_tp.tv_sec; |
| 764 | } |
| 765 | |
| 766 | /* Remove a gtphub_peer from its list and free it. */ |
| 767 | static void gtphub_peer_del(struct gtphub_peer *peer) |
| 768 | { |
Neels Hofmeyr | 1ed9a86 | 2015-11-20 00:04:41 +0100 | [diff] [blame] | 769 | OSMO_ASSERT(llist_empty(&peer->addresses)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 770 | nr_map_clear(&peer->seq_map); |
| 771 | llist_del(&peer->entry); |
| 772 | talloc_free(peer); |
| 773 | } |
| 774 | |
| 775 | static void gtphub_peer_addr_del(struct gtphub_peer_addr *pa) |
| 776 | { |
| 777 | OSMO_ASSERT(llist_empty(&pa->ports)); |
| 778 | llist_del(&pa->entry); |
| 779 | talloc_free(pa); |
| 780 | } |
| 781 | |
| 782 | static void gtphub_peer_port_del(struct gtphub_peer_port *pp) |
| 783 | { |
| 784 | OSMO_ASSERT(pp->ref_count == 0); |
| 785 | llist_del(&pp->entry); |
Neels Hofmeyr | e38fb66 | 2015-12-06 16:44:14 +0100 | [diff] [blame] | 786 | rate_ctr_group_free(pp->counters_io); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 787 | talloc_free(pp); |
| 788 | } |
| 789 | |
| 790 | /* From the information in the gtp_packet_desc, return the address of a GGSN. |
| 791 | * Return -1 on error. */ |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 792 | static int gtphub_resolve_ggsn(struct gtphub *hub, |
| 793 | struct gtp_packet_desc *p, |
| 794 | struct gtphub_peer_port **pp); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 795 | |
| 796 | /* See gtphub_ext.c (wrapped by unit test) */ |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 797 | struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub, |
| 798 | const char *imsi_str, |
| 799 | const char *apn_ni_str); |
| 800 | int gtphub_ares_init(struct gtphub *hub); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 801 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 802 | static void gtphub_zero(struct gtphub *hub) |
| 803 | { |
| 804 | ZERO_STRUCT(hub); |
Neels Hofmeyr | 16c3f57 | 2015-11-11 17:27:01 +0100 | [diff] [blame] | 805 | INIT_LLIST_HEAD(&hub->ggsn_lookups); |
| 806 | INIT_LLIST_HEAD(&hub->resolved_ggsns); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | static int gtphub_sock_init(struct osmo_fd *ofd, |
| 810 | const struct gtphub_cfg_addr *addr, |
| 811 | osmo_fd_cb_t cb, |
| 812 | void *data, |
| 813 | int ofd_id) |
| 814 | { |
| 815 | if (!addr->addr_str) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 816 | LOG(LOGL_FATAL, "Cannot bind: empty address.\n"); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 817 | return -1; |
| 818 | } |
| 819 | if (!addr->port) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 820 | LOG(LOGL_FATAL, "Cannot bind: zero port not permitted.\n"); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 821 | return -1; |
| 822 | } |
| 823 | |
| 824 | ofd->when = BSC_FD_READ; |
| 825 | ofd->cb = cb; |
| 826 | ofd->data = data; |
| 827 | ofd->priv_nr = ofd_id; |
| 828 | |
| 829 | int rc; |
| 830 | rc = osmo_sock_init_ofd(ofd, |
| 831 | AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, |
| 832 | addr->addr_str, addr->port, |
| 833 | OSMO_SOCK_F_BIND); |
| 834 | if (rc < 1) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 835 | LOG(LOGL_FATAL, "Cannot bind to %s port %d (rc %d)\n", |
| 836 | addr->addr_str, (int)addr->port, rc); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 837 | return -1; |
| 838 | } |
| 839 | |
| 840 | return 0; |
| 841 | } |
| 842 | |
Neels Hofmeyr | 20bd6bf | 2015-11-20 00:08:28 +0100 | [diff] [blame] | 843 | static void gtphub_sock_close(struct osmo_fd *ofd) |
| 844 | { |
| 845 | close(ofd->fd); |
| 846 | osmo_fd_unregister(ofd); |
| 847 | ofd->cb = NULL; |
| 848 | } |
| 849 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 850 | static void gtphub_bind_init(struct gtphub_bind *b) |
| 851 | { |
| 852 | ZERO_STRUCT(b); |
| 853 | |
| 854 | INIT_LLIST_HEAD(&b->peers); |
Neels Hofmeyr | 1ba50c6 | 2015-11-20 01:28:40 +0100 | [diff] [blame] | 855 | |
| 856 | b->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx, |
| 857 | >phub_ctrg_io_desc, 0); |
| 858 | OSMO_ASSERT(b->counters_io); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | static int gtphub_bind_start(struct gtphub_bind *b, |
| 862 | const struct gtphub_cfg_bind *cfg, |
| 863 | osmo_fd_cb_t cb, void *cb_data, |
| 864 | unsigned int ofd_id) |
| 865 | { |
Neels Hofmeyr | 800126b | 2015-11-30 14:13:19 +0100 | [diff] [blame] | 866 | LOG(LOGL_DEBUG, "Starting bind %s\n", b->label); |
| 867 | if (gsn_addr_from_str(&b->local_addr, cfg->bind.addr_str) != 0) { |
| 868 | LOG(LOGL_FATAL, "Invalid bind address for %s: %s\n", |
| 869 | b->label, cfg->bind.addr_str); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 870 | return -1; |
Neels Hofmeyr | 800126b | 2015-11-30 14:13:19 +0100 | [diff] [blame] | 871 | } |
| 872 | if (gtphub_sock_init(&b->ofd, &cfg->bind, cb, cb_data, ofd_id) != 0) { |
| 873 | LOG(LOGL_FATAL, "Cannot bind for %s: %s\n", |
| 874 | b->label, cfg->bind.addr_str); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 875 | return -1; |
Neels Hofmeyr | 800126b | 2015-11-30 14:13:19 +0100 | [diff] [blame] | 876 | } |
Neels Hofmeyr | 4b2cbda | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 877 | b->local_port = cfg->bind.port; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 878 | return 0; |
| 879 | } |
| 880 | |
Neels Hofmeyr | 20bd6bf | 2015-11-20 00:08:28 +0100 | [diff] [blame] | 881 | static void gtphub_bind_free(struct gtphub_bind *b) |
| 882 | { |
| 883 | OSMO_ASSERT(llist_empty(&b->peers)); |
Neels Hofmeyr | 1ba50c6 | 2015-11-20 01:28:40 +0100 | [diff] [blame] | 884 | rate_ctr_group_free(b->counters_io); |
Neels Hofmeyr | 20bd6bf | 2015-11-20 00:08:28 +0100 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | static void gtphub_bind_stop(struct gtphub_bind *b) { |
| 888 | gtphub_sock_close(&b->ofd); |
| 889 | gtphub_bind_free(b); |
| 890 | } |
| 891 | |
Neels Hofmeyr | 4034897 | 2015-11-17 12:22:28 +0100 | [diff] [blame] | 892 | /* Recv datagram from from->fd, write sender's address to *from_addr. |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 893 | * Return the number of bytes read, zero on error. */ |
| 894 | static int gtphub_read(const struct osmo_fd *from, |
| 895 | struct osmo_sockaddr *from_addr, |
| 896 | uint8_t *buf, size_t buf_len) |
| 897 | { |
Neels Hofmeyr | 4034897 | 2015-11-17 12:22:28 +0100 | [diff] [blame] | 898 | OSMO_ASSERT(from_addr); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 899 | |
Neels Hofmeyr | 4034897 | 2015-11-17 12:22:28 +0100 | [diff] [blame] | 900 | /* recvfrom requires the available length set in *from_addr_len. */ |
| 901 | from_addr->l = sizeof(from_addr->a); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 902 | errno = 0; |
| 903 | ssize_t received = recvfrom(from->fd, buf, buf_len, 0, |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 904 | (struct sockaddr*)&from_addr->a, |
| 905 | &from_addr->l); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 906 | /* TODO use recvmsg and get a MSG_TRUNC flag to make sure the message |
| 907 | * is not truncated. Then maybe reduce buf's size. */ |
| 908 | |
| 909 | if (received <= 0) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 910 | LOG((errno == EAGAIN? LOGL_DEBUG : LOGL_ERROR), |
| 911 | "error: %s\n", strerror(errno)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 912 | return 0; |
| 913 | } |
| 914 | |
Neels Hofmeyr | 28a70f2 | 2015-12-06 15:22:54 +0100 | [diff] [blame] | 915 | LOG(LOGL_DEBUG, "Received %d bytes from %s: %s\n", |
Neels Hofmeyr | 4034897 | 2015-11-17 12:22:28 +0100 | [diff] [blame] | 916 | (int)received, osmo_sockaddr_to_str(from_addr), |
| 917 | osmo_hexdump(buf, received)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 918 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 919 | return received; |
| 920 | } |
| 921 | |
| 922 | inline void gtphub_port_ref_count_inc(struct gtphub_peer_port *pp) |
| 923 | { |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 924 | OSMO_ASSERT(pp); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 925 | OSMO_ASSERT(pp->ref_count < UINT_MAX); |
| 926 | pp->ref_count++; |
| 927 | } |
| 928 | |
| 929 | inline void gtphub_port_ref_count_dec(struct gtphub_peer_port *pp) |
| 930 | { |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 931 | OSMO_ASSERT(pp); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 932 | OSMO_ASSERT(pp->ref_count > 0); |
| 933 | pp->ref_count--; |
| 934 | } |
| 935 | |
| 936 | inline void set_seq(struct gtp_packet_desc *p, uint16_t seq) |
| 937 | { |
| 938 | OSMO_ASSERT(p->version == 1); |
| 939 | p->data->gtp1l.h.seq = hton16(seq); |
| 940 | p->seq = seq; |
| 941 | } |
| 942 | |
| 943 | inline void set_tei(struct gtp_packet_desc *p, uint32_t tei) |
| 944 | { |
| 945 | OSMO_ASSERT(p->version == 1); |
| 946 | p->data->gtp1l.h.tei = hton32(tei); |
| 947 | p->header_tei = tei; |
| 948 | } |
| 949 | |
| 950 | static void gtphub_mapping_del_cb(struct expiring_item *expi); |
| 951 | |
| 952 | static struct nr_mapping *gtphub_mapping_new() |
| 953 | { |
| 954 | struct nr_mapping *nrm; |
| 955 | nrm = talloc_zero(osmo_gtphub_ctx, struct nr_mapping); |
| 956 | OSMO_ASSERT(nrm); |
| 957 | |
| 958 | nr_mapping_init(nrm); |
| 959 | nrm->expiry_entry.del_cb = gtphub_mapping_del_cb; |
| 960 | return nrm; |
| 961 | } |
| 962 | |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 963 | |
| 964 | #define APPEND(args...) \ |
| 965 | l = snprintf(pos, left, args); \ |
| 966 | pos += l; \ |
| 967 | left -= l |
| 968 | |
| 969 | static const char *gtphub_tunnel_side_str(struct gtphub_tunnel *tun, |
| 970 | int side_idx) |
| 971 | { |
| 972 | static char buf[256]; |
| 973 | char *pos = buf; |
| 974 | int left = sizeof(buf); |
| 975 | int l; |
| 976 | |
| 977 | struct gtphub_tunnel_endpoint *c, *u; |
| 978 | c = &tun->endpoint[side_idx][GTPH_PLANE_CTRL]; |
| 979 | u = &tun->endpoint[side_idx][GTPH_PLANE_USER]; |
| 980 | |
| 981 | /* print both only if they differ. */ |
| 982 | if (!c->peer) { |
| 983 | APPEND("(uninitialized)"); |
| 984 | } else { |
| 985 | APPEND("%s", gsn_addr_to_str(&c->peer->peer_addr->addr)); |
| 986 | } |
| 987 | |
| 988 | if (!u->peer) { |
| 989 | if (c->peer) { |
Neels Hofmeyr | ba0525e | 2015-12-06 16:39:23 +0100 | [diff] [blame] | 990 | APPEND("/(uninitialized)"); |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 991 | } |
| 992 | } else if ((!c->peer) |
| 993 | || (!gsn_addr_same(&u->peer->peer_addr->addr, |
| 994 | &c->peer->peer_addr->addr))) { |
Neels Hofmeyr | ba0525e | 2015-12-06 16:39:23 +0100 | [diff] [blame] | 995 | APPEND("/%s", gsn_addr_to_str(&u->peer->peer_addr->addr)); |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 996 | } |
| 997 | |
Neels Hofmeyr | ba0525e | 2015-12-06 16:39:23 +0100 | [diff] [blame] | 998 | APPEND(" (TEI C %x=%x/U %x=%x)", |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 999 | c->tei_orig, c->tei_repl, |
| 1000 | u->tei_orig, u->tei_repl); |
| 1001 | return buf; |
| 1002 | } |
| 1003 | |
| 1004 | const char *gtphub_tunnel_str(struct gtphub_tunnel *tun) |
| 1005 | { |
| 1006 | static char buf[512]; |
| 1007 | char *pos = buf; |
| 1008 | int left = sizeof(buf); |
| 1009 | int l; |
| 1010 | |
| 1011 | APPEND("%s", gtphub_tunnel_side_str(tun, GTPH_SIDE_SGSN)); |
| 1012 | APPEND(" <-> %s", gtphub_tunnel_side_str(tun, GTPH_SIDE_GGSN)); |
| 1013 | |
| 1014 | return buf; |
| 1015 | } |
| 1016 | |
| 1017 | #undef APPEND |
| 1018 | |
| 1019 | void gtphub_tunnel_endpoint_set_peer(struct gtphub_tunnel_endpoint *te, |
| 1020 | struct gtphub_peer_port *pp) |
| 1021 | { |
| 1022 | if (te->peer) |
| 1023 | gtphub_port_ref_count_dec(te->peer); |
| 1024 | te->peer = pp; |
| 1025 | if (te->peer) |
| 1026 | gtphub_port_ref_count_inc(te->peer); |
| 1027 | } |
| 1028 | |
| 1029 | int gtphub_tunnel_complete(struct gtphub_tunnel *tun) |
| 1030 | { |
| 1031 | if (!tun) |
| 1032 | return 0; |
| 1033 | int side_idx; |
| 1034 | int plane_idx; |
Neels Hofmeyr | f977320 | 2015-11-27 00:05:56 +0100 | [diff] [blame] | 1035 | for_each_side_and_plane(side_idx, plane_idx) { |
| 1036 | struct gtphub_tunnel_endpoint *te = |
| 1037 | &tun->endpoint[side_idx][plane_idx]; |
| 1038 | if (!(te->peer && te->tei_orig && te->tei_repl)) |
| 1039 | return 0; |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1040 | } |
| 1041 | return 1; |
| 1042 | } |
| 1043 | |
| 1044 | static void gtphub_tunnel_del_cb(struct expiring_item *expi) |
| 1045 | { |
| 1046 | struct gtphub_tunnel *tun = container_of(expi, |
| 1047 | struct gtphub_tunnel, |
| 1048 | expiry_entry); |
| 1049 | LOG(LOGL_DEBUG, "expired: %s\n", gtphub_tunnel_str(tun)); |
| 1050 | |
| 1051 | llist_del(&tun->entry); |
| 1052 | INIT_LLIST_HEAD(&tun->entry); /* mark unused */ |
| 1053 | |
| 1054 | expi->del_cb = 0; /* avoid recursion loops */ |
| 1055 | expiring_item_del(&tun->expiry_entry); /* usually already done, but make sure. */ |
| 1056 | |
| 1057 | int side_idx; |
| 1058 | int plane_idx; |
Neels Hofmeyr | f977320 | 2015-11-27 00:05:56 +0100 | [diff] [blame] | 1059 | for_each_side_and_plane(side_idx, plane_idx) { |
Neels Hofmeyr | e38fb66 | 2015-12-06 16:44:14 +0100 | [diff] [blame] | 1060 | struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][plane_idx]; |
| 1061 | |
Neels Hofmeyr | f977320 | 2015-11-27 00:05:56 +0100 | [diff] [blame] | 1062 | /* clear ref count */ |
Neels Hofmeyr | e38fb66 | 2015-12-06 16:44:14 +0100 | [diff] [blame] | 1063 | gtphub_tunnel_endpoint_set_peer(te, NULL); |
| 1064 | |
| 1065 | rate_ctr_group_free(te->counters_io); |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | talloc_free(tun); |
| 1069 | } |
| 1070 | |
| 1071 | static struct gtphub_tunnel *gtphub_tunnel_new() |
| 1072 | { |
| 1073 | struct gtphub_tunnel *tun; |
| 1074 | tun = talloc_zero(osmo_gtphub_ctx, struct gtphub_tunnel); |
| 1075 | OSMO_ASSERT(tun); |
| 1076 | |
| 1077 | INIT_LLIST_HEAD(&tun->entry); |
| 1078 | expiring_item_init(&tun->expiry_entry); |
| 1079 | |
Neels Hofmeyr | e38fb66 | 2015-12-06 16:44:14 +0100 | [diff] [blame] | 1080 | int side_idx, plane_idx; |
| 1081 | for_each_side_and_plane(side_idx, plane_idx) { |
| 1082 | struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][plane_idx]; |
| 1083 | te->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx, |
| 1084 | >phub_ctrg_io_desc, |
| 1085 | 0); |
| 1086 | OSMO_ASSERT(te->counters_io); |
| 1087 | } |
| 1088 | |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1089 | tun->expiry_entry.del_cb = gtphub_tunnel_del_cb; |
| 1090 | return tun; |
| 1091 | } |
| 1092 | |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1093 | static const char *gtphub_peer_strb(struct gtphub_peer *peer, char *buf, |
| 1094 | int buflen) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1095 | { |
| 1096 | if (llist_empty(&peer->addresses)) |
| 1097 | return "(addressless)"; |
| 1098 | |
| 1099 | struct gtphub_peer_addr *a = llist_first(&peer->addresses, |
| 1100 | struct gtphub_peer_addr, |
| 1101 | entry); |
| 1102 | return gsn_addr_to_strb(&a->addr, buf, buflen); |
| 1103 | } |
| 1104 | |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1105 | static const char *gtphub_port_strb(struct gtphub_peer_port *port, char *buf, |
| 1106 | int buflen) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1107 | { |
| 1108 | if (!port) |
| 1109 | return "(null port)"; |
| 1110 | |
| 1111 | snprintf(buf, buflen, "%s port %d", |
| 1112 | gsn_addr_to_str(&port->peer_addr->addr), |
| 1113 | (int)port->port); |
| 1114 | return buf; |
| 1115 | } |
| 1116 | |
| 1117 | const char *gtphub_peer_str(struct gtphub_peer *peer) |
| 1118 | { |
| 1119 | static char buf[256]; |
| 1120 | return gtphub_peer_strb(peer, buf, sizeof(buf)); |
| 1121 | } |
| 1122 | |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 1123 | const char *gtphub_port_str(struct gtphub_peer_port *port) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1124 | { |
| 1125 | static char buf[256]; |
| 1126 | return gtphub_port_strb(port, buf, sizeof(buf)); |
| 1127 | } |
| 1128 | |
| 1129 | static const char *gtphub_port_str2(struct gtphub_peer_port *port) |
| 1130 | { |
| 1131 | static char buf[256]; |
| 1132 | return gtphub_port_strb(port, buf, sizeof(buf)); |
| 1133 | } |
| 1134 | |
| 1135 | static void gtphub_mapping_del_cb(struct expiring_item *expi) |
| 1136 | { |
| 1137 | expi->del_cb = 0; /* avoid recursion loops */ |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1138 | expiring_item_del(expi); /* usually already done, but make sure. */ |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1139 | |
| 1140 | struct nr_mapping *nrm = container_of(expi, |
| 1141 | struct nr_mapping, |
| 1142 | expiry_entry); |
| 1143 | llist_del(&nrm->entry); |
| 1144 | INIT_LLIST_HEAD(&nrm->entry); /* mark unused */ |
| 1145 | |
| 1146 | /* Just for log */ |
| 1147 | struct gtphub_peer_port *from = nrm->origin; |
| 1148 | OSMO_ASSERT(from); |
Neels Hofmeyr | 334af5d | 2015-11-17 14:24:46 +0100 | [diff] [blame] | 1149 | LOG(LOGL_DEBUG, "expired: %d: nr mapping from %s: %u->%u\n", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1150 | (int)nrm->expiry_entry.expiry, |
| 1151 | gtphub_port_str(from), |
Neels Hofmeyr | 334af5d | 2015-11-17 14:24:46 +0100 | [diff] [blame] | 1152 | (unsigned int)nrm->orig, (unsigned int)nrm->repl); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1153 | |
| 1154 | gtphub_port_ref_count_dec(from); |
| 1155 | |
| 1156 | talloc_free(nrm); |
| 1157 | } |
| 1158 | |
| 1159 | static struct nr_mapping *gtphub_mapping_have(struct nr_map *map, |
| 1160 | struct gtphub_peer_port *from, |
| 1161 | nr_t orig_nr, |
| 1162 | time_t now) |
| 1163 | { |
| 1164 | struct nr_mapping *nrm; |
| 1165 | |
| 1166 | nrm = nr_map_get(map, from, orig_nr); |
| 1167 | |
| 1168 | if (!nrm) { |
| 1169 | nrm = gtphub_mapping_new(); |
| 1170 | nrm->orig = orig_nr; |
| 1171 | nrm->origin = from; |
| 1172 | nr_map_add(map, nrm, now); |
| 1173 | gtphub_port_ref_count_inc(from); |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1174 | LOG(LOGL_DEBUG, "peer %s: sequence map %d --> %d\n", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1175 | gtphub_port_str(from), |
| 1176 | (int)(nrm->orig), (int)(nrm->repl)); |
| 1177 | } else { |
Neels Hofmeyr | 508514c | 2015-11-24 13:30:38 +0100 | [diff] [blame] | 1178 | nr_map_refresh(map, nrm, now); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | OSMO_ASSERT(nrm); |
| 1182 | return nrm; |
| 1183 | } |
| 1184 | |
Neels Hofmeyr | 3317c84 | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 1185 | static void gtphub_map_seq(struct gtp_packet_desc *p, |
| 1186 | struct gtphub_peer_port *from_port, |
Neels Hofmeyr | 29d926b | 2015-11-24 13:27:13 +0100 | [diff] [blame] | 1187 | struct gtphub_peer_port *to_port) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1188 | { |
| 1189 | /* Store a mapping in to_peer's map, so when we later receive a GTP |
| 1190 | * packet back from to_peer, the seq nr can be unmapped back to its |
| 1191 | * origin (from_peer here). */ |
| 1192 | struct nr_mapping *nrm; |
| 1193 | nrm = gtphub_mapping_have(&to_port->peer_addr->peer->seq_map, |
Neels Hofmeyr | 29d926b | 2015-11-24 13:27:13 +0100 | [diff] [blame] | 1194 | from_port, p->seq, p->timestamp); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1195 | |
| 1196 | /* Change the GTP packet to yield the new, mapped seq nr */ |
| 1197 | set_seq(p, nrm->repl); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | static struct gtphub_peer_port *gtphub_unmap_seq(struct gtp_packet_desc *p, |
| 1201 | struct gtphub_peer_port *responding_port) |
| 1202 | { |
| 1203 | OSMO_ASSERT(p->version == 1); |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1204 | struct nr_mapping *nrm = |
| 1205 | nr_map_get_inv(&responding_port->peer_addr->peer->seq_map, |
| 1206 | p->seq); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1207 | if (!nrm) |
| 1208 | return NULL; |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1209 | LOG(LOGL_DEBUG, "peer %p: sequence unmap %d <-- %d\n", |
| 1210 | nrm->origin, (int)(nrm->orig), (int)(nrm->repl)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1211 | set_seq(p, nrm->orig); |
| 1212 | return nrm->origin; |
| 1213 | } |
| 1214 | |
Neels Hofmeyr | 7174b16 | 2015-11-27 01:22:13 +0100 | [diff] [blame] | 1215 | static int gtphub_check_mapped_tei(struct gtphub_tunnel_endpoint *new_te, |
| 1216 | struct gtphub_tunnel_endpoint *iterated_te, |
| 1217 | uint32_t *tei_min, |
| 1218 | uint32_t *tei_max) |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1219 | { |
Neels Hofmeyr | 7174b16 | 2015-11-27 01:22:13 +0100 | [diff] [blame] | 1220 | if (new_te->tei_repl != iterated_te->tei_repl) |
| 1221 | return 1; |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1222 | |
Neels Hofmeyr | 7174b16 | 2015-11-27 01:22:13 +0100 | [diff] [blame] | 1223 | /* new_te->tei_repl is already taken. Try to find one out of the known |
| 1224 | * range. */ |
Neels Hofmeyr | 328d2f7 | 2015-12-06 19:13:21 +0100 | [diff] [blame^] | 1225 | LOG(LOGL_DEBUG, "TEI replacement %d already taken.\n", new_te->tei_repl); |
Neels Hofmeyr | 7174b16 | 2015-11-27 01:22:13 +0100 | [diff] [blame] | 1226 | |
| 1227 | if ((*tei_max) < 0xffffffff) { |
| 1228 | (*tei_max)++; |
| 1229 | new_te->tei_repl = *tei_max; |
Neels Hofmeyr | 328d2f7 | 2015-12-06 19:13:21 +0100 | [diff] [blame^] | 1230 | LOG(LOGL_DEBUG, "Using TEI %d instead.\n", new_te->tei_repl); |
Neels Hofmeyr | 7174b16 | 2015-11-27 01:22:13 +0100 | [diff] [blame] | 1231 | return 1; |
| 1232 | } else if ((*tei_min) > 1) { |
| 1233 | (*tei_min)--; |
| 1234 | new_te->tei_repl = *tei_min; |
Neels Hofmeyr | 328d2f7 | 2015-12-06 19:13:21 +0100 | [diff] [blame^] | 1235 | LOG(LOGL_DEBUG, "Using TEI %d instead.\n", new_te->tei_repl); |
Neels Hofmeyr | 7174b16 | 2015-11-27 01:22:13 +0100 | [diff] [blame] | 1236 | return 1; |
| 1237 | } |
| 1238 | |
| 1239 | /* None seems to be available. */ |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | static int gtphub_check_reused_teis(struct gtphub *hub, |
| 1244 | struct gtphub_tunnel *new_tun) |
| 1245 | { |
| 1246 | uint32_t tei_min = 0xffffffff; |
| 1247 | uint32_t tei_max = 0; |
| 1248 | int side_idx; |
| 1249 | int plane_idx; |
| 1250 | int side_idx2; |
| 1251 | int plane_idx2; |
| 1252 | struct gtphub_tunnel_endpoint *te; |
| 1253 | struct gtphub_tunnel_endpoint *te2; |
| 1254 | |
| 1255 | struct gtphub_tunnel *tun, *ntun; |
| 1256 | |
| 1257 | llist_for_each_entry_safe(tun, ntun, &hub->tunnels, entry) { |
| 1258 | if (tun == new_tun) |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1259 | continue; |
| 1260 | |
Neels Hofmeyr | 7174b16 | 2015-11-27 01:22:13 +0100 | [diff] [blame] | 1261 | /* Check whether the GSN sent a TEI that it is reusing from a |
| 1262 | * previous tunnel. */ |
Neels Hofmeyr | 18d3049 | 2015-12-02 15:00:50 +0100 | [diff] [blame] | 1263 | int tun_continue = 0; |
| 1264 | for_each_side(side_idx) { |
| 1265 | for_each_plane(plane_idx) { |
| 1266 | te = &tun->endpoint[side_idx][plane_idx]; |
| 1267 | te2 = &new_tun->endpoint[side_idx][plane_idx]; |
| 1268 | if ((te->tei_orig != te2->tei_orig) |
| 1269 | || (!te->peer) |
| 1270 | || (!te2->peer) |
| 1271 | || !gsn_addr_same(&te->peer->peer_addr->addr, |
| 1272 | &te2->peer->peer_addr->addr)) |
| 1273 | continue; |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1274 | |
Neels Hofmeyr | 18d3049 | 2015-12-02 15:00:50 +0100 | [diff] [blame] | 1275 | /* The peer is reusing a TEI that I believe to |
| 1276 | * be part of another tunnel. The other tunnel |
| 1277 | * must be stale, then. */ |
| 1278 | LOG(LOGL_NOTICE, |
| 1279 | "Expiring tunnel due to reused TEI:" |
Neels Hofmeyr | ee1e5d7 | 2015-12-06 17:18:49 +0100 | [diff] [blame] | 1280 | " %s peer %s sent %s TEI %x," |
Neels Hofmeyr | 18d3049 | 2015-12-02 15:00:50 +0100 | [diff] [blame] | 1281 | " previously used by tunnel %s...\n", |
Neels Hofmeyr | ee1e5d7 | 2015-12-06 17:18:49 +0100 | [diff] [blame] | 1282 | gtphub_side_idx_names[side_idx], |
Neels Hofmeyr | 18d3049 | 2015-12-02 15:00:50 +0100 | [diff] [blame] | 1283 | gtphub_port_str(te->peer), |
| 1284 | gtphub_plane_idx_names[plane_idx], |
| 1285 | te->tei_orig, |
| 1286 | gtphub_tunnel_str(tun)); |
| 1287 | LOG(LOGL_NOTICE, "...while establishing tunnel %s\n", |
| 1288 | gtphub_tunnel_str(new_tun)); |
Neels Hofmeyr | 52c0bd3 | 2015-12-02 14:14:32 +0100 | [diff] [blame] | 1289 | |
Neels Hofmeyr | 18d3049 | 2015-12-02 15:00:50 +0100 | [diff] [blame] | 1290 | expiring_item_del(&tun->expiry_entry); |
| 1291 | /* continue to find more matches. There shouldn't be |
| 1292 | * any, but let's make sure. However, tun is deleted, |
| 1293 | * so we need to skip to the next tunnel. */ |
| 1294 | tun_continue = 1; |
| 1295 | break; |
| 1296 | } |
| 1297 | if (tun_continue) |
| 1298 | break; |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1299 | } |
Neels Hofmeyr | 18d3049 | 2015-12-02 15:00:50 +0100 | [diff] [blame] | 1300 | if (tun_continue) |
| 1301 | continue; |
Neels Hofmeyr | 7174b16 | 2015-11-27 01:22:13 +0100 | [diff] [blame] | 1302 | |
| 1303 | /* Check whether the mapped TEIs assigned to the endpoints are |
| 1304 | * used anywhere else. */ |
| 1305 | for_each_side_and_plane(side_idx, plane_idx) { |
| 1306 | te = &tun->endpoint[side_idx][plane_idx]; |
| 1307 | tei_min = (tei_min < te->tei_repl)? tei_min : te->tei_repl; |
| 1308 | tei_max = (tei_max > te->tei_repl)? tei_max : te->tei_repl; |
| 1309 | |
| 1310 | for_each_side_and_plane(side_idx2, plane_idx2) { |
| 1311 | te2 = &new_tun->endpoint[side_idx2][plane_idx2]; |
| 1312 | if (!gtphub_check_mapped_tei(te2, te, &tei_min, &tei_max)) { |
| 1313 | LOG(LOGL_ERROR, |
| 1314 | "No mapped TEI is readily available." |
| 1315 | " Searching for holes between occupied" |
| 1316 | " TEIs not implemented."); |
| 1317 | return 0; |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1322 | } |
Neels Hofmeyr | 7174b16 | 2015-11-27 01:22:13 +0100 | [diff] [blame] | 1323 | |
| 1324 | return 1; |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | static void gtphub_tunnel_refresh(struct gtphub *hub, |
| 1328 | struct gtphub_tunnel *tun, |
| 1329 | time_t now) |
| 1330 | { |
| 1331 | expiry_add(&hub->expire_slowly, |
| 1332 | &tun->expiry_entry, |
| 1333 | now); |
| 1334 | } |
| 1335 | |
| 1336 | static struct gtphub_tunnel_endpoint *gtphub_unmap_tei(struct gtphub *hub, |
| 1337 | struct gtp_packet_desc *p, |
Neels Hofmeyr | 10fc024 | 2015-12-01 00:23:45 +0100 | [diff] [blame] | 1338 | struct gtphub_peer_port *from, |
| 1339 | struct gtphub_tunnel **unmapped_from_tun) |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1340 | { |
| 1341 | OSMO_ASSERT(from); |
| 1342 | int other_side = other_side_idx(p->side_idx); |
| 1343 | |
| 1344 | struct gtphub_tunnel *tun; |
| 1345 | llist_for_each_entry(tun, &hub->tunnels, entry) { |
| 1346 | struct gtphub_tunnel_endpoint *te_from = |
| 1347 | &tun->endpoint[p->side_idx][p->plane_idx]; |
| 1348 | struct gtphub_tunnel_endpoint *te_to = |
| 1349 | &tun->endpoint[other_side][p->plane_idx]; |
| 1350 | if ((te_to->tei_repl == p->header_tei_rx) |
Neels Hofmeyr | fc1be3a | 2015-12-02 00:31:31 +0100 | [diff] [blame] | 1351 | && te_from->peer |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1352 | && gsn_addr_same(&te_from->peer->peer_addr->addr, |
| 1353 | &from->peer_addr->addr)) { |
| 1354 | gtphub_tunnel_refresh(hub, tun, p->timestamp); |
Neels Hofmeyr | 10fc024 | 2015-12-01 00:23:45 +0100 | [diff] [blame] | 1355 | if (unmapped_from_tun) |
| 1356 | *unmapped_from_tun = tun; |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1357 | return te_to; |
| 1358 | } |
| 1359 | } |
Neels Hofmeyr | 10fc024 | 2015-12-01 00:23:45 +0100 | [diff] [blame] | 1360 | |
| 1361 | if (unmapped_from_tun) |
| 1362 | *unmapped_from_tun = NULL; |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1363 | return NULL; |
| 1364 | } |
| 1365 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1366 | static void gtphub_map_restart_counter(struct gtphub *hub, |
Neels Hofmeyr | bc44330 | 2015-12-01 15:20:18 +0100 | [diff] [blame] | 1367 | struct gtp_packet_desc *p) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1368 | { |
Neels Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 1369 | if (p->rc != GTP_RC_PDU_C) |
| 1370 | return; |
| 1371 | |
| 1372 | int ie_idx; |
| 1373 | ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0); |
| 1374 | if (ie_idx < 0) |
| 1375 | return; |
| 1376 | |
Neels Hofmeyr | 3fdba2e | 2015-11-30 14:17:21 +0100 | [diff] [blame] | 1377 | /* Always send gtphub's own restart counter */ |
Neels Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 1378 | p->ie[ie_idx]->tv1.v = hton8(hub->restart_counter); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1379 | } |
| 1380 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1381 | static int gtphub_unmap_header_tei(struct gtphub_peer_port **to_port_p, |
Neels Hofmeyr | 10fc024 | 2015-12-01 00:23:45 +0100 | [diff] [blame] | 1382 | struct gtphub_tunnel **unmapped_from_tun, |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1383 | struct gtphub *hub, |
| 1384 | struct gtp_packet_desc *p, |
| 1385 | struct gtphub_peer_port *from_port) |
| 1386 | { |
| 1387 | OSMO_ASSERT(p->version == 1); |
| 1388 | *to_port_p = NULL; |
Neels Hofmeyr | 10fc024 | 2015-12-01 00:23:45 +0100 | [diff] [blame] | 1389 | if (unmapped_from_tun) |
| 1390 | *unmapped_from_tun = NULL; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1391 | |
| 1392 | /* If the header's TEI is zero, no PDP context has been established |
| 1393 | * yet. If nonzero, a mapping should actually already exist for this |
| 1394 | * TEI, since it must have been announced in a PDP context creation. */ |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1395 | if (!p->header_tei_rx) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1396 | return 0; |
| 1397 | |
| 1398 | /* to_peer has previously announced a TEI, which was stored and |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1399 | * mapped in a tunnel struct. */ |
| 1400 | struct gtphub_tunnel_endpoint *to; |
Neels Hofmeyr | 10fc024 | 2015-12-01 00:23:45 +0100 | [diff] [blame] | 1401 | to = gtphub_unmap_tei(hub, p, from_port, unmapped_from_tun); |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1402 | if (!to) { |
| 1403 | LOG(LOGL_ERROR, "Received unknown TEI %" PRIx32 " from %s\n", |
| 1404 | p->header_tei_rx, gtphub_port_str(from_port)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1405 | return -1; |
| 1406 | } |
Neels Hofmeyr | bc44330 | 2015-12-01 15:20:18 +0100 | [diff] [blame] | 1407 | OSMO_ASSERT(*unmapped_from_tun); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1408 | |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1409 | uint32_t unmapped_tei = to->tei_orig; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1410 | set_tei(p, unmapped_tei); |
| 1411 | |
Neels Hofmeyr | bc44330 | 2015-12-01 15:20:18 +0100 | [diff] [blame] | 1412 | LOG(LOGL_DEBUG, "Unmapped TEI coming from: %s\n", |
| 1413 | gtphub_tunnel_str(*unmapped_from_tun)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1414 | |
Neels Hofmeyr | bc44330 | 2015-12-01 15:20:18 +0100 | [diff] [blame] | 1415 | /* May be NULL for an invalidated tunnel. */ |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1416 | *to_port_p = to->peer; |
| 1417 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1418 | return 0; |
| 1419 | } |
| 1420 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 1421 | static int gtphub_handle_create_pdp_ctx(struct gtphub *hub, |
| 1422 | struct gtp_packet_desc *p, |
| 1423 | struct gtphub_peer_port *from_ctrl, |
| 1424 | struct gtphub_peer_port *to_ctrl) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1425 | { |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1426 | int plane_idx; |
| 1427 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1428 | osmo_static_assert((GTPH_PLANE_CTRL == 0) && (GTPH_PLANE_USER == 1), |
| 1429 | plane_nrs_match_GSN_addr_IE_indices); |
| 1430 | |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 1431 | struct gtphub_tunnel *tun = p->tun; |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1432 | |
| 1433 | if (p->type == GTP_CREATE_PDP_REQ) { |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 1434 | if (p->side_idx != GTPH_SIDE_SGSN) { |
| 1435 | LOG(LOGL_ERROR, "Wrong side: Create PDP Context" |
Neels Hofmeyr | e5a0798 | 2015-12-03 13:47:05 +0100 | [diff] [blame] | 1436 | " Request from the GGSN side: %s", |
| 1437 | gtphub_port_str(from_ctrl)); |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 1438 | return -1; |
| 1439 | } |
| 1440 | |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 1441 | if (tun) { |
| 1442 | LOG(LOGL_ERROR, "Not implemented: Received" |
| 1443 | " Create PDP Context Request for an already" |
| 1444 | " established tunnel:" |
| 1445 | " from %s, tunnel %s\n", |
| 1446 | gtphub_port_str(from_ctrl), |
| 1447 | gtphub_tunnel_str(p->tun)); |
| 1448 | return -1; |
| 1449 | } |
| 1450 | |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1451 | /* A new tunnel. */ |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 1452 | p->tun = tun = gtphub_tunnel_new(); |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1453 | llist_add(&tun->entry, &hub->tunnels); |
| 1454 | gtphub_tunnel_refresh(hub, tun, p->timestamp); |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 1455 | /* The endpoint peers on this side (SGSN) will be set from IEs |
| 1456 | * below. Also set the GGSN Ctrl endpoint, for logging. */ |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1457 | gtphub_tunnel_endpoint_set_peer(&tun->endpoint[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL], |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 1458 | to_ctrl); |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1459 | } else if (p->type == GTP_CREATE_PDP_RSP) { |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 1460 | if (p->side_idx != GTPH_SIDE_GGSN) { |
| 1461 | LOG(LOGL_ERROR, "Wrong side: Create PDP Context" |
Neels Hofmeyr | e5a0798 | 2015-12-03 13:47:05 +0100 | [diff] [blame] | 1462 | " Response from the SGSN side: %s", |
| 1463 | gtphub_port_str(from_ctrl)); |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 1464 | return -1; |
| 1465 | } |
| 1466 | |
Neels Hofmeyr | 8c5b073 | 2015-12-03 13:45:15 +0100 | [diff] [blame] | 1467 | /* The tunnel should already have been resolved from the header |
| 1468 | * TEI and be available in tun (== p->tun). Just fill in the |
| 1469 | * GSN Addresses below.*/ |
| 1470 | OSMO_ASSERT(tun); |
| 1471 | OSMO_ASSERT(tun->endpoint[other_side_idx(p->side_idx)][GTPH_PLANE_CTRL].tei_repl |
| 1472 | == p->header_tei_rx); |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 1473 | OSMO_ASSERT(to_ctrl); |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1474 | } |
| 1475 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1476 | uint8_t ie_type[] = { GTPIE_TEI_C, GTPIE_TEI_DI }; |
| 1477 | int ie_mandatory = (p->type == GTP_CREATE_PDP_REQ); |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1478 | unsigned int side_idx = p->side_idx; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1479 | |
| 1480 | for (plane_idx = 0; plane_idx < 2; plane_idx++) { |
Neels Hofmeyr | 0855008 | 2015-11-30 12:19:50 +0100 | [diff] [blame] | 1481 | int rc; |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 1482 | struct gsn_addr use_addr; |
| 1483 | uint16_t use_port; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1484 | uint32_t tei_from_ie; |
| 1485 | int ie_idx; |
| 1486 | |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1487 | /* Fetch GSN Address and TEI from IEs. As ensured by above |
| 1488 | * static asserts, plane_idx corresponds to the GSN Address IE |
| 1489 | * index (the first one = 0 = ctrl, second one = 1 = user). */ |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 1490 | rc = gsn_addr_get(&use_addr, p, plane_idx); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1491 | if (rc) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1492 | LOG(LOGL_ERROR, "Cannot read %s GSN Address IE\n", |
| 1493 | gtphub_plane_idx_names[plane_idx]); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1494 | return -1; |
| 1495 | } |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1496 | LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1497 | gtphub_plane_idx_names[plane_idx], |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 1498 | gsn_addr_to_str(&use_addr), |
| 1499 | use_addr.len); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1500 | |
| 1501 | ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0); |
| 1502 | if (ie_idx < 0) { |
| 1503 | if (ie_mandatory) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1504 | LOG(LOGL_ERROR, |
| 1505 | "Create PDP Context message invalid:" |
| 1506 | " missing IE %d\n", |
| 1507 | (int)ie_type[plane_idx]); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1508 | return -1; |
| 1509 | } |
| 1510 | tei_from_ie = 0; |
| 1511 | } |
| 1512 | else |
| 1513 | tei_from_ie = ntoh32(p->ie[ie_idx]->tv4.v); |
| 1514 | |
| 1515 | /* Make sure an entry for this peer address with default port |
Neels Hofmeyr | ca2361c | 2015-12-03 14:12:44 +0100 | [diff] [blame] | 1516 | * exists. |
| 1517 | * |
| 1518 | * Exception: if sgsn_use_sender is set, instead use the |
| 1519 | * sender's address and port for Ctrl -- the User port is not |
| 1520 | * known until the first User packet arrives. |
| 1521 | * |
| 1522 | * Note: doing this here is just an optimization, because |
| 1523 | * gtphub_handle_buf() has code to replace the tunnel |
| 1524 | * endpoints' addresses with the sender (needed for User |
| 1525 | * plane). We could just ignore sgsn_use_sender here. But if we |
| 1526 | * set up a default port here and replace it in |
| 1527 | * gtphub_handle_buf(), we'd be creating a peer port just to |
| 1528 | * expire it right away. */ |
| 1529 | if (hub->sgsn_use_sender && (side_idx == GTPH_SIDE_SGSN)) { |
| 1530 | gsn_addr_from_sockaddr(&use_addr, &use_port, &from_ctrl->sa); |
| 1531 | } else { |
| 1532 | use_port = gtphub_plane_idx_default_port[plane_idx]; |
| 1533 | |
| 1534 | } |
| 1535 | |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 1536 | struct gtphub_peer_port *peer_from_ie; |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 1537 | peer_from_ie = gtphub_port_have(hub, |
| 1538 | &hub->to_gsns[side_idx][plane_idx], |
| 1539 | &use_addr, use_port); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1540 | |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1541 | gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx], |
| 1542 | peer_from_ie); |
| 1543 | |
Neels Hofmeyr | 59c1b64 | 2015-12-03 11:30:43 +0100 | [diff] [blame] | 1544 | if (!tei_from_ie && |
| 1545 | !tun->endpoint[side_idx][plane_idx].tei_orig) { |
| 1546 | LOG(LOGL_ERROR, |
Neels Hofmeyr | 328d2f7 | 2015-12-06 19:13:21 +0100 | [diff] [blame^] | 1547 | "Create PDP Context message omits %s TEI, but" |
| 1548 | " no TEI has been announced for this tunnel: %s\n", |
Neels Hofmeyr | 59c1b64 | 2015-12-03 11:30:43 +0100 | [diff] [blame] | 1549 | gtphub_plane_idx_names[plane_idx], |
| 1550 | gtphub_tunnel_str(tun)); |
| 1551 | return -1; |
| 1552 | } |
| 1553 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1554 | if (tei_from_ie) { |
| 1555 | /* Create TEI mapping and replace in GTP packet IE */ |
Neels Hofmeyr | d121ea6 | 2015-11-27 01:20:53 +0100 | [diff] [blame] | 1556 | uint32_t mapped_tei = nr_pool_next(&hub->tei_pool); |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1557 | |
| 1558 | tun->endpoint[side_idx][plane_idx].tei_orig = tei_from_ie; |
| 1559 | tun->endpoint[side_idx][plane_idx].tei_repl = mapped_tei; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1560 | p->ie[ie_idx]->tv4.v = hton32(mapped_tei); |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1561 | |
Neels Hofmeyr | cd865d6 | 2015-11-30 12:58:48 +0100 | [diff] [blame] | 1562 | if (!gtphub_check_reused_teis(hub, tun)) { |
| 1563 | /* It's highly unlikely that all TEIs are |
| 1564 | * taken. But the code looking for an unused |
| 1565 | * TEI is, at the time of writing this comment, |
| 1566 | * not able to find gaps in the TEI space. To |
| 1567 | * explicitly alert the user of this problem, |
| 1568 | * rather abort than carry on. */ |
| 1569 | LOG(LOGL_FATAL, "TEI range exhausted. Cannot create TEI mapping, aborting.\n"); |
| 1570 | abort(); |
| 1571 | } |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | /* Replace the GSN address to reflect gtphub. */ |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 1575 | rc = gsn_addr_put(&hub->to_gsns[other_side_idx(side_idx)][plane_idx].local_addr, |
| 1576 | p, plane_idx); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1577 | if (rc) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1578 | LOG(LOGL_ERROR, "Cannot write %s GSN Address IE\n", |
| 1579 | gtphub_plane_idx_names[plane_idx]); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1580 | return -1; |
| 1581 | } |
| 1582 | } |
| 1583 | |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1584 | if (p->type == GTP_CREATE_PDP_REQ) { |
| 1585 | LOG(LOGL_DEBUG, "New tunnel, first half: %s\n", |
| 1586 | gtphub_tunnel_str(tun)); |
Neels Hofmeyr | 005f175 | 2015-11-30 12:19:11 +0100 | [diff] [blame] | 1587 | } else if (p->type == GTP_CREATE_PDP_RSP) { |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1588 | LOG(LOGL_DEBUG, "New tunnel: %s\n", |
| 1589 | gtphub_tunnel_str(tun)); |
| 1590 | } |
| 1591 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1592 | return 0; |
| 1593 | } |
| 1594 | |
Neels Hofmeyr | 10fc024 | 2015-12-01 00:23:45 +0100 | [diff] [blame] | 1595 | static void pending_delete_del_cb(struct expiring_item *expi) |
| 1596 | { |
| 1597 | struct pending_delete *pd; |
| 1598 | pd = container_of(expi, struct pending_delete, expiry_entry); |
| 1599 | |
| 1600 | llist_del(&pd->entry); |
| 1601 | INIT_LLIST_HEAD(&pd->entry); |
| 1602 | |
| 1603 | pd->expiry_entry.del_cb = 0; |
| 1604 | expiring_item_del(&pd->expiry_entry); |
| 1605 | |
| 1606 | talloc_free(pd); |
| 1607 | } |
| 1608 | |
| 1609 | static struct pending_delete *pending_delete_new(void) |
| 1610 | { |
| 1611 | struct pending_delete *pd = talloc_zero(osmo_gtphub_ctx, struct pending_delete); |
| 1612 | INIT_LLIST_HEAD(&pd->entry); |
| 1613 | expiring_item_init(&pd->expiry_entry); |
| 1614 | pd->expiry_entry.del_cb = pending_delete_del_cb; |
| 1615 | return pd; |
| 1616 | } |
| 1617 | |
Neels Hofmeyr | ff4b630 | 2015-11-30 00:07:02 +0100 | [diff] [blame] | 1618 | static int gtphub_handle_delete_pdp_ctx(struct gtphub *hub, |
| 1619 | struct gtp_packet_desc *p, |
| 1620 | struct gtphub_peer_port *from_ctrl, |
| 1621 | struct gtphub_peer_port *to_ctrl) |
| 1622 | { |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 1623 | struct gtphub_tunnel *known_tun = p->tun; |
| 1624 | |
Neels Hofmeyr | 10fc024 | 2015-12-01 00:23:45 +0100 | [diff] [blame] | 1625 | if (p->type == GTP_DELETE_PDP_REQ) { |
| 1626 | if (!known_tun) { |
| 1627 | LOG(LOGL_ERROR, "Cannot find tunnel for Delete PDP Context Request.\n"); |
| 1628 | return -1; |
| 1629 | } |
| 1630 | |
| 1631 | /* Store the Delete Request until a successful Response is seen. */ |
| 1632 | uint8_t teardown_ind; |
| 1633 | uint8_t nsapi; |
| 1634 | |
| 1635 | if (gtpie_gettv1(p->ie, GTPIE_TEARDOWN, 0, &teardown_ind) != 0) { |
| 1636 | LOG(LOGL_ERROR, "Missing Teardown Ind IE in Delete PDP Context Request.\n"); |
| 1637 | return -1; |
| 1638 | } |
| 1639 | |
| 1640 | if (gtpie_gettv1(p->ie, GTPIE_NSAPI, 0, &nsapi) != 0) { |
| 1641 | LOG(LOGL_ERROR, "Missing NSAPI IE in Delete PDP Context Request.\n"); |
| 1642 | return -1; |
| 1643 | } |
| 1644 | |
| 1645 | struct pending_delete *pd = NULL; |
| 1646 | |
| 1647 | struct pending_delete *pdi = NULL; |
| 1648 | llist_for_each_entry(pdi, &hub->pending_deletes, entry) { |
| 1649 | if ((pdi->tun == known_tun) |
| 1650 | && (pdi->teardown_ind == teardown_ind) |
| 1651 | && (pdi->nsapi == nsapi)) { |
| 1652 | pd = pdi; |
| 1653 | break; |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | if (!pd) { |
| 1658 | pd = pending_delete_new(); |
| 1659 | pd->tun = known_tun; |
| 1660 | pd->teardown_ind = teardown_ind; |
| 1661 | pd->nsapi = nsapi; |
| 1662 | |
| 1663 | LOG(LOGL_DEBUG, "Tunnel delete pending: %s\n", |
| 1664 | gtphub_tunnel_str(known_tun)); |
| 1665 | llist_add(&pd->entry, &hub->pending_deletes); |
| 1666 | } |
| 1667 | |
| 1668 | /* Add or refresh timeout. */ |
| 1669 | expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp); |
| 1670 | |
| 1671 | /* If a pending_delete should expire before the response to |
| 1672 | * indicate success comes in, the responding peer will have the |
| 1673 | * tunnel deactivated, while the requesting peer gets no reply |
| 1674 | * and keeps the tunnel. The hope is that the requesting peer |
| 1675 | * will try again and get a useful response. */ |
| 1676 | } else if (p->type == GTP_DELETE_PDP_RSP) { |
| 1677 | /* Find the Delete Request for this Response. */ |
| 1678 | struct pending_delete *pd = NULL; |
| 1679 | |
| 1680 | struct pending_delete *pdi; |
| 1681 | llist_for_each_entry(pdi, &hub->pending_deletes, entry) { |
| 1682 | if (known_tun == pdi->tun) { |
| 1683 | pd = pdi; |
| 1684 | break; |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | if (!pd) { |
| 1689 | LOG(LOGL_ERROR, "Delete PDP Context Response:" |
| 1690 | " Cannot find matching request."); |
| 1691 | /* If we delete the tunnel now, anyone can send a |
| 1692 | * Delete response to kill tunnels at will. */ |
| 1693 | return -1; |
| 1694 | } |
| 1695 | |
| 1696 | /* TODO handle teardown_ind and nsapi */ |
| 1697 | |
| 1698 | expiring_item_del(&pd->expiry_entry); |
| 1699 | |
| 1700 | uint8_t cause; |
| 1701 | if (gtpie_gettv1(p->ie, GTPIE_CAUSE, 0, &cause) != 0) { |
| 1702 | LOG(LOGL_ERROR, "Delete PDP Context Response:" |
| 1703 | " Missing Cause IE."); |
| 1704 | /* If we delete the tunnel now, at least one of the |
| 1705 | * peers may still think it is active. */ |
| 1706 | return -1; |
| 1707 | } |
| 1708 | |
| 1709 | if (cause != GTPCAUSE_ACC_REQ) { |
| 1710 | LOG(LOGL_NOTICE, |
| 1711 | "Delete PDP Context Response indicates failure;" |
| 1712 | "for %s\n", |
| 1713 | gtphub_tunnel_str(known_tun)); |
| 1714 | return -1; |
| 1715 | } |
| 1716 | |
| 1717 | LOG(LOGL_DEBUG, "Delete PDP Context: removing tunnel %s\n", |
| 1718 | gtphub_tunnel_str(known_tun)); |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 1719 | p->tun = NULL; |
Neels Hofmeyr | 10fc024 | 2015-12-01 00:23:45 +0100 | [diff] [blame] | 1720 | expiring_item_del(&known_tun->expiry_entry); |
| 1721 | } |
| 1722 | |
Neels Hofmeyr | ff4b630 | 2015-11-30 00:07:02 +0100 | [diff] [blame] | 1723 | return 0; |
| 1724 | } |
| 1725 | |
| 1726 | static int gtphub_handle_update_pdp_ctx(struct gtphub *hub, |
| 1727 | struct gtp_packet_desc *p, |
| 1728 | struct gtphub_peer_port *from_ctrl, |
| 1729 | struct gtphub_peer_port *to_ctrl) |
| 1730 | { |
| 1731 | /* TODO */ |
| 1732 | return 0; |
| 1733 | } |
| 1734 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 1735 | /* Read GSN address IEs from p, and make sure these peer addresses exist in |
| 1736 | * bind[plane_idx] with default ports, in their respective planes (both Ctrl |
| 1737 | * and User). Map TEIs announced in IEs, and write mapped TEIs in-place into |
| 1738 | * the packet p. */ |
| 1739 | static int gtphub_handle_pdp_ctx(struct gtphub *hub, |
| 1740 | struct gtp_packet_desc *p, |
| 1741 | struct gtphub_peer_port *from_ctrl, |
| 1742 | struct gtphub_peer_port *to_ctrl) |
| 1743 | { |
| 1744 | OSMO_ASSERT(p->plane_idx == GTPH_PLANE_CTRL); |
| 1745 | |
| 1746 | switch (p->type) { |
| 1747 | case GTP_CREATE_PDP_REQ: |
| 1748 | case GTP_CREATE_PDP_RSP: |
| 1749 | return gtphub_handle_create_pdp_ctx(hub, p, |
| 1750 | from_ctrl, to_ctrl); |
Neels Hofmeyr | ff4b630 | 2015-11-30 00:07:02 +0100 | [diff] [blame] | 1751 | |
| 1752 | case GTP_DELETE_PDP_REQ: |
| 1753 | case GTP_DELETE_PDP_RSP: |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 1754 | return gtphub_handle_delete_pdp_ctx(hub, p, |
Neels Hofmeyr | ff4b630 | 2015-11-30 00:07:02 +0100 | [diff] [blame] | 1755 | from_ctrl, to_ctrl); |
| 1756 | |
| 1757 | case GTP_UPDATE_PDP_REQ: |
| 1758 | case GTP_UPDATE_PDP_RSP: |
| 1759 | return gtphub_handle_update_pdp_ctx(hub, p, |
| 1760 | from_ctrl, to_ctrl); |
| 1761 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 1762 | default: |
| 1763 | /* Nothing to do for this message type. */ |
| 1764 | return 0; |
| 1765 | } |
| 1766 | |
| 1767 | } |
| 1768 | |
Neels Hofmeyr | bc44330 | 2015-12-01 15:20:18 +0100 | [diff] [blame] | 1769 | static int gtphub_send_del_pdp_ctx(struct gtphub *hub, |
| 1770 | struct gtphub_tunnel *tun, |
| 1771 | int to_side) |
| 1772 | { |
| 1773 | static uint8_t del_ctx_msg[16] = { |
| 1774 | 0x32, /* GTP v1 flags */ |
| 1775 | GTP_DELETE_PDP_REQ, |
| 1776 | 0x00, 0x08, /* Length in network byte order */ |
| 1777 | 0x00, 0x00, 0x00, 0x00, /* TEI to be replaced */ |
| 1778 | 0, 0, /* Seq, to be replaced */ |
| 1779 | 0, 0, /* no extensions */ |
| 1780 | 0x13, 0xff, /* 19: Teardown ind = 1 */ |
| 1781 | 0x14, 0 /* 20: NSAPI = 0 */ |
| 1782 | }; |
| 1783 | |
| 1784 | uint32_t *tei = (uint32_t*)&del_ctx_msg[4]; |
| 1785 | uint16_t *seq = (uint16_t*)&del_ctx_msg[8]; |
| 1786 | |
| 1787 | struct gtphub_tunnel_endpoint *te = |
| 1788 | &tun->endpoint[to_side][GTPH_PLANE_CTRL]; |
| 1789 | |
| 1790 | if (! te->peer) |
| 1791 | return 0; |
| 1792 | |
| 1793 | *tei = hton32(te->tei_orig); |
| 1794 | *seq = hton16(nr_pool_next(&te->peer->peer_addr->peer->seq_pool)); |
| 1795 | |
| 1796 | struct gtphub_bind *to_bind = &hub->to_gsns[to_side][GTPH_PLANE_CTRL]; |
Neels Hofmeyr | d8660ef | 2015-12-03 11:24:39 +0100 | [diff] [blame] | 1797 | int rc = gtphub_write(&to_bind->ofd, &te->peer->sa, |
| 1798 | del_ctx_msg, sizeof(del_ctx_msg)); |
Neels Hofmeyr | bc44330 | 2015-12-01 15:20:18 +0100 | [diff] [blame] | 1799 | if (rc != 0) { |
| 1800 | LOG(LOGL_ERROR, |
| 1801 | "Failed to send out-of-band Delete PDP Context Request to %s\n", |
| 1802 | gtphub_port_str(te->peer)); |
| 1803 | } |
| 1804 | return rc; |
| 1805 | } |
| 1806 | |
| 1807 | /* Tell all peers on the other end of tunnels that PDP contexts are void. */ |
| 1808 | static void gtphub_restarted(struct gtphub *hub, |
| 1809 | struct gtp_packet_desc *p, |
| 1810 | struct gtphub_peer_port *pp) |
| 1811 | { |
Neels Hofmeyr | 936b890 | 2015-12-02 14:31:08 +0100 | [diff] [blame] | 1812 | LOG(LOGL_DEBUG, "Peer has restarted: %s\n", |
| 1813 | gtphub_port_str(pp)); |
| 1814 | |
Neels Hofmeyr | bc44330 | 2015-12-01 15:20:18 +0100 | [diff] [blame] | 1815 | struct gtphub_tunnel *tun; |
| 1816 | llist_for_each_entry(tun, &hub->tunnels, entry) { |
| 1817 | int side_idx; |
| 1818 | for_each_side(side_idx) { |
Neels Hofmeyr | c6d51f5 | 2015-12-02 15:44:34 +0100 | [diff] [blame] | 1819 | struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][GTPH_PLANE_CTRL]; |
| 1820 | struct gtphub_tunnel_endpoint *te2 = &tun->endpoint[other_side_idx(side_idx)][GTPH_PLANE_CTRL]; |
| 1821 | if ((!te->peer) |
| 1822 | || (!te2->tei_orig) |
| 1823 | || (pp->peer_addr->peer != te->peer->peer_addr->peer)) |
Neels Hofmeyr | bc44330 | 2015-12-01 15:20:18 +0100 | [diff] [blame] | 1824 | continue; |
| 1825 | |
Neels Hofmeyr | 936b890 | 2015-12-02 14:31:08 +0100 | [diff] [blame] | 1826 | LOG(LOGL_DEBUG, "Deleting tunnel due to peer restart: %s\n", |
| 1827 | gtphub_tunnel_str(tun)); |
| 1828 | |
Neels Hofmeyr | bc44330 | 2015-12-01 15:20:18 +0100 | [diff] [blame] | 1829 | /* Send a Delete PDP Context Request to the |
| 1830 | * peer on the other side, remember the pending |
| 1831 | * delete and wait for the response to delete |
| 1832 | * the tunnel. Clear this side of the tunnel to |
| 1833 | * make sure it isn't used. |
| 1834 | * |
| 1835 | * Should the delete message send fail, or if no |
| 1836 | * response is received, this tunnel will expire. If |
| 1837 | * its TEIs come up in a new PDP Context Request, it |
| 1838 | * will be removed. If messages for this tunnel should |
| 1839 | * come in (from the not restarted side), they will be |
| 1840 | * dropped because the tunnel is rendered unusable. */ |
| 1841 | gtphub_send_del_pdp_ctx(hub, tun, other_side_idx(side_idx)); |
| 1842 | |
| 1843 | struct pending_delete *pd; |
| 1844 | pd = pending_delete_new(); |
| 1845 | pd->tun = tun; |
| 1846 | pd->teardown_ind = 0xff; |
| 1847 | pd->nsapi = 0; |
| 1848 | llist_add(&pd->entry, &hub->pending_deletes); |
| 1849 | expiry_add(&hub->expire_quickly, &pd->expiry_entry, p->timestamp); |
| 1850 | |
| 1851 | gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_CTRL], |
| 1852 | NULL); |
| 1853 | gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][GTPH_PLANE_USER], |
| 1854 | NULL); |
| 1855 | } |
| 1856 | } |
| 1857 | } |
| 1858 | |
| 1859 | static int get_restart_count(struct gtp_packet_desc *p) |
| 1860 | { |
| 1861 | int ie_idx; |
| 1862 | ie_idx = gtpie_getie(p->ie, GTPIE_RECOVERY, 0); |
| 1863 | if (ie_idx < 0) |
| 1864 | return -1; |
| 1865 | return ntoh8(p->ie[ie_idx]->tv1.v); |
| 1866 | } |
| 1867 | |
| 1868 | static void gtphub_check_restart_counter(struct gtphub *hub, |
| 1869 | struct gtp_packet_desc *p, |
| 1870 | struct gtphub_peer_port *from) |
| 1871 | { |
| 1872 | /* If the peer is sending a Recovery IE (7.7.11) with a restart counter |
| 1873 | * that doesn't match the peer's previously sent restart counter, clear |
| 1874 | * that peer and cancel PDP contexts. */ |
| 1875 | |
| 1876 | int restart = get_restart_count(p); |
| 1877 | |
| 1878 | if ((restart < 0) || (restart > 255)) |
| 1879 | return; |
| 1880 | |
| 1881 | if ((from->last_restart_count >= 0) && (from->last_restart_count <= 255)) { |
| 1882 | if (from->last_restart_count != restart) { |
| 1883 | gtphub_restarted(hub, p, from); |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | from->last_restart_count = restart; |
| 1888 | } |
| 1889 | |
Neels Hofmeyr | 3fdba2e | 2015-11-30 14:17:21 +0100 | [diff] [blame] | 1890 | static int from_sgsns_read_cb(struct osmo_fd *from_sgsns_ofd, unsigned int what) |
| 1891 | { |
| 1892 | unsigned int plane_idx = from_sgsns_ofd->priv_nr; |
| 1893 | OSMO_ASSERT(plane_idx < GTPH_PLANE_N); |
Neels Hofmeyr | 28a70f2 | 2015-12-06 15:22:54 +0100 | [diff] [blame] | 1894 | LOG(LOGL_DEBUG, "=== reading from SGSN (%s)\n", |
Neels Hofmeyr | 3fdba2e | 2015-11-30 14:17:21 +0100 | [diff] [blame] | 1895 | gtphub_plane_idx_names[plane_idx]); |
| 1896 | |
| 1897 | if (!(what & BSC_FD_READ)) |
| 1898 | return 0; |
| 1899 | |
| 1900 | struct gtphub *hub = from_sgsns_ofd->data; |
| 1901 | |
| 1902 | static uint8_t buf[4096]; |
| 1903 | struct osmo_sockaddr from_addr; |
| 1904 | struct osmo_sockaddr to_addr; |
| 1905 | struct osmo_fd *to_ofd; |
| 1906 | int len; |
| 1907 | uint8_t *reply_buf; |
| 1908 | |
| 1909 | len = gtphub_read(from_sgsns_ofd, &from_addr, buf, sizeof(buf)); |
| 1910 | if (len < 1) |
| 1911 | return 0; |
| 1912 | |
| 1913 | len = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, &from_addr, |
| 1914 | buf, len, gtphub_now(), |
| 1915 | &reply_buf, &to_ofd, &to_addr); |
| 1916 | if (len < 1) |
| 1917 | return 0; |
| 1918 | |
| 1919 | return gtphub_write(to_ofd, &to_addr, reply_buf, len); |
| 1920 | } |
| 1921 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1922 | static int from_ggsns_read_cb(struct osmo_fd *from_ggsns_ofd, unsigned int what) |
| 1923 | { |
| 1924 | unsigned int plane_idx = from_ggsns_ofd->priv_nr; |
| 1925 | OSMO_ASSERT(plane_idx < GTPH_PLANE_N); |
Neels Hofmeyr | 28a70f2 | 2015-12-06 15:22:54 +0100 | [diff] [blame] | 1926 | LOG(LOGL_DEBUG, "=== reading from GGSN (%s)\n", |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1927 | gtphub_plane_idx_names[plane_idx]); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1928 | if (!(what & BSC_FD_READ)) |
| 1929 | return 0; |
| 1930 | |
| 1931 | struct gtphub *hub = from_ggsns_ofd->data; |
| 1932 | |
| 1933 | static uint8_t buf[4096]; |
| 1934 | struct osmo_sockaddr from_addr; |
| 1935 | struct osmo_sockaddr to_addr; |
| 1936 | struct osmo_fd *to_ofd; |
Neels Hofmeyr | 16c3f57 | 2015-11-11 17:27:01 +0100 | [diff] [blame] | 1937 | int len; |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1938 | uint8_t *reply_buf; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1939 | |
| 1940 | len = gtphub_read(from_ggsns_ofd, &from_addr, buf, sizeof(buf)); |
| 1941 | if (len < 1) |
| 1942 | return 0; |
| 1943 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 1944 | len = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, &from_addr, |
| 1945 | buf, len, gtphub_now(), |
| 1946 | &reply_buf, &to_ofd, &to_addr); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1947 | if (len < 1) |
| 1948 | return 0; |
| 1949 | |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 1950 | return gtphub_write(to_ofd, &to_addr, reply_buf, len); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | static int gtphub_unmap(struct gtphub *hub, |
| 1954 | struct gtp_packet_desc *p, |
| 1955 | struct gtphub_peer_port *from, |
| 1956 | struct gtphub_peer_port *to_proxy, |
| 1957 | struct gtphub_peer_port **final_unmapped, |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 1958 | struct gtphub_peer_port **unmapped_from_seq) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1959 | { |
| 1960 | /* Always (try to) unmap sequence and TEI numbers, which need to be |
| 1961 | * replaced in the packet. Either way, give precedence to the proxy, if |
| 1962 | * configured. */ |
| 1963 | |
Neels Hofmeyr | 3fdba2e | 2015-11-30 14:17:21 +0100 | [diff] [blame] | 1964 | if (unmapped_from_seq) |
| 1965 | *unmapped_from_seq = NULL; |
Neels Hofmeyr | 3fdba2e | 2015-11-30 14:17:21 +0100 | [diff] [blame] | 1966 | if (final_unmapped) |
| 1967 | *final_unmapped = NULL; |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 1968 | p->tun = NULL; |
Neels Hofmeyr | 3fdba2e | 2015-11-30 14:17:21 +0100 | [diff] [blame] | 1969 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1970 | struct gtphub_peer_port *from_seq = NULL; |
| 1971 | struct gtphub_peer_port *from_tei = NULL; |
| 1972 | struct gtphub_peer_port *unmapped = NULL; |
| 1973 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1974 | from_seq = gtphub_unmap_seq(p, from); |
| 1975 | |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 1976 | if (gtphub_unmap_header_tei(&from_tei, &p->tun, hub, p, from) < 0) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1977 | return -1; |
| 1978 | |
| 1979 | struct gtphub_peer *from_peer = from->peer_addr->peer; |
| 1980 | if (from_seq && from_tei && (from_seq != from_tei)) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1981 | LOG(LOGL_DEBUG, |
| 1982 | "Seq unmap and TEI unmap yield two different peers." |
| 1983 | " Using seq unmap." |
Neels Hofmeyr | 28a70f2 | 2015-12-06 15:22:54 +0100 | [diff] [blame] | 1984 | " (from %s %s: seq %d yields %s, tei %u yields %s)\n", |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1985 | gtphub_plane_idx_names[p->plane_idx], |
| 1986 | gtphub_peer_str(from_peer), |
| 1987 | (int)p->seq, |
| 1988 | gtphub_port_str(from_seq), |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 1989 | (unsigned int)p->header_tei_rx, |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1990 | gtphub_port_str2(from_tei) |
| 1991 | ); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1992 | } |
| 1993 | unmapped = (from_seq? from_seq : from_tei); |
| 1994 | |
| 1995 | if (unmapped && to_proxy && (unmapped != to_proxy)) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 1996 | LOG(LOGL_NOTICE, |
| 1997 | "Unmap yields a different peer than the configured proxy." |
| 1998 | " Using proxy." |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 1999 | " unmapped: %s proxy: %s\n", |
| 2000 | gtphub_port_str(unmapped), |
| 2001 | gtphub_port_str2(to_proxy) |
| 2002 | ); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2003 | } |
| 2004 | unmapped = (to_proxy? to_proxy : unmapped); |
| 2005 | |
| 2006 | if (!unmapped) { |
| 2007 | /* Return no error, but returned pointers are all NULL. */ |
| 2008 | return 0; |
| 2009 | } |
| 2010 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2011 | if (unmapped_from_seq) |
| 2012 | *unmapped_from_seq = from_seq; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2013 | if (final_unmapped) |
| 2014 | *final_unmapped = unmapped; |
| 2015 | return 0; |
| 2016 | } |
| 2017 | |
| 2018 | static int gsn_addr_to_sockaddr(struct gsn_addr *src, |
| 2019 | uint16_t port, |
| 2020 | struct osmo_sockaddr *dst) |
| 2021 | { |
| 2022 | return osmo_sockaddr_init_udp(dst, gsn_addr_to_str(src), port); |
| 2023 | } |
| 2024 | |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 2025 | /* If p is an Echo request, replace p's data with the matching response and |
| 2026 | * return 1. If p is no Echo request, return 0, or -1 if an invalid packet is |
| 2027 | * detected. */ |
Neels Hofmeyr | 3fdba2e | 2015-11-30 14:17:21 +0100 | [diff] [blame] | 2028 | static int gtphub_handle_echo_req(struct gtphub *hub, struct gtp_packet_desc *p, |
| 2029 | uint8_t **reply_buf) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2030 | { |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 2031 | if (p->type != GTP_ECHO_REQ) |
| 2032 | return 0; |
| 2033 | |
| 2034 | static uint8_t echo_response_data[14] = { |
Neels Hofmeyr | 3fdba2e | 2015-11-30 14:17:21 +0100 | [diff] [blame] | 2035 | 0x32, /* GTP v1 flags */ |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 2036 | GTP_ECHO_RSP, |
| 2037 | 0x00, 14 - 8, /* Length in network byte order */ |
| 2038 | 0x00, 0x00, 0x00, 0x00, /* Zero TEI */ |
| 2039 | 0, 0, /* Seq, to be replaced */ |
| 2040 | 0, 0, /* no extensions */ |
| 2041 | 0x0e, /* Recovery IE */ |
Neels Hofmeyr | 3fdba2e | 2015-11-30 14:17:21 +0100 | [diff] [blame] | 2042 | 0 /* Restart counter, to be replaced */ |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 2043 | }; |
| 2044 | uint16_t *seq = (uint16_t*)&echo_response_data[8]; |
| 2045 | uint8_t *recovery = &echo_response_data[13]; |
| 2046 | |
| 2047 | *seq = hton16(p->seq); |
| 2048 | *recovery = hub->restart_counter; |
| 2049 | |
| 2050 | *reply_buf = echo_response_data; |
| 2051 | |
| 2052 | return sizeof(echo_response_data); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2053 | } |
| 2054 | |
Neels Hofmeyr | c83cd89 | 2015-11-11 14:01:06 +0100 | [diff] [blame] | 2055 | struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind, |
| 2056 | const struct osmo_sockaddr *addr); |
| 2057 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2058 | /* Parse buffer as GTP packet, replace elements in-place and return the ofd and |
| 2059 | * address to forward to. Return a pointer to the osmo_fd, but copy the |
| 2060 | * sockaddr to *to_addr. The reason for this is that the sockaddr may expire at |
| 2061 | * any moment, while the osmo_fd is guaranteed to persist. Return the number of |
| 2062 | * bytes to forward, 0 or less on failure. */ |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2063 | int gtphub_handle_buf(struct gtphub *hub, |
| 2064 | unsigned int side_idx, |
| 2065 | unsigned int plane_idx, |
| 2066 | const struct osmo_sockaddr *from_addr, |
| 2067 | uint8_t *buf, |
| 2068 | size_t received, |
| 2069 | time_t now, |
| 2070 | uint8_t **reply_buf, |
| 2071 | struct osmo_fd **to_ofd, |
| 2072 | struct osmo_sockaddr *to_addr) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2073 | { |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2074 | struct gtphub_bind *from_bind = &hub->to_gsns[side_idx][plane_idx]; |
| 2075 | struct gtphub_bind *to_bind = &hub->to_gsns[other_side_idx(side_idx)][plane_idx]; |
Neels Hofmeyr | dba6d1a | 2015-11-20 01:27:22 +0100 | [diff] [blame] | 2076 | |
Neels Hofmeyr | 1ba50c6 | 2015-11-20 01:28:40 +0100 | [diff] [blame] | 2077 | rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_IN], |
| 2078 | received); |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2079 | LOG(LOGL_DEBUG, "%s rx %s from %s %s\n", |
| 2080 | (side_idx == GTPH_SIDE_GGSN)? "<-" : "->", |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 2081 | gtphub_plane_idx_names[plane_idx], |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2082 | gtphub_side_idx_names[side_idx], |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 2083 | osmo_sockaddr_to_str(from_addr)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2084 | |
Neels Hofmeyr | d8660ef | 2015-12-03 11:24:39 +0100 | [diff] [blame] | 2085 | struct gtp_packet_desc p; |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2086 | gtp_decode(buf, received, side_idx, plane_idx, &p, now); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2087 | |
Neels Hofmeyr | 87c83d0 | 2015-12-03 11:25:27 +0100 | [diff] [blame] | 2088 | if (p.rc <= 0) { |
| 2089 | LOG(LOGL_ERROR, "INVALID: dropping GTP packet from %s %s %s\n", |
| 2090 | gtphub_side_idx_names[side_idx], |
| 2091 | gtphub_plane_idx_names[plane_idx], |
| 2092 | osmo_sockaddr_to_str(from_addr)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2093 | return -1; |
Neels Hofmeyr | 87c83d0 | 2015-12-03 11:25:27 +0100 | [diff] [blame] | 2094 | } |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2095 | |
Neels Hofmeyr | 1ba50c6 | 2015-11-20 01:28:40 +0100 | [diff] [blame] | 2096 | rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_IN]); |
| 2097 | |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 2098 | int reply_len; |
Neels Hofmeyr | 3fdba2e | 2015-11-30 14:17:21 +0100 | [diff] [blame] | 2099 | reply_len = gtphub_handle_echo_req(hub, &p, reply_buf); |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 2100 | if (reply_len > 0) { |
| 2101 | /* It was an echo. Nothing left to do. */ |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2102 | osmo_sockaddr_copy(to_addr, from_addr); |
Neels Hofmeyr | dba6d1a | 2015-11-20 01:27:22 +0100 | [diff] [blame] | 2103 | *to_ofd = &from_bind->ofd; |
Neels Hofmeyr | 4b2cbda | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 2104 | |
| 2105 | rate_ctr_inc(&from_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]); |
| 2106 | rate_ctr_add(&from_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT], |
| 2107 | reply_len); |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2108 | LOG(LOGL_DEBUG, "%s Echo response to %s: %d bytes to %s\n", |
| 2109 | (side_idx == GTPH_SIDE_GGSN)? "-->" : "<--", |
| 2110 | gtphub_side_idx_names[side_idx], |
Neels Hofmeyr | 4b2cbda | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 2111 | (int)reply_len, osmo_sockaddr_to_str(to_addr)); |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 2112 | return reply_len; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2113 | } |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 2114 | if (reply_len < 0) |
| 2115 | return -1; |
| 2116 | |
Neels Hofmeyr | dba6d1a | 2015-11-20 01:27:22 +0100 | [diff] [blame] | 2117 | *to_ofd = &to_bind->ofd; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2118 | |
Neels Hofmeyr | 3fdba2e | 2015-11-30 14:17:21 +0100 | [diff] [blame] | 2119 | /* If a proxy is configured, check that it's indeed that proxy talking |
| 2120 | * to us. A proxy is a forced 1:1 connection, e.g. to another gtphub, |
| 2121 | * so no-one else is allowed to talk to us from that side. */ |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2122 | struct gtphub_peer_port *from_peer = hub->proxy[side_idx][plane_idx]; |
| 2123 | if (from_peer) { |
| 2124 | if (osmo_sockaddr_cmp(&from_peer->sa, from_addr) != 0) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 2125 | LOG(LOGL_ERROR, |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2126 | "Rejecting: %s proxy configured, but GTP packet" |
| 2127 | " received on %s bind is from another sender:" |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 2128 | " proxy: %s sender: %s\n", |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2129 | gtphub_side_idx_names[side_idx], |
| 2130 | gtphub_side_idx_names[side_idx], |
| 2131 | gtphub_port_str(from_peer), |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 2132 | osmo_sockaddr_to_str(from_addr)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2133 | return -1; |
| 2134 | } |
| 2135 | } |
| 2136 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2137 | if (!from_peer) { |
| 2138 | /* Find or create a peer with a matching address. The sender's |
| 2139 | * port may in fact differ. */ |
| 2140 | from_peer = gtphub_known_addr_have_port(from_bind, from_addr); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | /* If any PDP context has been created, we already have an entry for |
Neels Hofmeyr | 3fdba2e | 2015-11-30 14:17:21 +0100 | [diff] [blame] | 2144 | * this GSN. If we don't have an entry, a GGSN has nothing to tell us |
| 2145 | * about, while an SGSN may initiate a PDP context. */ |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2146 | if (!from_peer) { |
| 2147 | if (side_idx == GTPH_SIDE_GGSN) { |
| 2148 | LOG(LOGL_ERROR, "Dropping packet: unknown GGSN peer: %s\n", |
| 2149 | osmo_sockaddr_to_str(from_addr)); |
| 2150 | return -1; |
| 2151 | } else { |
| 2152 | /* SGSN */ |
| 2153 | /* A new peer. If this is on the Ctrl plane, an SGSN |
| 2154 | * may make first contact without being known yet, so |
| 2155 | * create the peer struct for the current sender. */ |
| 2156 | if (plane_idx != GTPH_PLANE_CTRL) { |
| 2157 | LOG(LOGL_ERROR, |
| 2158 | "Dropping packet: User plane peer was not" |
| 2159 | "announced by PDP Context: %s\n", |
| 2160 | osmo_sockaddr_to_str(from_addr)); |
| 2161 | return -1; |
| 2162 | } |
| 2163 | |
| 2164 | struct gsn_addr from_gsna; |
| 2165 | uint16_t from_port; |
| 2166 | if (gsn_addr_from_sockaddr(&from_gsna, &from_port, from_addr) != 0) |
| 2167 | return -1; |
| 2168 | |
| 2169 | from_peer = gtphub_port_have(hub, from_bind, &from_gsna, from_port); |
| 2170 | } |
| 2171 | } |
| 2172 | |
| 2173 | if (!from_peer) { |
| 2174 | /* This could theoretically happen for invalid address data or |
| 2175 | * somesuch. */ |
| 2176 | LOG(LOGL_ERROR, "Dropping packet: invalid %s peer: %s\n", |
| 2177 | gtphub_side_idx_names[side_idx], |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 2178 | osmo_sockaddr_to_str(from_addr)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2179 | return -1; |
| 2180 | } |
| 2181 | |
Neels Hofmeyr | e38fb66 | 2015-12-06 16:44:14 +0100 | [diff] [blame] | 2182 | rate_ctr_add(&from_peer->counters_io->ctr[GTPH_CTR_BYTES_IN], |
| 2183 | received); |
| 2184 | rate_ctr_inc(&from_peer->counters_io->ctr[GTPH_CTR_PKTS_IN]); |
| 2185 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2186 | LOG(LOGL_DEBUG, "from %s peer: %s\n", gtphub_side_idx_names[side_idx], |
| 2187 | gtphub_port_str(from_peer)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2188 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2189 | struct gtphub_peer_port *to_peer_from_seq; |
| 2190 | struct gtphub_peer_port *to_peer; |
| 2191 | if (gtphub_unmap(hub, &p, from_peer, |
| 2192 | hub->proxy[other_side_idx(side_idx)][plane_idx], |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 2193 | &to_peer, &to_peer_from_seq) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2194 | != 0) { |
| 2195 | return -1; |
| 2196 | } |
| 2197 | |
Neels Hofmeyr | e38fb66 | 2015-12-06 16:44:14 +0100 | [diff] [blame] | 2198 | if (p.tun) { |
| 2199 | struct gtphub_tunnel_endpoint *te = &p.tun->endpoint[p.side_idx][p.plane_idx]; |
| 2200 | rate_ctr_add(&te->counters_io->ctr[GTPH_CTR_BYTES_IN], |
| 2201 | received); |
| 2202 | rate_ctr_inc(&te->counters_io->ctr[GTPH_CTR_PKTS_IN]); |
| 2203 | } |
| 2204 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2205 | if ((!to_peer) && (side_idx == GTPH_SIDE_SGSN)) { |
| 2206 | if (gtphub_resolve_ggsn(hub, &p, &to_peer) < 0) |
| 2207 | return -1; |
| 2208 | } |
| 2209 | |
| 2210 | if (!to_peer) { |
| 2211 | LOG(LOGL_ERROR, "No %s to send to. Dropping packet.\n", |
| 2212 | gtphub_side_idx_names[other_side_idx(side_idx)]); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2213 | return -1; |
| 2214 | } |
| 2215 | |
| 2216 | if (plane_idx == GTPH_PLANE_CTRL) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 2217 | /* This may be a Create PDP Context response. If it is, there |
| 2218 | * are other addresses in the GTP message to set up apart from |
| 2219 | * the sender. */ |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 2220 | if (gtphub_handle_pdp_ctx(hub, &p, from_peer, to_peer) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2221 | != 0) |
| 2222 | return -1; |
| 2223 | } |
Neels Hofmeyr | d53c604 | 2015-12-03 13:59:49 +0100 | [diff] [blame] | 2224 | |
| 2225 | /* Either to_peer was resolved from an existing tunnel, |
| 2226 | * or a PDP Ctx and thus a tunnel has just been created, |
| 2227 | * or the tunnel has been deleted due to this message. */ |
| 2228 | OSMO_ASSERT(p.tun || (p.type == GTP_DELETE_PDP_RSP)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2229 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2230 | gtphub_check_restart_counter(hub, &p, from_peer); |
Neels Hofmeyr | bc44330 | 2015-12-01 15:20:18 +0100 | [diff] [blame] | 2231 | gtphub_map_restart_counter(hub, &p); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2232 | |
| 2233 | /* If the GGSN is replying to an SGSN request, the sequence nr has |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2234 | * already been unmapped above (to_peer_from_seq != NULL), and we need not |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2235 | * create a new mapping. */ |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2236 | if (!to_peer_from_seq) |
| 2237 | gtphub_map_seq(&p, from_peer, to_peer); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2238 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2239 | osmo_sockaddr_copy(to_addr, &to_peer->sa); |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 2240 | |
| 2241 | *reply_buf = (uint8_t*)p.data; |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 2242 | |
Neels Hofmeyr | 1ba50c6 | 2015-11-20 01:28:40 +0100 | [diff] [blame] | 2243 | if (received) { |
| 2244 | rate_ctr_inc(&to_bind->counters_io->ctr[GTPH_CTR_PKTS_OUT]); |
| 2245 | rate_ctr_add(&to_bind->counters_io->ctr[GTPH_CTR_BYTES_OUT], |
| 2246 | received); |
Neels Hofmeyr | e38fb66 | 2015-12-06 16:44:14 +0100 | [diff] [blame] | 2247 | |
| 2248 | rate_ctr_inc(&to_peer->counters_io->ctr[GTPH_CTR_PKTS_OUT]); |
| 2249 | rate_ctr_add(&to_peer->counters_io->ctr[GTPH_CTR_BYTES_OUT], |
| 2250 | received); |
Neels Hofmeyr | 1ba50c6 | 2015-11-20 01:28:40 +0100 | [diff] [blame] | 2251 | } |
Neels Hofmeyr | e38fb66 | 2015-12-06 16:44:14 +0100 | [diff] [blame] | 2252 | |
| 2253 | if (p.tun) { |
| 2254 | struct gtphub_tunnel_endpoint *te = &p.tun->endpoint[other_side_idx(p.side_idx)][p.plane_idx]; |
| 2255 | rate_ctr_inc(&te->counters_io->ctr[GTPH_CTR_PKTS_OUT]); |
| 2256 | rate_ctr_add(&te->counters_io->ctr[GTPH_CTR_BYTES_OUT], |
| 2257 | received); |
| 2258 | } |
| 2259 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2260 | LOG(LOGL_DEBUG, "%s Forward to %s: %d bytes to %s\n", |
| 2261 | (side_idx == GTPH_SIDE_SGSN)? "-->" : "<--", |
| 2262 | gtphub_side_idx_names[other_side_idx(side_idx)], |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 2263 | (int)received, osmo_sockaddr_to_str(to_addr)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2264 | return received; |
| 2265 | } |
| 2266 | |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 2267 | static void resolved_gssn_del_cb(struct expiring_item *expi) |
| 2268 | { |
| 2269 | struct gtphub_resolved_ggsn *ggsn; |
| 2270 | ggsn = container_of(expi, struct gtphub_resolved_ggsn, expiry_entry); |
| 2271 | |
| 2272 | gtphub_port_ref_count_dec(ggsn->peer); |
| 2273 | llist_del(&ggsn->entry); |
| 2274 | |
| 2275 | ggsn->expiry_entry.del_cb = 0; |
| 2276 | expiring_item_del(&ggsn->expiry_entry); |
| 2277 | |
| 2278 | talloc_free(ggsn); |
| 2279 | } |
| 2280 | |
| 2281 | void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str, |
| 2282 | struct gsn_addr *resolved_addr, |
| 2283 | time_t now) |
| 2284 | { |
| 2285 | struct gtphub_peer_port *pp; |
| 2286 | struct gtphub_resolved_ggsn *ggsn; |
| 2287 | |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 2288 | LOG(LOGL_DEBUG, "Resolved GGSN callback: %s %s\n", |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 2289 | apn_oi_str, osmo_hexdump((unsigned char*)resolved_addr, |
| 2290 | sizeof(*resolved_addr))); |
Neels Hofmeyr | 3317c84 | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 2291 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2292 | pp = gtphub_port_have(hub, &hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL], |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 2293 | resolved_addr, 2123); |
| 2294 | if (!pp) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 2295 | LOG(LOGL_ERROR, "Internal: Cannot create/find peer '%s'\n", |
| 2296 | gsn_addr_to_str(resolved_addr)); |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 2297 | return; |
| 2298 | } |
| 2299 | |
| 2300 | ggsn = talloc_zero(osmo_gtphub_ctx, struct gtphub_resolved_ggsn); |
| 2301 | OSMO_ASSERT(ggsn); |
Neels Hofmeyr | a4370dd | 2015-11-24 12:46:11 +0100 | [diff] [blame] | 2302 | INIT_LLIST_HEAD(&ggsn->entry); |
| 2303 | expiring_item_init(&ggsn->expiry_entry); |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 2304 | |
| 2305 | ggsn->peer = pp; |
| 2306 | gtphub_port_ref_count_inc(pp); |
| 2307 | |
| 2308 | 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] | 2309 | ggsn->apn_oi_str[sizeof(ggsn->apn_oi_str) - 1] = '\0'; |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 2310 | |
| 2311 | ggsn->expiry_entry.del_cb = resolved_gssn_del_cb; |
Neels Hofmeyr | 2c8b581 | 2015-11-25 16:45:59 +0100 | [diff] [blame] | 2312 | expiry_add(&hub->expire_slowly, &ggsn->expiry_entry, now); |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 2313 | |
| 2314 | llist_add(&ggsn->entry, &hub->resolved_ggsns); |
| 2315 | } |
| 2316 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2317 | static int gtphub_gc_peer_port(struct gtphub_peer_port *pp) |
| 2318 | { |
| 2319 | return pp->ref_count == 0; |
| 2320 | } |
| 2321 | |
| 2322 | static int gtphub_gc_peer_addr(struct gtphub_peer_addr *pa) |
| 2323 | { |
| 2324 | struct gtphub_peer_port *pp, *npp; |
| 2325 | llist_for_each_entry_safe(pp, npp, &pa->ports, entry) { |
| 2326 | if (gtphub_gc_peer_port(pp)) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 2327 | LOG(LOGL_DEBUG, "expired: peer %s\n", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2328 | gtphub_port_str(pp)); |
| 2329 | gtphub_peer_port_del(pp); |
| 2330 | } |
| 2331 | } |
| 2332 | return llist_empty(&pa->ports); |
| 2333 | } |
| 2334 | |
| 2335 | static int gtphub_gc_peer(struct gtphub_peer *p) |
| 2336 | { |
| 2337 | struct gtphub_peer_addr *pa, *npa; |
| 2338 | llist_for_each_entry_safe(pa, npa, &p->addresses, entry) { |
| 2339 | if (gtphub_gc_peer_addr(pa)) { |
| 2340 | gtphub_peer_addr_del(pa); |
| 2341 | } |
| 2342 | } |
| 2343 | |
| 2344 | /* Note that there's a ref_count in each gtphub_peer_port instance |
| 2345 | * listed within p->addresses, referenced by TEI mappings from |
| 2346 | * hub->tei_map. As long as those don't expire, this peer will stay. */ |
| 2347 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2348 | return llist_empty(&p->addresses) |
| 2349 | && nr_map_empty(&p->seq_map); |
| 2350 | } |
| 2351 | |
| 2352 | static void gtphub_gc_bind(struct gtphub_bind *b) |
| 2353 | { |
| 2354 | struct gtphub_peer *p, *n; |
| 2355 | llist_for_each_entry_safe(p, n, &b->peers, entry) { |
| 2356 | if (gtphub_gc_peer(p)) { |
| 2357 | gtphub_peer_del(p); |
| 2358 | } |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | void gtphub_gc(struct gtphub *hub, time_t now) |
| 2363 | { |
| 2364 | int expired; |
Neels Hofmeyr | 2c8b581 | 2015-11-25 16:45:59 +0100 | [diff] [blame] | 2365 | expired = expiry_tick(&hub->expire_quickly, now); |
| 2366 | expired += expiry_tick(&hub->expire_slowly, now); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2367 | |
| 2368 | /* ... */ |
| 2369 | |
| 2370 | if (expired) { |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2371 | int s, p; |
| 2372 | for_each_side_and_plane(s, p) { |
| 2373 | gtphub_gc_bind(&hub->to_gsns[s][p]); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2374 | } |
| 2375 | } |
| 2376 | } |
| 2377 | |
| 2378 | static void gtphub_gc_cb(void *data) |
| 2379 | { |
| 2380 | struct gtphub *hub = data; |
| 2381 | gtphub_gc(hub, gtphub_now()); |
| 2382 | osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0); |
| 2383 | } |
| 2384 | |
| 2385 | static void gtphub_gc_start(struct gtphub *hub) |
| 2386 | { |
| 2387 | hub->gc_timer.cb = gtphub_gc_cb; |
| 2388 | hub->gc_timer.data = hub; |
| 2389 | |
| 2390 | osmo_timer_schedule(&hub->gc_timer, GTPH_GC_TICK_SECONDS, 0); |
| 2391 | } |
| 2392 | |
| 2393 | /* called by unit tests */ |
| 2394 | void gtphub_init(struct gtphub *hub) |
| 2395 | { |
| 2396 | gtphub_zero(hub); |
| 2397 | |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 2398 | INIT_LLIST_HEAD(&hub->tunnels); |
Neels Hofmeyr | 10fc024 | 2015-12-01 00:23:45 +0100 | [diff] [blame] | 2399 | INIT_LLIST_HEAD(&hub->pending_deletes); |
Neels Hofmeyr | e54cd15 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 2400 | |
Neels Hofmeyr | 2c8b581 | 2015-11-25 16:45:59 +0100 | [diff] [blame] | 2401 | expiry_init(&hub->expire_quickly, GTPH_EXPIRE_QUICKLY_SECS); |
| 2402 | expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2403 | |
Neels Hofmeyr | d121ea6 | 2015-11-27 01:20:53 +0100 | [diff] [blame] | 2404 | nr_pool_init(&hub->tei_pool, 1, 0xffffffff); |
| 2405 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2406 | int side_idx; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2407 | int plane_idx; |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2408 | for_each_side_and_plane(side_idx, plane_idx) { |
| 2409 | gtphub_bind_init(&hub->to_gsns[side_idx][plane_idx]); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2410 | } |
Neels Hofmeyr | 390e910 | 2015-11-16 13:45:13 +0100 | [diff] [blame] | 2411 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2412 | hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].label = "SGSN Ctrl"; |
| 2413 | hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].label = "GGSN Ctrl"; |
| 2414 | hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].label = "SGSN User"; |
| 2415 | hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].label = "GGSN User"; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2416 | } |
| 2417 | |
Neels Hofmeyr | 20bd6bf | 2015-11-20 00:08:28 +0100 | [diff] [blame] | 2418 | /* For the test suite, this is kept separate from gtphub_stop(), which also |
| 2419 | * closes sockets. The test suite avoids using sockets and would cause |
| 2420 | * segfaults when trying to close uninitialized ofds. */ |
| 2421 | void gtphub_free(struct gtphub *hub) |
| 2422 | { |
| 2423 | /* By expiring all mappings, a garbage collection should free |
| 2424 | * everything else. A gtphub_bind_free() will assert that everything is |
| 2425 | * indeed empty. */ |
Neels Hofmeyr | 2c8b581 | 2015-11-25 16:45:59 +0100 | [diff] [blame] | 2426 | expiry_clear(&hub->expire_quickly); |
| 2427 | expiry_clear(&hub->expire_slowly); |
Neels Hofmeyr | 20bd6bf | 2015-11-20 00:08:28 +0100 | [diff] [blame] | 2428 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2429 | int side_idx; |
Neels Hofmeyr | 20bd6bf | 2015-11-20 00:08:28 +0100 | [diff] [blame] | 2430 | int plane_idx; |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2431 | for_each_side_and_plane(side_idx, plane_idx) { |
| 2432 | gtphub_gc_bind(&hub->to_gsns[side_idx][plane_idx]); |
| 2433 | gtphub_bind_free(&hub->to_gsns[side_idx][plane_idx]); |
Neels Hofmeyr | 20bd6bf | 2015-11-20 00:08:28 +0100 | [diff] [blame] | 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | void gtphub_stop(struct gtphub *hub) |
| 2438 | { |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2439 | int side_idx; |
Neels Hofmeyr | 20bd6bf | 2015-11-20 00:08:28 +0100 | [diff] [blame] | 2440 | int plane_idx; |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2441 | for_each_side_and_plane(side_idx, plane_idx) { |
| 2442 | gtphub_bind_stop(&hub->to_gsns[side_idx][plane_idx]); |
Neels Hofmeyr | 20bd6bf | 2015-11-20 00:08:28 +0100 | [diff] [blame] | 2443 | } |
| 2444 | gtphub_free(hub); |
| 2445 | } |
| 2446 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2447 | static int gtphub_make_proxy(struct gtphub *hub, |
| 2448 | struct gtphub_peer_port **pp, |
| 2449 | struct gtphub_bind *bind, |
| 2450 | const struct gtphub_cfg_addr *addr) |
| 2451 | { |
| 2452 | if (!addr->addr_str) |
| 2453 | return 0; |
| 2454 | |
| 2455 | struct gsn_addr gsna; |
| 2456 | if (gsn_addr_from_str(&gsna, addr->addr_str) != 0) |
| 2457 | return -1; |
| 2458 | |
| 2459 | *pp = gtphub_port_have(hub, bind, &gsna, addr->port); |
| 2460 | |
| 2461 | /* This is *the* proxy. Make sure it is never expired. */ |
| 2462 | gtphub_port_ref_count_inc(*pp); |
| 2463 | return 0; |
| 2464 | } |
| 2465 | |
Neels Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 2466 | int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg, |
| 2467 | uint8_t restart_counter) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2468 | { |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2469 | gtphub_init(hub); |
Neels Hofmeyr | b6c2db5 | 2015-11-18 18:11:32 +0100 | [diff] [blame] | 2470 | |
Neels Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 2471 | hub->restart_counter = restart_counter; |
Neels Hofmeyr | ca2361c | 2015-12-03 14:12:44 +0100 | [diff] [blame] | 2472 | hub->sgsn_use_sender = cfg->sgsn_use_sender? 1 : 0; |
Neels Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 2473 | |
Neels Hofmeyr | b6c2db5 | 2015-11-18 18:11:32 +0100 | [diff] [blame] | 2474 | /* If a Ctrl plane proxy is configured, ares will never be used. */ |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2475 | if (!cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str) { |
Neels Hofmeyr | b6c2db5 | 2015-11-18 18:11:32 +0100 | [diff] [blame] | 2476 | if (gtphub_ares_init(hub) != 0) { |
| 2477 | LOG(LOGL_FATAL, "Failed to initialize ares\n"); |
| 2478 | return -1; |
| 2479 | } |
| 2480 | } |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2481 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2482 | int side_idx; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2483 | int plane_idx; |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2484 | for_each_side_and_plane(side_idx, plane_idx) { |
Neels Hofmeyr | 0855008 | 2015-11-30 12:19:50 +0100 | [diff] [blame] | 2485 | int rc; |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2486 | rc = gtphub_bind_start(&hub->to_gsns[side_idx][plane_idx], |
| 2487 | &cfg->to_gsns[side_idx][plane_idx], |
| 2488 | (side_idx == GTPH_SIDE_SGSN) |
| 2489 | ? from_sgsns_read_cb |
| 2490 | : from_ggsns_read_cb, |
| 2491 | hub, plane_idx); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2492 | if (rc) { |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2493 | LOG(LOGL_FATAL, "Failed to bind for %ss (%s)\n", |
| 2494 | gtphub_side_idx_names[side_idx], |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 2495 | gtphub_plane_idx_names[plane_idx]); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2496 | return rc; |
| 2497 | } |
| 2498 | } |
| 2499 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2500 | for_each_side_and_plane(side_idx, plane_idx) { |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2501 | if (gtphub_make_proxy(hub, |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2502 | &hub->proxy[side_idx][plane_idx], |
| 2503 | &hub->to_gsns[side_idx][plane_idx], |
| 2504 | &cfg->proxy[side_idx][plane_idx]) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2505 | != 0) { |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2506 | LOG(LOGL_FATAL, "Cannot configure %s proxy" |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 2507 | " %s port %d.\n", |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2508 | gtphub_side_idx_names[side_idx], |
| 2509 | cfg->proxy[side_idx][plane_idx].addr_str, |
| 2510 | (int)cfg->proxy[side_idx][plane_idx].port); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2511 | return -1; |
| 2512 | } |
| 2513 | } |
| 2514 | |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2515 | for_each_side_and_plane(side_idx, plane_idx) { |
| 2516 | if (hub->proxy[side_idx][plane_idx]) |
| 2517 | LOG(LOGL_NOTICE, "Using %s %s proxy %s\n", |
| 2518 | gtphub_side_idx_names[side_idx], |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2519 | gtphub_plane_idx_names[plane_idx], |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 2520 | gtphub_port_str(hub->proxy[side_idx][plane_idx])); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2521 | } |
| 2522 | |
Neels Hofmeyr | e1ba781 | 2015-12-03 14:48:22 +0100 | [diff] [blame] | 2523 | if (hub->sgsn_use_sender) |
Neels Hofmeyr | 9d8f506 | 2015-12-03 14:52:33 +0100 | [diff] [blame] | 2524 | LOG(LOGL_NOTICE, "Using sender address and port for SGSN instead of GSN Addr IE and default ports.\n"); |
Neels Hofmeyr | e1ba781 | 2015-12-03 14:48:22 +0100 | [diff] [blame] | 2525 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2526 | gtphub_gc_start(hub); |
| 2527 | return 0; |
| 2528 | } |
| 2529 | |
| 2530 | static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer *peer, |
| 2531 | const struct gsn_addr *addr) |
| 2532 | { |
| 2533 | struct gtphub_peer_addr *a; |
| 2534 | llist_for_each_entry(a, &peer->addresses, entry) { |
| 2535 | if (gsn_addr_same(&a->addr, addr)) |
| 2536 | return a; |
| 2537 | } |
| 2538 | return NULL; |
| 2539 | } |
| 2540 | |
| 2541 | static struct gtphub_peer_port *gtphub_addr_find_port(const struct gtphub_peer_addr *a, |
| 2542 | uint16_t port) |
| 2543 | { |
| 2544 | OSMO_ASSERT(port); |
| 2545 | struct gtphub_peer_port *pp; |
| 2546 | llist_for_each_entry(pp, &a->ports, entry) { |
| 2547 | if (pp->port == port) |
| 2548 | return pp; |
| 2549 | } |
| 2550 | return NULL; |
| 2551 | } |
| 2552 | |
| 2553 | static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind *bind, |
| 2554 | const struct gsn_addr *addr) |
| 2555 | { |
| 2556 | struct gtphub_peer *peer; |
| 2557 | llist_for_each_entry(peer, &bind->peers, entry) { |
| 2558 | struct gtphub_peer_addr *a = gtphub_peer_find_addr(peer, addr); |
| 2559 | if (a) |
| 2560 | return a; |
| 2561 | } |
| 2562 | return NULL; |
| 2563 | } |
| 2564 | |
| 2565 | static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind *bind, |
| 2566 | const struct gsn_addr *addr, |
| 2567 | uint16_t port) |
| 2568 | { |
| 2569 | struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr); |
| 2570 | if (!a) |
| 2571 | return NULL; |
| 2572 | return gtphub_addr_find_port(a, port); |
| 2573 | } |
| 2574 | |
| 2575 | struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind, |
| 2576 | const struct osmo_sockaddr *addr) |
| 2577 | { |
| 2578 | struct gsn_addr gsna; |
| 2579 | uint16_t port; |
| 2580 | gsn_addr_from_sockaddr(&gsna, &port, addr); |
| 2581 | return gtphub_port_find(bind, &gsna, port); |
| 2582 | } |
| 2583 | |
| 2584 | static struct gtphub_peer *gtphub_peer_new(struct gtphub *hub, |
| 2585 | struct gtphub_bind *bind) |
| 2586 | { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 2587 | struct gtphub_peer *peer = talloc_zero(osmo_gtphub_ctx, |
| 2588 | struct gtphub_peer); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2589 | OSMO_ASSERT(peer); |
| 2590 | |
| 2591 | INIT_LLIST_HEAD(&peer->addresses); |
| 2592 | |
Neels Hofmeyr | e2ed8e6 | 2015-11-17 14:30:37 +0100 | [diff] [blame] | 2593 | nr_pool_init(&peer->seq_pool, 0, 0xffff); |
Neels Hofmeyr | 2c8b581 | 2015-11-25 16:45:59 +0100 | [diff] [blame] | 2594 | nr_map_init(&peer->seq_map, &peer->seq_pool, &hub->expire_quickly); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2595 | |
| 2596 | /* TODO use something random to pick the initial sequence nr. |
| 2597 | 0x6d31 produces the ASCII character sequence 'm1', currently used in |
| 2598 | gtphub_nc_test.sh. */ |
| 2599 | peer->seq_pool.last_nr = 0x6d31 - 1; |
| 2600 | |
| 2601 | llist_add(&peer->entry, &bind->peers); |
| 2602 | return peer; |
| 2603 | } |
| 2604 | |
| 2605 | static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer, |
| 2606 | const struct gsn_addr *addr) |
| 2607 | { |
| 2608 | struct gtphub_peer_addr *a; |
| 2609 | a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr); |
| 2610 | OSMO_ASSERT(a); |
| 2611 | a->peer = peer; |
| 2612 | gsn_addr_copy(&a->addr, addr); |
| 2613 | INIT_LLIST_HEAD(&a->ports); |
| 2614 | llist_add(&a->entry, &peer->addresses); |
| 2615 | |
| 2616 | return a; |
| 2617 | } |
| 2618 | |
| 2619 | static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub, |
| 2620 | struct gtphub_bind *bind, |
| 2621 | const struct gsn_addr *addr) |
| 2622 | { |
| 2623 | struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr); |
| 2624 | if (a) |
| 2625 | return a; |
| 2626 | |
| 2627 | /* If we haven't found an address, that means we need to create an |
| 2628 | * entirely new peer for the new address. More addresses may be added |
| 2629 | * to this peer later, but not via this function. */ |
| 2630 | struct gtphub_peer *peer = gtphub_peer_new(hub, bind); |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 2631 | |
| 2632 | a = gtphub_peer_add_addr(peer, addr); |
| 2633 | |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 2634 | LOG(LOGL_DEBUG, "New peer address: %s %s\n", |
Neels Hofmeyr | 390e910 | 2015-11-16 13:45:13 +0100 | [diff] [blame] | 2635 | bind->label, |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame] | 2636 | gsn_addr_to_str(&a->addr)); |
| 2637 | |
| 2638 | return a; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2639 | } |
| 2640 | |
| 2641 | static struct gtphub_peer_port *gtphub_addr_add_port(struct gtphub_peer_addr *a, |
| 2642 | uint16_t port) |
| 2643 | { |
| 2644 | struct gtphub_peer_port *pp; |
| 2645 | |
| 2646 | pp = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_port); |
| 2647 | OSMO_ASSERT(pp); |
| 2648 | pp->peer_addr = a; |
| 2649 | pp->port = port; |
Neels Hofmeyr | bc44330 | 2015-12-01 15:20:18 +0100 | [diff] [blame] | 2650 | pp->last_restart_count = -1; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2651 | |
| 2652 | if (gsn_addr_to_sockaddr(&a->addr, port, &pp->sa) != 0) { |
| 2653 | talloc_free(pp); |
| 2654 | return NULL; |
| 2655 | } |
| 2656 | |
Neels Hofmeyr | e38fb66 | 2015-12-06 16:44:14 +0100 | [diff] [blame] | 2657 | pp->counters_io = rate_ctr_group_alloc(osmo_gtphub_ctx, |
| 2658 | >phub_ctrg_io_desc, 0); |
| 2659 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2660 | llist_add(&pp->entry, &a->ports); |
| 2661 | |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 2662 | LOG(LOGL_DEBUG, "New peer port: %s port %d\n", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2663 | gsn_addr_to_str(&a->addr), |
| 2664 | (int)port); |
| 2665 | |
| 2666 | return pp; |
| 2667 | } |
| 2668 | |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 2669 | struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub, |
| 2670 | struct gtphub_bind *bind, |
| 2671 | const struct gsn_addr *addr, |
| 2672 | uint16_t port) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2673 | { |
| 2674 | struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr); |
| 2675 | |
| 2676 | struct gtphub_peer_port *pp = gtphub_addr_find_port(a, port); |
| 2677 | if (pp) |
| 2678 | return pp; |
| 2679 | |
| 2680 | return gtphub_addr_add_port(a, port); |
| 2681 | } |
| 2682 | |
Neels Hofmeyr | c83cd89 | 2015-11-11 14:01:06 +0100 | [diff] [blame] | 2683 | /* Find a GGSN peer with a matching address. If the address is known but the |
| 2684 | * port not, create a new port for that peer address. */ |
| 2685 | struct gtphub_peer_port *gtphub_known_addr_have_port(const struct gtphub_bind *bind, |
| 2686 | const struct osmo_sockaddr *addr) |
| 2687 | { |
| 2688 | struct gtphub_peer_addr *pa; |
| 2689 | struct gtphub_peer_port *pp; |
| 2690 | |
| 2691 | struct gsn_addr gsna; |
| 2692 | uint16_t port; |
| 2693 | gsn_addr_from_sockaddr(&gsna, &port, addr); |
| 2694 | |
| 2695 | pa = gtphub_addr_find(bind, &gsna); |
| 2696 | if (!pa) |
| 2697 | return NULL; |
| 2698 | |
| 2699 | pp = gtphub_addr_find_port(pa, port); |
| 2700 | |
| 2701 | if (!pp) |
| 2702 | pp = gtphub_addr_add_port(pa, port); |
| 2703 | |
| 2704 | return pp; |
| 2705 | } |
| 2706 | |
| 2707 | |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 2708 | /* Return 0 if the message in p is not applicable for GGSN resolution, -1 if |
| 2709 | * resolution should be possible but failed, and 1 if resolution was |
| 2710 | * successful. *pp will be set to NULL if <1 is returned. */ |
| 2711 | static int gtphub_resolve_ggsn(struct gtphub *hub, |
| 2712 | struct gtp_packet_desc *p, |
| 2713 | struct gtphub_peer_port **pp) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2714 | { |
Neels Hofmeyr | 5b664f4 | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 2715 | *pp = NULL; |
| 2716 | |
| 2717 | /* TODO determine from message type whether IEs should be present? */ |
| 2718 | |
| 2719 | int rc; |
| 2720 | const char *imsi_str; |
| 2721 | rc = get_ie_imsi_str(p->ie, 0, &imsi_str); |
| 2722 | if (rc < 1) |
| 2723 | return rc; |
| 2724 | OSMO_ASSERT(imsi_str); |
| 2725 | |
| 2726 | const char *apn_str; |
| 2727 | rc = get_ie_apn_str(p->ie, &apn_str); |
| 2728 | if (rc < 1) |
| 2729 | return rc; |
| 2730 | OSMO_ASSERT(apn_str); |
| 2731 | |
| 2732 | *pp = gtphub_resolve_ggsn_addr(hub, imsi_str, apn_str); |
| 2733 | return (*pp)? 1 : -1; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | |
| 2737 | /* TODO move to osmocom/core/socket.c ? */ |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2738 | /* use this in osmo_sock_init() to remove dup. */ |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 2739 | /* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required |
| 2740 | to call freeaddrinfo(*result), iff zero is returned. */ |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2741 | static int _osmo_getaddrinfo(struct addrinfo **result, |
| 2742 | uint16_t family, uint16_t type, uint8_t proto, |
| 2743 | const char *host, uint16_t port) |
| 2744 | { |
| 2745 | struct addrinfo hints; |
| 2746 | char portbuf[16]; |
| 2747 | |
| 2748 | sprintf(portbuf, "%u", port); |
| 2749 | memset(&hints, '\0', sizeof(struct addrinfo)); |
| 2750 | hints.ai_family = family; |
| 2751 | if (type == SOCK_RAW) { |
| 2752 | /* Workaround for glibc, that returns EAI_SERVICE (-8) if |
| 2753 | * SOCK_RAW and IPPROTO_GRE is used. |
| 2754 | */ |
| 2755 | hints.ai_socktype = SOCK_DGRAM; |
| 2756 | hints.ai_protocol = IPPROTO_UDP; |
| 2757 | } else { |
| 2758 | hints.ai_socktype = type; |
| 2759 | hints.ai_protocol = proto; |
| 2760 | } |
| 2761 | |
| 2762 | return getaddrinfo(host, portbuf, &hints, result); |
| 2763 | } |
| 2764 | |
| 2765 | /* TODO move to osmocom/core/socket.c ? */ |
| 2766 | int osmo_sockaddr_init(struct osmo_sockaddr *addr, |
| 2767 | uint16_t family, uint16_t type, uint8_t proto, |
| 2768 | const char *host, uint16_t port) |
| 2769 | { |
| 2770 | struct addrinfo *res; |
| 2771 | int rc; |
| 2772 | rc = _osmo_getaddrinfo(&res, family, type, proto, host, port); |
| 2773 | |
| 2774 | if (rc != 0) { |
Neels Hofmeyr | 063a802 | 2015-11-16 14:35:13 +0100 | [diff] [blame] | 2775 | LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2776 | return -EINVAL; |
| 2777 | } |
| 2778 | |
| 2779 | OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a)); |
| 2780 | memcpy(&addr->a, res->ai_addr, res->ai_addrlen); |
| 2781 | addr->l = res->ai_addrlen; |
| 2782 | freeaddrinfo(res); |
| 2783 | |
| 2784 | return 0; |
| 2785 | } |
| 2786 | |
| 2787 | int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len, |
| 2788 | char *port_str, size_t port_str_len, |
| 2789 | const struct osmo_sockaddr *addr, |
| 2790 | int flags) |
| 2791 | { |
| 2792 | int rc; |
| 2793 | |
| 2794 | if ((addr->l < 1) || (addr->l > sizeof(addr->a))) { |
| 2795 | LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l); |
| 2796 | return -1; |
| 2797 | } |
| 2798 | |
| 2799 | if (addr->l > sizeof(addr->a)) { |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 2800 | LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n", |
| 2801 | addr->l); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2802 | return -1; |
| 2803 | } |
| 2804 | |
| 2805 | rc = getnameinfo((struct sockaddr*)&addr->a, addr->l, |
| 2806 | addr_str, addr_str_len, |
| 2807 | port_str, port_str_len, |
| 2808 | flags); |
| 2809 | |
| 2810 | if (rc) |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 2811 | LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n", |
| 2812 | gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a, |
| 2813 | addr->l)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2814 | |
| 2815 | return rc; |
| 2816 | } |
| 2817 | |
| 2818 | const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr, |
| 2819 | char *buf, size_t buf_len) |
| 2820 | { |
| 2821 | const int portbuf_len = 6; |
| 2822 | OSMO_ASSERT(buf_len > portbuf_len); |
| 2823 | char *portbuf = buf + buf_len - portbuf_len; |
| 2824 | buf_len -= portbuf_len; |
| 2825 | if (osmo_sockaddr_to_strs(buf, buf_len, |
| 2826 | portbuf, portbuf_len, |
| 2827 | addr, |
| 2828 | NI_NUMERICHOST | NI_NUMERICSERV)) |
| 2829 | return NULL; |
| 2830 | |
| 2831 | char *pos = buf + strnlen(buf, buf_len-1); |
| 2832 | size_t len = buf_len - (pos - buf); |
| 2833 | |
| 2834 | snprintf(pos, len, " port %s", portbuf); |
| 2835 | buf[buf_len-1] = '\0'; |
| 2836 | |
| 2837 | return buf; |
| 2838 | } |
| 2839 | |
| 2840 | const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr) |
| 2841 | { |
| 2842 | static char buf[256]; |
| 2843 | const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf)); |
| 2844 | if (! result) |
| 2845 | return "(invalid)"; |
| 2846 | return result; |
| 2847 | } |
| 2848 | |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 2849 | int osmo_sockaddr_cmp(const struct osmo_sockaddr *a, |
| 2850 | const struct osmo_sockaddr *b) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2851 | { |
| 2852 | if (a == b) |
| 2853 | return 0; |
| 2854 | if (!a) |
| 2855 | return -1; |
| 2856 | if (!b) |
| 2857 | return 1; |
| 2858 | if (a->l != b->l) { |
| 2859 | /* Lengths are not the same, but determine the order. Will |
| 2860 | * anyone ever sort a list by osmo_sockaddr though...? */ |
| 2861 | int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l); |
| 2862 | if (cmp == 0) { |
| 2863 | if (a->l < b->l) |
| 2864 | return -1; |
| 2865 | else |
| 2866 | return 1; |
| 2867 | } |
| 2868 | return cmp; |
| 2869 | } |
| 2870 | return memcmp(&a->a, &b->a, a->l); |
| 2871 | } |
| 2872 | |
Neels Hofmeyr | 9cfe037 | 2015-11-16 14:52:05 +0100 | [diff] [blame] | 2873 | void osmo_sockaddr_copy(struct osmo_sockaddr *dst, |
| 2874 | const struct osmo_sockaddr *src) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 2875 | { |
| 2876 | OSMO_ASSERT(src->l <= sizeof(dst->a)); |
| 2877 | memcpy(&dst->a, &src->a, src->l); |
| 2878 | dst->l = src->l; |
| 2879 | } |