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 | * gtphub_ext.c -- ext means extern. This file is kept separate so that these |
| 7 | * functions can be wrapped for gtphub_test.c. When a function and its callers |
| 8 | * are in the same compilational unit, the wrappability may be optimized away. |
| 9 | * |
| 10 | * Author: Neels Hofmeyr |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU Affero General Public License as published by |
| 14 | * the Free Software Foundation; either version 3 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU Affero General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Affero General Public License |
| 23 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include <string.h> |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 27 | #include <unistd.h> |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 28 | |
| 29 | #include <openbsc/gtphub.h> |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 30 | #include <openbsc/debug.h> |
| 31 | |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 32 | #include <osmocom/core/utils.h> |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 33 | #include <osmocom/gsm/apn.h> |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 34 | |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 35 | /* TODO split GRX ares from sgsn into a separate struct and allow use without |
| 36 | * globals. */ |
| 37 | #include <openbsc/sgsn.h> |
| 38 | extern struct sgsn_instance *sgsn; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 39 | |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 40 | struct sgsn_instance sgsn_inst = { 0 }; |
| 41 | struct sgsn_instance *sgsn = &sgsn_inst; |
| 42 | |
| 43 | extern void *osmo_gtphub_ctx; |
| 44 | |
| 45 | int gtphub_ares_init(struct gtphub *hub) |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 46 | { |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 47 | return sgsn_ares_init(sgsn); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 50 | struct ggsn_lookup { |
| 51 | struct llist_head entry; |
| 52 | struct expiring_item expiry_entry; |
| 53 | |
| 54 | struct gtphub *hub; |
| 55 | |
| 56 | char imsi_str[GSM_IMSI_LENGTH]; |
| 57 | char apn_ni_str[GSM_APN_LENGTH]; |
| 58 | char apn_oi_str[GSM_APN_LENGTH]; |
| 59 | int have_3dig_mnc; |
| 60 | }; |
| 61 | |
| 62 | static int start_ares_query(struct ggsn_lookup *lookup); |
| 63 | |
| 64 | static void ggsn_lookup_cb(void *arg, int status, int timeouts, struct hostent *hostent) |
| 65 | { |
| 66 | struct ggsn_lookup *lookup = arg; |
Neels Hofmeyr | 05c593f | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 67 | LOGP(DGTPHUB, LOGL_NOTICE, "ggsn_lookup_cb(%p / %p)", lookup, &lookup->expiry_entry); |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 68 | |
| 69 | if (status != ARES_SUCCESS) { |
| 70 | LOGP(DGTPHUB, LOGL_ERROR, "DNS query failed.\n"); |
| 71 | |
| 72 | /* Need to try with three digits now */ |
| 73 | if (!lookup->have_3dig_mnc) { |
| 74 | lookup->have_3dig_mnc = 1; |
| 75 | if (start_ares_query(lookup) == 0) |
| 76 | return; |
| 77 | } |
| 78 | |
Neels Hofmeyr | 05c593f | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 79 | LOGP(DGTPHUB, LOGL_ERROR, "Failed to resolve GGSN. (%p)\n", lookup); |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 80 | goto remove_from_queue; |
| 81 | } |
| 82 | |
| 83 | struct gsn_addr resolved_addr; |
| 84 | if (hostent->h_length > sizeof(resolved_addr.buf)) { |
| 85 | LOGP(DGTPHUB, LOGL_ERROR, "Addr size too large: %d > %d\n", |
| 86 | (int)hostent->h_length, (int)sizeof(resolved_addr.buf)); |
| 87 | goto remove_from_queue; |
| 88 | } |
| 89 | |
| 90 | /* Get the first addr from the list */ |
| 91 | char *addr0 = hostent->h_addr_list[0]; |
| 92 | if (!addr0) { |
| 93 | LOGP(DGTPHUB, LOGL_ERROR, "No host address.\n"); |
| 94 | goto remove_from_queue; |
| 95 | } |
| 96 | |
Neels Hofmeyr | 97b5f14 | 2015-11-11 17:27:01 +0100 | [diff] [blame] | 97 | memcpy(resolved_addr.buf, addr0, hostent->h_length); |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 98 | resolved_addr.len = hostent->h_length; |
| 99 | |
Neels Hofmeyr | 05c593f | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 100 | LOGP(DGTPHUB, LOGL_NOTICE, "resolved addr %s\n", |
Neels Hofmeyr | 86d7401 | 2015-11-11 19:26:09 +0100 | [diff] [blame^] | 101 | osmo_hexdump((unsigned char*)&resolved_addr, sizeof(resolved_addr))); |
Neels Hofmeyr | 05c593f | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 102 | |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 103 | gtphub_resolved_ggsn(lookup->hub, lookup->apn_oi_str, &resolved_addr, |
| 104 | gtphub_now()); |
| 105 | |
| 106 | remove_from_queue: |
Neels Hofmeyr | 05c593f | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 107 | LOGP(DGTPHUB, LOGL_ERROR, "Removing GGSN lookup. (%p / %p)\n", lookup, &lookup->expiry_entry); |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 108 | expiring_item_del(&lookup->expiry_entry); |
| 109 | } |
| 110 | |
| 111 | static void make_addr_str(struct ggsn_lookup *lookup) |
| 112 | { |
| 113 | char *apn_oi_str; |
| 114 | apn_oi_str = osmo_apn_qualify_from_imsi(lookup->imsi_str, |
| 115 | lookup->apn_ni_str, |
| 116 | lookup->have_3dig_mnc); |
| 117 | strncpy(lookup->apn_oi_str, apn_oi_str, sizeof(lookup->apn_oi_str)); |
| 118 | lookup->apn_oi_str[sizeof(lookup->apn_oi_str)-1] = '\0'; |
| 119 | } |
| 120 | |
| 121 | static int start_ares_query(struct ggsn_lookup *lookup) |
| 122 | { |
Neels Hofmeyr | 05c593f | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 123 | LOGP(DGTPHUB, LOGL_DEBUG, "Going to query %s (%p / %p)\n", lookup->apn_oi_str, lookup, &lookup->expiry_entry); |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 124 | |
Neels Hofmeyr | 97b5f14 | 2015-11-11 17:27:01 +0100 | [diff] [blame] | 125 | int rc = sgsn_ares_query(sgsn, lookup->apn_oi_str, ggsn_lookup_cb, lookup); |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 126 | if (rc != 0) |
| 127 | LOGP(DGTPHUB, LOGL_ERROR, "Failed to start ares query.\n"); |
| 128 | return rc; |
| 129 | } |
| 130 | |
| 131 | static void ggsn_lookup_del_cb(struct expiring_item *expi) |
| 132 | { |
Neels Hofmeyr | 05c593f | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 133 | struct ggsn_lookup *lookup; |
| 134 | lookup = container_of(expi, struct ggsn_lookup, expiry_entry); |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 135 | |
Neels Hofmeyr | 05c593f | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 136 | LOGP(DGTPHUB, LOGL_NOTICE, "ggsn_lookup_del_cb(%p / %p)\n", lookup, expi); |
| 137 | |
| 138 | lookup->expiry_entry.del_cb = 0; |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 139 | expiring_item_del(expi); |
| 140 | |
Neels Hofmeyr | 05c593f | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 141 | llist_del(&lookup->entry); |
| 142 | talloc_free(lookup); |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub, |
| 146 | const char *imsi_str, |
| 147 | const char *apn_ni_str) |
| 148 | { |
Neels Hofmeyr | ba35d0c | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 149 | OSMO_ASSERT(imsi_str); |
| 150 | OSMO_ASSERT(apn_ni_str); |
| 151 | |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 152 | struct ggsn_lookup *lookup = talloc_zero(osmo_gtphub_ctx, struct ggsn_lookup); |
Neels Hofmeyr | ba35d0c | 2015-11-10 20:32:13 +0100 | [diff] [blame] | 153 | OSMO_ASSERT(lookup); |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 154 | |
Neels Hofmeyr | 05c593f | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 155 | LOGP(DGTPHUB, LOGL_NOTICE, "Request to resolve IMSI '%s' with APN-NI '%s' (%p / %p)\n", |
| 156 | imsi_str, apn_ni_str, lookup, &lookup->expiry_entry); |
| 157 | |
Neels Hofmeyr | 97b5f14 | 2015-11-11 17:27:01 +0100 | [diff] [blame] | 158 | expiring_item_init(&lookup->expiry_entry); |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 159 | lookup->hub = hub; |
| 160 | |
| 161 | strncpy(lookup->imsi_str, imsi_str, sizeof(lookup->imsi_str)); |
| 162 | lookup->imsi_str[sizeof(lookup->imsi_str)-1] = '\0'; |
| 163 | |
| 164 | strncpy(lookup->apn_ni_str, apn_ni_str, sizeof(lookup->apn_ni_str)); |
| 165 | lookup->apn_ni_str[sizeof(lookup->apn_ni_str)-1] = '\0'; |
| 166 | |
| 167 | make_addr_str(lookup); |
| 168 | |
Neels Hofmeyr | 05c593f | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 169 | LOGP(DGTPHUB, LOGL_NOTICE, "looking for active queries...\n"); |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 170 | struct ggsn_lookup *active; |
| 171 | llist_for_each_entry(active, &hub->ggsn_lookups, entry) { |
| 172 | if (strncmp(active->apn_oi_str, lookup->apn_oi_str, |
| 173 | sizeof(lookup->apn_oi_str)) == 0) { |
| 174 | /* A query already pending. Just tip our hat. */ |
| 175 | return NULL; |
| 176 | } |
| 177 | } |
| 178 | |
Neels Hofmeyr | 05c593f | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 179 | LOGP(DGTPHUB, LOGL_NOTICE, "looking for already resolved GGSNs...\n"); |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 180 | struct gtphub_resolved_ggsn *resolved; |
| 181 | llist_for_each_entry(resolved, &hub->resolved_ggsns, entry) { |
| 182 | if (strncmp(resolved->apn_oi_str, lookup->apn_oi_str, |
| 183 | sizeof(lookup->apn_oi_str)) == 0) { |
| 184 | /* Already resolved. */ |
| 185 | return resolved->peer; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /* Kick off a resolution, but so far return nothing. The hope is that |
| 190 | * the peer will resend the request (a couple of times), and by then |
| 191 | * the GGSN will be resolved. */ |
Neels Hofmeyr | 05c593f | 2015-11-11 17:20:42 +0100 | [diff] [blame] | 192 | LOGP(DGTPHUB, LOGL_NOTICE, "kick off resolution.\n"); |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 193 | |
| 194 | llist_add(&lookup->entry, &hub->ggsn_lookups); |
| 195 | |
| 196 | lookup->expiry_entry.del_cb = ggsn_lookup_del_cb; |
| 197 | expiry_add(&hub->expire_seq_maps, &lookup->expiry_entry, gtphub_now()); |
| 198 | |
| 199 | start_ares_query(lookup); |
Neels Hofmeyr | 86d7401 | 2015-11-11 19:26:09 +0100 | [diff] [blame^] | 200 | LOGP(DGTPHUB, LOGL_NOTICE, "Resolving %s %s ..." |
| 201 | " (Returning failure, hoping for a retry" |
| 202 | "once resolution has concluded)\n", |
| 203 | imsi_str, apn_ni_str); |
Neels Hofmeyr | f16657a | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 204 | |
| 205 | return NULL; |
| 206 | } |