blob: 41b88adcbee2e04663217317e1c16b985e242b38 [file] [log] [blame]
Neels Hofmeyr3df5d532016-12-08 21:17:01 +01001/* Generic Subscriber Update Protocol client */
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +01002
Neels Hofmeyr3df5d532016-12-08 21:17:01 +01003/* (C) 2014-2016 by Sysmocom s.f.m.c. GmbH
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +01004 * All Rights Reserved
5 *
6 * Author: Jacob Erlbeck
Neels Hofmeyr3df5d532016-12-08 21:17:01 +01007 * Author: Neels Hofmeyr
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +01008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <openbsc/gprs_gsup_client.h>
25
26#include <osmocom/abis/ipa.h>
27#include <osmocom/gsm/protocol/ipaccess.h>
28#include <osmocom/core/msgb.h>
Harald Welte4f8e34b2016-05-06 23:27:38 +020029#include <osmocom/core/logging.h>
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +010030
31#include <openbsc/debug.h>
32
33#include <errno.h>
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010034#include <string.h>
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +010035
36extern void *tall_bsc_ctx;
37
Jacob Erlbeck03b46302014-12-19 19:18:54 +010038static void start_test_procedure(struct gprs_gsup_client *gsupc);
39
40static void gsup_client_send_ping(struct gprs_gsup_client *gsupc)
41{
42 struct msgb *msg = gprs_gsup_msgb_alloc();
43
44 msg->l2h = msgb_put(msg, 1);
45 msg->l2h[0] = IPAC_MSGT_PING;
46 ipa_msg_push_header(msg, IPAC_PROTO_IPACCESS);
47 ipa_client_conn_send(gsupc->link, msg);
48}
49
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010050static int gsup_client_connect(struct gprs_gsup_client *gsupc)
51{
52 int rc;
53
54 if (gsupc->is_connected)
55 return 0;
56
57 if (osmo_timer_pending(&gsupc->connect_timer)) {
58 LOGP(DLINP, LOGL_DEBUG,
59 "GSUP connect: connect timer already running\n");
60 osmo_timer_del(&gsupc->connect_timer);
61 }
62
Jacob Erlbeck03b46302014-12-19 19:18:54 +010063 if (osmo_timer_pending(&gsupc->ping_timer)) {
64 LOGP(DLINP, LOGL_DEBUG,
65 "GSUP connect: ping timer already running\n");
66 osmo_timer_del(&gsupc->ping_timer);
67 }
68
Jacob Erlbeck4188c302014-12-19 18:50:05 +010069 if (ipa_client_conn_clear_queue(gsupc->link) > 0)
70 LOGP(DLINP, LOGL_DEBUG, "GSUP connect: discarded stored messages\n");
71
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010072 rc = ipa_client_conn_open(gsupc->link);
73
Jacob Erlbeck69e16b92014-12-19 19:00:56 +010074 if (rc >= 0) {
75 LOGP(DGPRS, LOGL_INFO, "GSUP connecting to %s:%d\n",
76 gsupc->link->addr, gsupc->link->port);
77 return 0;
78 }
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010079
80 LOGP(DGPRS, LOGL_INFO, "GSUP failed to connect to %s:%d: %s\n",
81 gsupc->link->addr, gsupc->link->port, strerror(-rc));
82
83 if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT ||
84 rc == -EINVAL)
85 return rc;
86
87 osmo_timer_schedule(&gsupc->connect_timer, GPRS_GSUP_RECONNECT_INTERVAL, 0);
88
Jacob Erlbeck37184902015-01-19 10:56:15 +010089 LOGP(DGPRS, LOGL_INFO, "Scheduled timer to retry GSUP connect to %s:%d\n",
90 gsupc->link->addr, gsupc->link->port);
91
92 return 0;
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010093}
94
95static void connect_timer_cb(void *gsupc_)
96{
97 struct gprs_gsup_client *gsupc = gsupc_;
98
99 if (gsupc->is_connected)
100 return;
101
102 gsup_client_connect(gsupc);
103}
104
Neels Hofmeyr06cfe002015-10-01 15:23:51 +0200105static void gsup_client_send(struct gprs_gsup_client *gsupc, int proto_ext, struct msgb *msg_tx)
106{
107 ipa_prepend_header_ext(msg_tx, proto_ext);
108 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
109 ipa_client_conn_send(gsupc->link, msg_tx);
110 /* msg_tx is now queued and will be freed. */
111}
112
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200113static void gsup_client_oap_register(struct gprs_gsup_client *gsupc)
114{
115 struct msgb *msg_tx;
116 int rc;
117 rc = oap_register(&gsupc->oap_state, &msg_tx);
118
119 if ((rc < 0) || (!msg_tx)) {
120 LOGP(DGPRS, LOGL_ERROR, "GSUP OAP set up, but cannot register.\n");
121 return;
122 }
123
124 gsup_client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
125}
126
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100127static void gsup_client_updown_cb(struct ipa_client_conn *link, int up)
128{
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100129 struct gprs_gsup_client *gsupc = link->data;
130
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100131 LOGP(DGPRS, LOGL_INFO, "GSUP link to %s:%d %s\n",
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100132 link->addr, link->port, up ? "UP" : "DOWN");
133
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100134 gsupc->is_connected = up;
135
136 if (up) {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100137 start_test_procedure(gsupc);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100138
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200139 if (gsupc->oap_state.state == OAP_INITIALIZED)
140 gsup_client_oap_register(gsupc);
141
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100142 osmo_timer_del(&gsupc->connect_timer);
143 } else {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100144 osmo_timer_del(&gsupc->ping_timer);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100145
146 osmo_timer_schedule(&gsupc->connect_timer,
147 GPRS_GSUP_RECONNECT_INTERVAL, 0);
148 }
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100149}
150
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200151static int gsup_client_oap_handle(struct gprs_gsup_client *gsupc, struct msgb *msg_rx)
152{
153 int rc;
154 struct msgb *msg_tx;
155
156 rc = oap_handle(&gsupc->oap_state, msg_rx, &msg_tx);
157 msgb_free(msg_rx);
158 if (rc < 0)
159 return rc;
160
161 if (msg_tx)
162 gsup_client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
163
164 return 0;
165}
166
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100167static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg)
168{
169 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
170 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
171 struct gprs_gsup_client *gsupc = (struct gprs_gsup_client *)link->data;
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100172 int rc;
173 static struct ipaccess_unit ipa_dev = {
174 .unit_name = "SGSN"
175 };
176
177 msg->l2h = &hh->data[0];
178
179 rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg);
180
181 if (rc < 0) {
182 LOGP(DGPRS, LOGL_NOTICE,
183 "GSUP received an invalid IPA/CCM message from %s:%d\n",
184 link->addr, link->port);
185 /* Link has been closed */
186 gsupc->is_connected = 0;
187 msgb_free(msg);
188 return -1;
189 }
190
191 if (rc == 1) {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100192 uint8_t msg_type = *(msg->l2h);
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100193 /* CCM message */
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100194 if (msg_type == IPAC_MSGT_PONG) {
195 LOGP(DGPRS, LOGL_DEBUG, "GSUP receiving PONG\n");
196 gsupc->got_ipa_pong = 1;
197 }
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100198
199 msgb_free(msg);
200 return 0;
201 }
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100202
203 if (hh->proto != IPAC_PROTO_OSMO)
204 goto invalid;
205
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200206 if (!he || msgb_l2len(msg) < sizeof(*he))
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100207 goto invalid;
208
209 msg->l2h = &he->data[0];
210
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200211 if (he->proto == IPAC_PROTO_EXT_GSUP) {
212 OSMO_ASSERT(gsupc->read_cb != NULL);
213 gsupc->read_cb(gsupc, msg);
214 /* expecting read_cb() to free msg */
215 } else if (he->proto == IPAC_PROTO_EXT_OAP) {
216 return gsup_client_oap_handle(gsupc, msg);
217 /* gsup_client_oap_handle frees msg */
218 } else
219 goto invalid;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100220
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100221 return 0;
222
223invalid:
224 LOGP(DGPRS, LOGL_NOTICE,
225 "GSUP received an invalid IPA message from %s:%d, size = %d\n",
226 link->addr, link->port, msgb_length(msg));
227
228 msgb_free(msg);
229 return -1;
230}
231
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100232static void ping_timer_cb(void *gsupc_)
233{
234 struct gprs_gsup_client *gsupc = gsupc_;
235
236 LOGP(DGPRS, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n",
237 gsupc->is_connected ? "connected" : "not connected",
238 gsupc->got_ipa_pong ? "got" : "didn't get");
239
240 if (gsupc->got_ipa_pong) {
241 start_test_procedure(gsupc);
242 return;
243 }
244
245 LOGP(DGPRS, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n");
246 ipa_client_conn_close(gsupc->link);
247 gsupc->is_connected = 0;
248
249 gsup_client_connect(gsupc);
250}
251
252static void start_test_procedure(struct gprs_gsup_client *gsupc)
253{
254 gsupc->ping_timer.data = gsupc;
255 gsupc->ping_timer.cb = &ping_timer_cb;
256
257 gsupc->got_ipa_pong = 0;
258 osmo_timer_schedule(&gsupc->ping_timer, GPRS_GSUP_PING_INTERVAL, 0);
259 LOGP(DGPRS, LOGL_DEBUG, "GSUP sending PING\n");
260 gsup_client_send_ping(gsupc);
261}
262
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100263struct gprs_gsup_client *gprs_gsup_client_create(const char *ip_addr,
264 unsigned int tcp_port,
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200265 gprs_gsup_read_cb_t read_cb,
266 struct oap_config *oap_config)
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100267{
268 struct gprs_gsup_client *gsupc;
269 int rc;
270
271 gsupc = talloc_zero(tall_bsc_ctx, struct gprs_gsup_client);
272 OSMO_ASSERT(gsupc);
273
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200274 rc = oap_init(oap_config, &gsupc->oap_state);
275 if (rc != 0)
276 goto failed;
277
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100278 gsupc->link = ipa_client_conn_create(gsupc,
279 /* no e1inp */ NULL,
280 0,
281 ip_addr, tcp_port,
282 gsup_client_updown_cb,
283 gsup_client_read_cb,
284 /* default write_cb */ NULL,
285 gsupc);
286 if (!gsupc->link)
287 goto failed;
288
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100289 gsupc->connect_timer.data = gsupc;
290 gsupc->connect_timer.cb = &connect_timer_cb;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100291
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100292 rc = gsup_client_connect(gsupc);
293
Jacob Erlbeck3ee67ff2015-01-26 09:22:39 +0100294 if (rc < 0)
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100295 goto failed;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100296
297 gsupc->read_cb = read_cb;
298
299 return gsupc;
300
301failed:
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100302 gprs_gsup_client_destroy(gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100303 return NULL;
304}
305
306void gprs_gsup_client_destroy(struct gprs_gsup_client *gsupc)
307{
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100308 osmo_timer_del(&gsupc->connect_timer);
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100309 osmo_timer_del(&gsupc->ping_timer);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100310
311 if (gsupc->link) {
312 ipa_client_conn_close(gsupc->link);
313 ipa_client_conn_destroy(gsupc->link);
314 gsupc->link = NULL;
315 }
316 talloc_free(gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100317}
318
319int gprs_gsup_client_send(struct gprs_gsup_client *gsupc, struct msgb *msg)
320{
321 if (!gsupc) {
322 msgb_free(msg);
323 return -ENOTCONN;
324 }
325
Jacob Erlbeck4188c302014-12-19 18:50:05 +0100326 if (!gsupc->is_connected) {
327 msgb_free(msg);
328 return -EAGAIN;
329 }
330
Neels Hofmeyr06cfe002015-10-01 15:23:51 +0200331 gsup_client_send(gsupc, IPAC_PROTO_EXT_GSUP, msg);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100332
333 return 0;
334}
335
336struct msgb *gprs_gsup_msgb_alloc(void)
337{
338 return msgb_alloc_headroom(4000, 64, __func__);
339}