blob: deaf15622d06de9464b233f79e9173c47e97a054 [file] [log] [blame]
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +01001/* GPRS Subscriber Update Protocol client */
2
3/* (C) 2014 by Sysmocom s.f.m.c. GmbH
4 * All Rights Reserved
5 *
6 * Author: Jacob Erlbeck
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <openbsc/gprs_gsup_client.h>
24
25#include <osmocom/abis/ipa.h>
26#include <osmocom/gsm/protocol/ipaccess.h>
27#include <osmocom/core/msgb.h>
Harald Welteabc65052016-05-06 23:27:38 +020028#include <osmocom/core/logging.h>
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +010029
30#include <openbsc/debug.h>
31
32#include <errno.h>
Jacob Erlbeck58f75c22014-12-18 15:00:29 +010033#include <string.h>
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +010034
35extern void *tall_bsc_ctx;
36
Jacob Erlbecka93c91c2014-12-19 19:18:54 +010037static void start_test_procedure(struct gprs_gsup_client *gsupc);
38
39static void gsup_client_send_ping(struct gprs_gsup_client *gsupc)
40{
41 struct msgb *msg = gprs_gsup_msgb_alloc();
42
43 msg->l2h = msgb_put(msg, 1);
44 msg->l2h[0] = IPAC_MSGT_PING;
45 ipa_msg_push_header(msg, IPAC_PROTO_IPACCESS);
46 ipa_client_conn_send(gsupc->link, msg);
47}
48
Jacob Erlbeck58f75c22014-12-18 15:00:29 +010049static int gsup_client_connect(struct gprs_gsup_client *gsupc)
50{
51 int rc;
52
53 if (gsupc->is_connected)
54 return 0;
55
56 if (osmo_timer_pending(&gsupc->connect_timer)) {
57 LOGP(DLINP, LOGL_DEBUG,
58 "GSUP connect: connect timer already running\n");
59 osmo_timer_del(&gsupc->connect_timer);
60 }
61
Jacob Erlbecka93c91c2014-12-19 19:18:54 +010062 if (osmo_timer_pending(&gsupc->ping_timer)) {
63 LOGP(DLINP, LOGL_DEBUG,
64 "GSUP connect: ping timer already running\n");
65 osmo_timer_del(&gsupc->ping_timer);
66 }
67
Jacob Erlbeckad8b7e02014-12-19 18:50:05 +010068 if (ipa_client_conn_clear_queue(gsupc->link) > 0)
69 LOGP(DLINP, LOGL_DEBUG, "GSUP connect: discarded stored messages\n");
70
Jacob Erlbeck58f75c22014-12-18 15:00:29 +010071 rc = ipa_client_conn_open(gsupc->link);
72
Jacob Erlbeck89a3b2b2014-12-19 19:00:56 +010073 if (rc >= 0) {
74 LOGP(DGPRS, LOGL_INFO, "GSUP connecting to %s:%d\n",
75 gsupc->link->addr, gsupc->link->port);
76 return 0;
77 }
Jacob Erlbeck58f75c22014-12-18 15:00:29 +010078
79 LOGP(DGPRS, LOGL_INFO, "GSUP failed to connect to %s:%d: %s\n",
80 gsupc->link->addr, gsupc->link->port, strerror(-rc));
81
82 if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT ||
83 rc == -EINVAL)
84 return rc;
85
86 osmo_timer_schedule(&gsupc->connect_timer, GPRS_GSUP_RECONNECT_INTERVAL, 0);
87
Jacob Erlbeck01c4da82015-01-19 10:56:15 +010088 LOGP(DGPRS, LOGL_INFO, "Scheduled timer to retry GSUP connect to %s:%d\n",
89 gsupc->link->addr, gsupc->link->port);
90
91 return 0;
Jacob Erlbeck58f75c22014-12-18 15:00:29 +010092}
93
94static void connect_timer_cb(void *gsupc_)
95{
96 struct gprs_gsup_client *gsupc = gsupc_;
97
98 if (gsupc->is_connected)
99 return;
100
101 gsup_client_connect(gsupc);
102}
103
Neels Hofmeyr427a4af2015-10-01 15:23:51 +0200104static void gsup_client_send(struct gprs_gsup_client *gsupc, int proto_ext, struct msgb *msg_tx)
105{
106 ipa_prepend_header_ext(msg_tx, proto_ext);
107 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
108 ipa_client_conn_send(gsupc->link, msg_tx);
109 /* msg_tx is now queued and will be freed. */
110}
111
Neels Hofmeyr38b5e272015-10-12 11:57:37 +0200112static void gsup_client_oap_register(struct gprs_gsup_client *gsupc)
113{
114 struct msgb *msg_tx;
115 int rc;
116 rc = oap_register(&gsupc->oap_state, &msg_tx);
117
118 if ((rc < 0) || (!msg_tx)) {
119 LOGP(DGPRS, LOGL_ERROR, "GSUP OAP set up, but cannot register.\n");
120 return;
121 }
122
123 gsup_client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
124}
125
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100126static void gsup_client_updown_cb(struct ipa_client_conn *link, int up)
127{
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100128 struct gprs_gsup_client *gsupc = link->data;
129
Jacob Erlbecka93c91c2014-12-19 19:18:54 +0100130 LOGP(DGPRS, LOGL_INFO, "GSUP link to %s:%d %s\n",
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100131 link->addr, link->port, up ? "UP" : "DOWN");
132
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100133 gsupc->is_connected = up;
134
135 if (up) {
Jacob Erlbecka93c91c2014-12-19 19:18:54 +0100136 start_test_procedure(gsupc);
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100137
Neels Hofmeyr38b5e272015-10-12 11:57:37 +0200138 if (gsupc->oap_state.state == OAP_INITIALIZED)
139 gsup_client_oap_register(gsupc);
140
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100141 osmo_timer_del(&gsupc->connect_timer);
142 } else {
Jacob Erlbecka93c91c2014-12-19 19:18:54 +0100143 osmo_timer_del(&gsupc->ping_timer);
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100144
145 osmo_timer_schedule(&gsupc->connect_timer,
146 GPRS_GSUP_RECONNECT_INTERVAL, 0);
147 }
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100148}
149
Neels Hofmeyr38b5e272015-10-12 11:57:37 +0200150static int gsup_client_oap_handle(struct gprs_gsup_client *gsupc, struct msgb *msg_rx)
151{
152 int rc;
153 struct msgb *msg_tx;
154
155 rc = oap_handle(&gsupc->oap_state, msg_rx, &msg_tx);
156 msgb_free(msg_rx);
157 if (rc < 0)
158 return rc;
159
160 if (msg_tx)
161 gsup_client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
162
163 return 0;
164}
165
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100166static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg)
167{
168 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
169 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
170 struct gprs_gsup_client *gsupc = (struct gprs_gsup_client *)link->data;
Jacob Erlbeckedb67a12014-12-19 19:15:55 +0100171 int rc;
172 static struct ipaccess_unit ipa_dev = {
173 .unit_name = "SGSN"
174 };
175
176 msg->l2h = &hh->data[0];
177
178 rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg);
179
180 if (rc < 0) {
181 LOGP(DGPRS, LOGL_NOTICE,
182 "GSUP received an invalid IPA/CCM message from %s:%d\n",
183 link->addr, link->port);
184 /* Link has been closed */
185 gsupc->is_connected = 0;
186 msgb_free(msg);
187 return -1;
188 }
189
190 if (rc == 1) {
Jacob Erlbecka93c91c2014-12-19 19:18:54 +0100191 uint8_t msg_type = *(msg->l2h);
Jacob Erlbeckedb67a12014-12-19 19:15:55 +0100192 /* CCM message */
Jacob Erlbecka93c91c2014-12-19 19:18:54 +0100193 if (msg_type == IPAC_MSGT_PONG) {
194 LOGP(DGPRS, LOGL_DEBUG, "GSUP receiving PONG\n");
195 gsupc->got_ipa_pong = 1;
196 }
Jacob Erlbeckedb67a12014-12-19 19:15:55 +0100197
198 msgb_free(msg);
199 return 0;
200 }
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100201
202 if (hh->proto != IPAC_PROTO_OSMO)
203 goto invalid;
204
Neels Hofmeyr38b5e272015-10-12 11:57:37 +0200205 if (!he || msgb_l2len(msg) < sizeof(*he))
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100206 goto invalid;
207
208 msg->l2h = &he->data[0];
209
Neels Hofmeyr38b5e272015-10-12 11:57:37 +0200210 if (he->proto == IPAC_PROTO_EXT_GSUP) {
211 OSMO_ASSERT(gsupc->read_cb != NULL);
212 gsupc->read_cb(gsupc, msg);
213 /* expecting read_cb() to free msg */
214 } else if (he->proto == IPAC_PROTO_EXT_OAP) {
215 return gsup_client_oap_handle(gsupc, msg);
216 /* gsup_client_oap_handle frees msg */
217 } else
218 goto invalid;
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100219
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100220 return 0;
221
222invalid:
223 LOGP(DGPRS, LOGL_NOTICE,
224 "GSUP received an invalid IPA message from %s:%d, size = %d\n",
225 link->addr, link->port, msgb_length(msg));
226
227 msgb_free(msg);
228 return -1;
229}
230
Jacob Erlbecka93c91c2014-12-19 19:18:54 +0100231static void ping_timer_cb(void *gsupc_)
232{
233 struct gprs_gsup_client *gsupc = gsupc_;
234
235 LOGP(DGPRS, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n",
236 gsupc->is_connected ? "connected" : "not connected",
237 gsupc->got_ipa_pong ? "got" : "didn't get");
238
239 if (gsupc->got_ipa_pong) {
240 start_test_procedure(gsupc);
241 return;
242 }
243
244 LOGP(DGPRS, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n");
245 ipa_client_conn_close(gsupc->link);
246 gsupc->is_connected = 0;
247
248 gsup_client_connect(gsupc);
249}
250
251static void start_test_procedure(struct gprs_gsup_client *gsupc)
252{
253 gsupc->ping_timer.data = gsupc;
254 gsupc->ping_timer.cb = &ping_timer_cb;
255
256 gsupc->got_ipa_pong = 0;
257 osmo_timer_schedule(&gsupc->ping_timer, GPRS_GSUP_PING_INTERVAL, 0);
258 LOGP(DGPRS, LOGL_DEBUG, "GSUP sending PING\n");
259 gsup_client_send_ping(gsupc);
260}
261
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100262struct gprs_gsup_client *gprs_gsup_client_create(const char *ip_addr,
263 unsigned int tcp_port,
Neels Hofmeyr38b5e272015-10-12 11:57:37 +0200264 gprs_gsup_read_cb_t read_cb,
265 struct oap_config *oap_config)
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100266{
267 struct gprs_gsup_client *gsupc;
268 int rc;
269
270 gsupc = talloc_zero(tall_bsc_ctx, struct gprs_gsup_client);
271 OSMO_ASSERT(gsupc);
272
Neels Hofmeyr38b5e272015-10-12 11:57:37 +0200273 rc = oap_init(oap_config, &gsupc->oap_state);
274 if (rc != 0)
275 goto failed;
276
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100277 gsupc->link = ipa_client_conn_create(gsupc,
278 /* no e1inp */ NULL,
279 0,
280 ip_addr, tcp_port,
281 gsup_client_updown_cb,
282 gsup_client_read_cb,
283 /* default write_cb */ NULL,
284 gsupc);
285 if (!gsupc->link)
286 goto failed;
287
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100288 gsupc->connect_timer.data = gsupc;
289 gsupc->connect_timer.cb = &connect_timer_cb;
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100290
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100291 rc = gsup_client_connect(gsupc);
292
Jacob Erlbeck4e20fb32015-01-26 09:22:39 +0100293 if (rc < 0)
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100294 goto failed;
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100295
296 gsupc->read_cb = read_cb;
297
298 return gsupc;
299
300failed:
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100301 gprs_gsup_client_destroy(gsupc);
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100302 return NULL;
303}
304
305void gprs_gsup_client_destroy(struct gprs_gsup_client *gsupc)
306{
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100307 osmo_timer_del(&gsupc->connect_timer);
Jacob Erlbecka93c91c2014-12-19 19:18:54 +0100308 osmo_timer_del(&gsupc->ping_timer);
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100309
310 if (gsupc->link) {
311 ipa_client_conn_close(gsupc->link);
312 ipa_client_conn_destroy(gsupc->link);
313 gsupc->link = NULL;
314 }
315 talloc_free(gsupc);
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100316}
317
318int gprs_gsup_client_send(struct gprs_gsup_client *gsupc, struct msgb *msg)
319{
320 if (!gsupc) {
321 msgb_free(msg);
322 return -ENOTCONN;
323 }
324
Jacob Erlbeckad8b7e02014-12-19 18:50:05 +0100325 if (!gsupc->is_connected) {
326 msgb_free(msg);
327 return -EAGAIN;
328 }
329
Neels Hofmeyr427a4af2015-10-01 15:23:51 +0200330 gsup_client_send(gsupc, IPAC_PROTO_EXT_GSUP, msg);
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100331
332 return 0;
333}
334
335struct msgb *gprs_gsup_msgb_alloc(void)
336{
337 return msgb_alloc_headroom(4000, 64, __func__);
338}