blob: afeeda657fff42d527fc972802db4d54e37ad271 [file] [log] [blame]
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001/* GTP Hub Implementation */
2
3/* (C) 2015 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
Neels Hofmeyr4960fab2015-11-18 17:53:00 +01006 * 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 Hofmeyrc8a614d2015-09-24 17:32:30 +020011 *
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 Hofmeyr30f7bcb2015-11-08 20:34:47 +010029#include <unistd.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020030
31#include <openbsc/gtphub.h>
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010032#include <openbsc/debug.h>
33
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020034#include <osmocom/core/utils.h>
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010035#include <osmocom/gsm/apn.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020036
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010037/* TODO split GRX ares from sgsn into a separate struct and allow use without
38 * globals. */
39#include <openbsc/sgsn.h>
40extern struct sgsn_instance *sgsn;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020041
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010042struct sgsn_instance sgsn_inst = { 0 };
43struct sgsn_instance *sgsn = &sgsn_inst;
44
45extern void *osmo_gtphub_ctx;
46
47int gtphub_ares_init(struct gtphub *hub)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020048{
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010049 return sgsn_ares_init(sgsn);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020050}
51
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010052struct ggsn_lookup {
53 struct llist_head entry;
54 struct expiring_item expiry_entry;
55
56 struct gtphub *hub;
57
Harald Welted3fa84d2016-04-20 17:50:17 +020058 char imsi_str[GSM23003_IMSI_MAX_DIGITS+1];
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010059 char apn_ni_str[GSM_APN_LENGTH];
60 char apn_oi_str[GSM_APN_LENGTH];
61 int have_3dig_mnc;
62};
63
64static int start_ares_query(struct ggsn_lookup *lookup);
65
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010066static void ggsn_lookup_cb(void *arg, int status, int timeouts,
67 struct hostent *hostent)
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010068{
69 struct ggsn_lookup *lookup = arg;
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +010070 LOGP(DGTPHUB, LOGL_NOTICE, "ggsn_lookup_cb(%p / %p)", lookup,
71 &lookup->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010072
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 Hofmeyr9cfe0372015-11-16 14:52:05 +010083 LOGP(DGTPHUB, LOGL_ERROR, "Failed to resolve GGSN. (%p)\n",
84 lookup);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010085 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 Hofmeyr16c3f572015-11-11 17:27:01 +0100102 memcpy(resolved_addr.buf, addr0, hostent->h_length);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100103 resolved_addr.len = hostent->h_length;
104
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100105 LOGP(DGTPHUB, LOGL_NOTICE, "resolved addr %s\n",
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100106 osmo_hexdump((unsigned char*)&resolved_addr,
107 sizeof(resolved_addr)));
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100108
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100109 gtphub_resolved_ggsn(lookup->hub, lookup->apn_oi_str, &resolved_addr,
110 gtphub_now());
111
112remove_from_queue:
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100113 LOGP(DGTPHUB, LOGL_ERROR, "Removing GGSN lookup. (%p / %p)\n", lookup,
114 &lookup->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100115 expiring_item_del(&lookup->expiry_entry);
116}
117
118static 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);
Neels Hofmeyr93bafb62017-01-13 03:12:08 +0100124 osmo_strlcpy(lookup->apn_oi_str, apn_oi_str,
125 sizeof(lookup->apn_oi_str));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100126}
127
128static int start_ares_query(struct ggsn_lookup *lookup)
129{
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100130 LOGP(DGTPHUB, LOGL_DEBUG, "Going to query %s (%p / %p)\n",
131 lookup->apn_oi_str, lookup, &lookup->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100132
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100133 int rc = sgsn_ares_query(sgsn, lookup->apn_oi_str, ggsn_lookup_cb,
134 lookup);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100135 if (rc != 0)
136 LOGP(DGTPHUB, LOGL_ERROR, "Failed to start ares query.\n");
137 return rc;
138}
139
140static void ggsn_lookup_del_cb(struct expiring_item *expi)
141{
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100142 struct ggsn_lookup *lookup;
143 lookup = container_of(expi, struct ggsn_lookup, expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100144
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100145 LOGP(DGTPHUB, LOGL_NOTICE, "ggsn_lookup_del_cb(%p / %p)\n", lookup,
146 expi);
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100147
148 lookup->expiry_entry.del_cb = 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100149 expiring_item_del(expi);
150
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100151 llist_del(&lookup->entry);
152 talloc_free(lookup);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100153}
154
155struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
156 const char *imsi_str,
157 const char *apn_ni_str)
158{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100159 OSMO_ASSERT(imsi_str);
160 OSMO_ASSERT(apn_ni_str);
161
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100162 struct ggsn_lookup *lookup = talloc_zero(osmo_gtphub_ctx,
163 struct ggsn_lookup);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100164 OSMO_ASSERT(lookup);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100165
Neels Hofmeyrd9b1d492015-11-18 18:11:09 +0100166 LOGP(DGTPHUB, LOGL_DEBUG, "Request to resolve IMSI"
Neels Hofmeyr9cfe0372015-11-16 14:52:05 +0100167 " '%s' with APN-NI '%s' (%p / %p)\n",
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100168 imsi_str, apn_ni_str, lookup, &lookup->expiry_entry);
169
Neels Hofmeyr16c3f572015-11-11 17:27:01 +0100170 expiring_item_init(&lookup->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100171 lookup->hub = hub;
172
Neels Hofmeyr93bafb62017-01-13 03:12:08 +0100173 osmo_strlcpy(lookup->imsi_str, imsi_str, sizeof(lookup->imsi_str));
174 osmo_strlcpy(lookup->apn_ni_str, apn_ni_str,
175 sizeof(lookup->apn_ni_str));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100176
177 make_addr_str(lookup);
178
179 struct ggsn_lookup *active;
180 llist_for_each_entry(active, &hub->ggsn_lookups, entry) {
181 if (strncmp(active->apn_oi_str, lookup->apn_oi_str,
182 sizeof(lookup->apn_oi_str)) == 0) {
Neels Hofmeyrd9b1d492015-11-18 18:11:09 +0100183 LOGP(DGTPHUB, LOGL_DEBUG,
184 "Query already pending for %s\n",
185 lookup->apn_oi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100186 /* A query already pending. Just tip our hat. */
187 return NULL;
188 }
189 }
190
191 struct gtphub_resolved_ggsn *resolved;
192 llist_for_each_entry(resolved, &hub->resolved_ggsns, entry) {
193 if (strncmp(resolved->apn_oi_str, lookup->apn_oi_str,
194 sizeof(lookup->apn_oi_str)) == 0) {
Neels Hofmeyrd9b1d492015-11-18 18:11:09 +0100195 LOGP(DGTPHUB, LOGL_DEBUG,
196 "GGSN resolved from cache: %s -> %s\n",
197 lookup->apn_oi_str,
Neels Hofmeyrb6c2db52015-11-18 18:11:32 +0100198 gtphub_port_str(resolved->peer));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100199 return resolved->peer;
200 }
201 }
202
203 /* Kick off a resolution, but so far return nothing. The hope is that
204 * the peer will resend the request (a couple of times), and by then
205 * the GGSN will be resolved. */
Neels Hofmeyrd9b1d492015-11-18 18:11:09 +0100206 LOGP(DGTPHUB, LOGL_DEBUG,
207 "Sending out DNS query for %s..."
208 " (Returning failure, hoping for a retry once resolution"
209 " has concluded)\n",
210 lookup->apn_oi_str);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100211
212 llist_add(&lookup->entry, &hub->ggsn_lookups);
213
214 lookup->expiry_entry.del_cb = ggsn_lookup_del_cb;
Neels Hofmeyre54cd152015-11-24 13:31:06 +0100215 expiry_add(&hub->expire_quickly, &lookup->expiry_entry, gtphub_now());
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100216
217 start_ares_query(lookup);
218
219 return NULL;
220}