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