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