blob: 386ee131864122e9916d961abdd7a150dc8dfc4d [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;
67
68 if (status != ARES_SUCCESS) {
69 LOGP(DGTPHUB, LOGL_ERROR, "DNS query failed.\n");
70
71 /* Need to try with three digits now */
72 if (!lookup->have_3dig_mnc) {
73 lookup->have_3dig_mnc = 1;
74 if (start_ares_query(lookup) == 0)
75 return;
76 }
77
78 LOGP(DGTPHUB, LOGL_ERROR, "Failed to resolve GGSN.\n");
79 goto remove_from_queue;
80 }
81
82 struct gsn_addr resolved_addr;
83 if (hostent->h_length > sizeof(resolved_addr.buf)) {
84 LOGP(DGTPHUB, LOGL_ERROR, "Addr size too large: %d > %d\n",
85 (int)hostent->h_length, (int)sizeof(resolved_addr.buf));
86 goto remove_from_queue;
87 }
88
89 /* Get the first addr from the list */
90 char *addr0 = hostent->h_addr_list[0];
91 if (!addr0) {
92 LOGP(DGTPHUB, LOGL_ERROR, "No host address.\n");
93 goto remove_from_queue;
94 }
95
96 memcpy(&resolved_addr.buf, addr0, hostent->h_length);
97 resolved_addr.len = hostent->h_length;
98
99 gtphub_resolved_ggsn(lookup->hub, lookup->apn_oi_str, &resolved_addr,
100 gtphub_now());
101
102remove_from_queue:
103 expiring_item_del(&lookup->expiry_entry);
104}
105
106static void make_addr_str(struct ggsn_lookup *lookup)
107{
108 char *apn_oi_str;
109 apn_oi_str = osmo_apn_qualify_from_imsi(lookup->imsi_str,
110 lookup->apn_ni_str,
111 lookup->have_3dig_mnc);
112 strncpy(lookup->apn_oi_str, apn_oi_str, sizeof(lookup->apn_oi_str));
113 lookup->apn_oi_str[sizeof(lookup->apn_oi_str)-1] = '\0';
114}
115
116static int start_ares_query(struct ggsn_lookup *lookup)
117{
118 LOGP(DGTPHUB, LOGL_DEBUG, "Going to query %s\n", lookup->apn_oi_str);
119
120 int rc = sgsn_ares_query(sgsn, lookup->apn_oi_str, ggsn_lookup_cb, &lookup);
121 if (rc != 0)
122 LOGP(DGTPHUB, LOGL_ERROR, "Failed to start ares query.\n");
123 return rc;
124}
125
126static void ggsn_lookup_del_cb(struct expiring_item *expi)
127{
128 struct ggsn_lookup *ggsn;
129 ggsn = container_of(expi, struct ggsn_lookup, expiry_entry);
130
131 ggsn->expiry_entry.del_cb = 0;
132 expiring_item_del(expi);
133
134 llist_del(&ggsn->entry);
135 talloc_free(ggsn);
136}
137
138struct gtphub_peer_port *gtphub_resolve_ggsn_addr(struct gtphub *hub,
139 const char *imsi_str,
140 const char *apn_ni_str)
141{
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100142 LOGP(DGTPHUB, LOGL_NOTICE, "Request to resolve IMSI '%s' with APN-NI '%s'\n",
143 imsi_str, apn_ni_str);
144 OSMO_ASSERT(imsi_str);
145 OSMO_ASSERT(apn_ni_str);
146
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100147 struct ggsn_lookup *lookup = talloc_zero(osmo_gtphub_ctx, struct ggsn_lookup);
Neels Hofmeyr5b664f42015-11-10 20:32:13 +0100148 OSMO_ASSERT(lookup);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100149
150 lookup->hub = hub;
151
152 strncpy(lookup->imsi_str, imsi_str, sizeof(lookup->imsi_str));
153 lookup->imsi_str[sizeof(lookup->imsi_str)-1] = '\0';
154
155 strncpy(lookup->apn_ni_str, apn_ni_str, sizeof(lookup->apn_ni_str));
156 lookup->apn_ni_str[sizeof(lookup->apn_ni_str)-1] = '\0';
157
158 make_addr_str(lookup);
159
160 struct ggsn_lookup *active;
161 llist_for_each_entry(active, &hub->ggsn_lookups, entry) {
162 if (strncmp(active->apn_oi_str, lookup->apn_oi_str,
163 sizeof(lookup->apn_oi_str)) == 0) {
164 /* A query already pending. Just tip our hat. */
165 return NULL;
166 }
167 }
168
169 struct gtphub_resolved_ggsn *resolved;
170 llist_for_each_entry(resolved, &hub->resolved_ggsns, entry) {
171 if (strncmp(resolved->apn_oi_str, lookup->apn_oi_str,
172 sizeof(lookup->apn_oi_str)) == 0) {
173 /* Already resolved. */
174 return resolved->peer;
175 }
176 }
177
178 /* Kick off a resolution, but so far return nothing. The hope is that
179 * the peer will resend the request (a couple of times), and by then
180 * the GGSN will be resolved. */
181
182 llist_add(&lookup->entry, &hub->ggsn_lookups);
183
184 lookup->expiry_entry.del_cb = ggsn_lookup_del_cb;
185 expiry_add(&hub->expire_seq_maps, &lookup->expiry_entry, gtphub_now());
186
187 start_ares_query(lookup);
188
189 return NULL;
190}