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