blob: 0d66273508d974550dd2b69a9399d9eb80ae8561 [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 *
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 Hofmeyr30f7bcb2015-11-08 20:34:47 +010027#include <unistd.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020028
29#include <openbsc/gtphub.h>
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010030#include <openbsc/debug.h>
31
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020032#include <osmocom/core/utils.h>
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010033#include <osmocom/gsm/apn.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020034
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010035/* TODO split GRX ares from sgsn into a separate struct and allow use without
36 * globals. */
37#include <openbsc/sgsn.h>
38extern struct sgsn_instance *sgsn;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020039
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010040struct sgsn_instance sgsn_inst = { 0 };
41struct sgsn_instance *sgsn = &sgsn_inst;
42
43extern void *osmo_gtphub_ctx;
44
45int gtphub_ares_init(struct gtphub *hub)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020046{
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010047 return sgsn_ares_init(sgsn);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020048}
49
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010050struct 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
62static int start_ares_query(struct ggsn_lookup *lookup);
63
64static void ggsn_lookup_cb(void *arg, int status, int timeouts, struct hostent *hostent)
65{
66 struct ggsn_lookup *lookup = arg;
Neels Hofmeyr3317c842015-11-11 17:20:42 +010067 LOGP(DGTPHUB, LOGL_NOTICE, "ggsn_lookup_cb(%p / %p)", lookup, &lookup->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010068
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 Hofmeyr3317c842015-11-11 17:20:42 +010079 LOGP(DGTPHUB, LOGL_ERROR, "Failed to resolve GGSN. (%p)\n", lookup);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +010080 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
97 memcpy(&resolved_addr.buf, addr0, hostent->h_length);
98 resolved_addr.len = hostent->h_length;
99
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100100 LOGP(DGTPHUB, LOGL_NOTICE, "resolved addr %s\n",
101 osmo_hexdump(&resolved_addr, sizeof(resolved_addr)));
102
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100103 gtphub_resolved_ggsn(lookup->hub, lookup->apn_oi_str, &resolved_addr,
104 gtphub_now());
105
106remove_from_queue:
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100107 LOGP(DGTPHUB, LOGL_ERROR, "Removing GGSN lookup. (%p / %p)\n", lookup, &lookup->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100108 expiring_item_del(&lookup->expiry_entry);
109}
110
111static 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
121static int start_ares_query(struct ggsn_lookup *lookup)
122{
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100123 LOGP(DGTPHUB, LOGL_DEBUG, "Going to query %s (%p / %p)\n", lookup->apn_oi_str, lookup, &lookup->expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100124
125 int rc = sgsn_ares_query(sgsn, lookup->apn_oi_str, ggsn_lookup_cb, &lookup);
126 if (rc != 0)
127 LOGP(DGTPHUB, LOGL_ERROR, "Failed to start ares query.\n");
128 return rc;
129}
130
131static void ggsn_lookup_del_cb(struct expiring_item *expi)
132{
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100133 struct ggsn_lookup *lookup;
134 lookup = container_of(expi, struct ggsn_lookup, expiry_entry);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100135
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100136 LOGP(DGTPHUB, LOGL_NOTICE, "ggsn_lookup_del_cb(%p / %p)\n", lookup, expi);
137
138 lookup->expiry_entry.del_cb = 0;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100139 expiring_item_del(expi);
140
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100141 llist_del(&lookup->entry);
142 talloc_free(lookup);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100143}
144
145struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
146 const char *imsi_str,
147 const char *apn_ni_str)
148{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100149 OSMO_ASSERT(imsi_str);
150 OSMO_ASSERT(apn_ni_str);
151
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100152 struct ggsn_lookup *lookup = talloc_zero(osmo_gtphub_ctx, struct ggsn_lookup);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100153 OSMO_ASSERT(lookup);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100154
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100155 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 Hofmeyr30f7bcb2015-11-08 20:34:47 +0100158 lookup->hub = hub;
159
160 strncpy(lookup->imsi_str, imsi_str, sizeof(lookup->imsi_str));
161 lookup->imsi_str[sizeof(lookup->imsi_str)-1] = '\0';
162
163 strncpy(lookup->apn_ni_str, apn_ni_str, sizeof(lookup->apn_ni_str));
164 lookup->apn_ni_str[sizeof(lookup->apn_ni_str)-1] = '\0';
165
166 make_addr_str(lookup);
167
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100168 LOGP(DGTPHUB, LOGL_NOTICE, "looking for active queries...\n");
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100169 struct ggsn_lookup *active;
170 llist_for_each_entry(active, &hub->ggsn_lookups, entry) {
171 if (strncmp(active->apn_oi_str, lookup->apn_oi_str,
172 sizeof(lookup->apn_oi_str)) == 0) {
173 /* A query already pending. Just tip our hat. */
174 return NULL;
175 }
176 }
177
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100178 LOGP(DGTPHUB, LOGL_NOTICE, "looking for already resolved GGSNs...\n");
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100179 struct gtphub_resolved_ggsn *resolved;
180 llist_for_each_entry(resolved, &hub->resolved_ggsns, entry) {
181 if (strncmp(resolved->apn_oi_str, lookup->apn_oi_str,
182 sizeof(lookup->apn_oi_str)) == 0) {
183 /* Already resolved. */
184 return resolved->peer;
185 }
186 }
187
188 /* Kick off a resolution, but so far return nothing. The hope is that
189 * the peer will resend the request (a couple of times), and by then
190 * the GGSN will be resolved. */
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100191 LOGP(DGTPHUB, LOGL_NOTICE, "kick off resolution.\n");
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100192
193 llist_add(&lookup->entry, &hub->ggsn_lookups);
194
195 lookup->expiry_entry.del_cb = ggsn_lookup_del_cb;
196 expiry_add(&hub->expire_seq_maps, &lookup->expiry_entry, gtphub_now());
197
198 start_ares_query(lookup);
Neels Hofmeyr3317c842015-11-11 17:20:42 +0100199 LOGP(DGTPHUB, LOGL_NOTICE, "resolution started.\n");
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100200
201 return NULL;
202}