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