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