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