blob: 4f76efc3d90429bea266b9b5824579c9f82883ac [file] [log] [blame]
Harald Welteec6915a2018-07-23 14:25:33 +02001/* Generic Subscriber Update Protocol client */
2
3/* (C) 2014-2016 by Sysmocom s.f.m.c. GmbH
4 * All Rights Reserved
5 *
6 * Author: Jacob Erlbeck
7 * Author: Neels Hofmeyr
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte0da9f2f2018-08-13 20:42:36 +020010 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
Harald Welteec6915a2018-07-23 14:25:33 +020012 * (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
Harald Welte0da9f2f2018-08-13 20:42:36 +020017 * GNU General Public License for more details.
Harald Welteec6915a2018-07-23 14:25:33 +020018 *
Harald Welte0da9f2f2018-08-13 20:42:36 +020019 * You should have received a copy of the GNU General Public License
Harald Welteec6915a2018-07-23 14:25:33 +020020 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <osmocom/gsupclient/gsup_client.h>
25
26#include <osmocom/abis/ipa.h>
27#include <osmocom/gsm/oap_client.h>
28#include <osmocom/gsm/protocol/ipaccess.h>
29#include <osmocom/core/msgb.h>
30#include <osmocom/core/logging.h>
31
32#include <errno.h>
33#include <string.h>
34
Harald Welte953d27c2018-07-23 11:11:22 +020035static void start_test_procedure(struct osmo_gsup_client *gsupc);
Harald Welteec6915a2018-07-23 14:25:33 +020036
Harald Welte953d27c2018-07-23 11:11:22 +020037static void gsup_client_send_ping(struct osmo_gsup_client *gsupc)
Harald Welteec6915a2018-07-23 14:25:33 +020038{
Harald Welte953d27c2018-07-23 11:11:22 +020039 struct msgb *msg = osmo_gsup_client_msgb_alloc();
Harald Welteec6915a2018-07-23 14:25:33 +020040
41 msg->l2h = msgb_put(msg, 1);
42 msg->l2h[0] = IPAC_MSGT_PING;
43 ipa_msg_push_header(msg, IPAC_PROTO_IPACCESS);
44 ipa_client_conn_send(gsupc->link, msg);
45}
46
Harald Welte953d27c2018-07-23 11:11:22 +020047static int gsup_client_connect(struct osmo_gsup_client *gsupc)
Harald Welteec6915a2018-07-23 14:25:33 +020048{
49 int rc;
50
51 if (gsupc->is_connected)
52 return 0;
53
54 if (osmo_timer_pending(&gsupc->connect_timer)) {
55 LOGP(DLGSUP, LOGL_DEBUG,
56 "GSUP connect: connect timer already running\n");
57 osmo_timer_del(&gsupc->connect_timer);
58 }
59
60 if (osmo_timer_pending(&gsupc->ping_timer)) {
61 LOGP(DLGSUP, LOGL_DEBUG,
62 "GSUP connect: ping timer already running\n");
63 osmo_timer_del(&gsupc->ping_timer);
64 }
65
66 if (ipa_client_conn_clear_queue(gsupc->link) > 0)
67 LOGP(DLGSUP, LOGL_DEBUG, "GSUP connect: discarded stored messages\n");
68
69 rc = ipa_client_conn_open(gsupc->link);
70
71 if (rc >= 0) {
72 LOGP(DLGSUP, LOGL_NOTICE, "GSUP connecting to %s:%d\n",
73 gsupc->link->addr, gsupc->link->port);
74 return 0;
75 }
76
77 LOGP(DLGSUP, LOGL_ERROR, "GSUP failed to connect to %s:%d: %s\n",
78 gsupc->link->addr, gsupc->link->port, strerror(-rc));
79
80 if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT ||
81 rc == -EINVAL)
82 return rc;
83
84 osmo_timer_schedule(&gsupc->connect_timer,
Harald Welte953d27c2018-07-23 11:11:22 +020085 OSMO_GSUP_CLIENT_RECONNECT_INTERVAL, 0);
Harald Welteec6915a2018-07-23 14:25:33 +020086
87 LOGP(DLGSUP, LOGL_INFO, "Scheduled timer to retry GSUP connect to %s:%d\n",
88 gsupc->link->addr, gsupc->link->port);
89
90 return 0;
91}
92
93static void connect_timer_cb(void *gsupc_)
94{
Harald Welte953d27c2018-07-23 11:11:22 +020095 struct osmo_gsup_client *gsupc = gsupc_;
Harald Welteec6915a2018-07-23 14:25:33 +020096
97 if (gsupc->is_connected)
98 return;
99
Neels Hofmeyr82112ea2019-11-25 03:59:44 +0100100 if (gsupc->up_down_cb) {
101 /* When the up_down_cb() returns false, the user asks us not to retry connecting. */
102 if (!gsupc->up_down_cb(gsupc, false))
103 return;
104 }
105
Harald Welteec6915a2018-07-23 14:25:33 +0200106 gsup_client_connect(gsupc);
107}
108
Harald Welte953d27c2018-07-23 11:11:22 +0200109static void client_send(struct osmo_gsup_client *gsupc, int proto_ext,
Harald Welteec6915a2018-07-23 14:25:33 +0200110 struct msgb *msg_tx)
111{
112 ipa_prepend_header_ext(msg_tx, proto_ext);
113 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
114 ipa_client_conn_send(gsupc->link, msg_tx);
115 /* msg_tx is now queued and will be freed. */
116}
117
Harald Welte953d27c2018-07-23 11:11:22 +0200118static void gsup_client_oap_register(struct osmo_gsup_client *gsupc)
Harald Welteec6915a2018-07-23 14:25:33 +0200119{
120 struct msgb *msg_tx;
121 int rc;
122 rc = osmo_oap_client_register(&gsupc->oap_state, &msg_tx);
123
124 if ((rc < 0) || (!msg_tx)) {
125 LOGP(DLGSUP, LOGL_ERROR, "GSUP OAP set up, but cannot register.\n");
126 return;
127 }
128
129 client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
130}
131
132static void gsup_client_updown_cb(struct ipa_client_conn *link, int up)
133{
Harald Welte953d27c2018-07-23 11:11:22 +0200134 struct osmo_gsup_client *gsupc = link->data;
Harald Welteec6915a2018-07-23 14:25:33 +0200135
136 LOGP(DLGSUP, LOGL_INFO, "GSUP link to %s:%d %s\n",
137 link->addr, link->port, up ? "UP" : "DOWN");
138
139 gsupc->is_connected = up;
140
141 if (up) {
142 start_test_procedure(gsupc);
143
144 if (gsupc->oap_state.state == OSMO_OAP_INITIALIZED)
145 gsup_client_oap_register(gsupc);
146
147 osmo_timer_del(&gsupc->connect_timer);
Neels Hofmeyr82112ea2019-11-25 03:59:44 +0100148
149 if (gsupc->up_down_cb)
150 gsupc->up_down_cb(gsupc, true);
Harald Welteec6915a2018-07-23 14:25:33 +0200151 } else {
152 osmo_timer_del(&gsupc->ping_timer);
153
Neels Hofmeyr82112ea2019-11-25 03:59:44 +0100154 if (gsupc->up_down_cb) {
155 /* When the up_down_cb() returns false, the user asks us not to retry connecting. */
156 if (!gsupc->up_down_cb(gsupc, false))
157 return;
158 }
159
Harald Welteec6915a2018-07-23 14:25:33 +0200160 osmo_timer_schedule(&gsupc->connect_timer,
Harald Welte953d27c2018-07-23 11:11:22 +0200161 OSMO_GSUP_CLIENT_RECONNECT_INTERVAL, 0);
Harald Welteec6915a2018-07-23 14:25:33 +0200162 }
163}
164
Harald Welte953d27c2018-07-23 11:11:22 +0200165static int gsup_client_oap_handle(struct osmo_gsup_client *gsupc, struct msgb *msg_rx)
Harald Welteec6915a2018-07-23 14:25:33 +0200166{
167 int rc;
168 struct msgb *msg_tx;
169
170 /* If the oap_state is disabled, this will reject the messages. */
171 rc = osmo_oap_client_handle(&gsupc->oap_state, msg_rx, &msg_tx);
172 msgb_free(msg_rx);
173 if (rc < 0)
174 return rc;
175
176 if (msg_tx)
177 client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
178
179 return 0;
180}
181
182static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg)
183{
184 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
185 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
Harald Welte953d27c2018-07-23 11:11:22 +0200186 struct osmo_gsup_client *gsupc = (struct osmo_gsup_client *)link->data;
Harald Welteec6915a2018-07-23 14:25:33 +0200187 int rc;
Harald Welteec6915a2018-07-23 14:25:33 +0200188
Stefan Sperling55f5efa2018-12-04 11:40:30 +0100189 OSMO_ASSERT(gsupc->unit_name);
Harald Welteec6915a2018-07-23 14:25:33 +0200190
191 msg->l2h = &hh->data[0];
192
Stefan Sperling55f5efa2018-12-04 11:40:30 +0100193 rc = ipaccess_bts_handle_ccm(link, gsupc->ipa_dev, msg);
Harald Welteec6915a2018-07-23 14:25:33 +0200194
195 if (rc < 0) {
196 LOGP(DLGSUP, LOGL_NOTICE,
197 "GSUP received an invalid IPA/CCM message from %s:%d\n",
198 link->addr, link->port);
199 /* Link has been closed */
200 gsupc->is_connected = 0;
201 msgb_free(msg);
202 return -1;
203 }
204
205 if (rc == 1) {
206 uint8_t msg_type = *(msg->l2h);
207 /* CCM message */
208 if (msg_type == IPAC_MSGT_PONG) {
209 LOGP(DLGSUP, LOGL_DEBUG, "GSUP receiving PONG\n");
210 gsupc->got_ipa_pong = 1;
211 }
212
213 msgb_free(msg);
214 return 0;
215 }
216
217 if (hh->proto != IPAC_PROTO_OSMO)
218 goto invalid;
219
220 if (!he || msgb_l2len(msg) < sizeof(*he))
221 goto invalid;
222
223 msg->l2h = &he->data[0];
224
225 if (he->proto == IPAC_PROTO_EXT_GSUP) {
226 OSMO_ASSERT(gsupc->read_cb != NULL);
227 gsupc->read_cb(gsupc, msg);
228 /* expecting read_cb() to free msg */
229 } else if (he->proto == IPAC_PROTO_EXT_OAP) {
230 return gsup_client_oap_handle(gsupc, msg);
231 /* gsup_client_oap_handle frees msg */
232 } else
233 goto invalid;
234
235 return 0;
236
237invalid:
238 LOGP(DLGSUP, LOGL_NOTICE,
239 "GSUP received an invalid IPA message from %s:%d, size = %d\n",
240 link->addr, link->port, msgb_length(msg));
241
242 msgb_free(msg);
243 return -1;
244}
245
246static void ping_timer_cb(void *gsupc_)
247{
Harald Welte953d27c2018-07-23 11:11:22 +0200248 struct osmo_gsup_client *gsupc = gsupc_;
Harald Welteec6915a2018-07-23 14:25:33 +0200249
250 LOGP(DLGSUP, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n",
251 gsupc->is_connected ? "connected" : "not connected",
252 gsupc->got_ipa_pong ? "got" : "didn't get");
253
254 if (gsupc->got_ipa_pong) {
255 start_test_procedure(gsupc);
256 return;
257 }
258
259 LOGP(DLGSUP, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n");
260 ipa_client_conn_close(gsupc->link);
261 gsupc->is_connected = 0;
262
263 gsup_client_connect(gsupc);
264}
265
Harald Welte953d27c2018-07-23 11:11:22 +0200266static void start_test_procedure(struct osmo_gsup_client *gsupc)
Harald Welteec6915a2018-07-23 14:25:33 +0200267{
268 osmo_timer_setup(&gsupc->ping_timer, ping_timer_cb, gsupc);
269
270 gsupc->got_ipa_pong = 0;
Harald Welte953d27c2018-07-23 11:11:22 +0200271 osmo_timer_schedule(&gsupc->ping_timer, OSMO_GSUP_CLIENT_PING_INTERVAL, 0);
Harald Welteec6915a2018-07-23 14:25:33 +0200272 LOGP(DLGSUP, LOGL_DEBUG, "GSUP sending PING\n");
273 gsup_client_send_ping(gsupc);
274}
275
Stefan Sperling55f5efa2018-12-04 11:40:30 +0100276/*!
277 * Create a gsup client connecting to the specified IP address and TCP port.
278 * Use the provided ipaccess unit as the client-side identifier; ipa_dev should
279 * be allocated in talloc_ctx talloc_ctx as well.
280 * \param[in] talloc_ctx talloc context.
Neels Hofmeyr82112ea2019-11-25 03:59:44 +0100281 * \param[in] config Parameters for setting up the GSUP client.
282 * \return a GSUP client connection, or NULL on failure.
Stefan Sperling55f5efa2018-12-04 11:40:30 +0100283 */
Neels Hofmeyr82112ea2019-11-25 03:59:44 +0100284struct osmo_gsup_client *osmo_gsup_client_create3(void *talloc_ctx, struct osmo_gsup_client_config *config)
Harald Welteec6915a2018-07-23 14:25:33 +0200285{
Harald Welte953d27c2018-07-23 11:11:22 +0200286 struct osmo_gsup_client *gsupc;
Harald Welteec6915a2018-07-23 14:25:33 +0200287 int rc;
288
Neels Hofmeyr82112ea2019-11-25 03:59:44 +0100289 OSMO_ASSERT(config->ipa_dev->unit_name);
290
Harald Welte953d27c2018-07-23 11:11:22 +0200291 gsupc = talloc_zero(talloc_ctx, struct osmo_gsup_client);
Harald Welteec6915a2018-07-23 14:25:33 +0200292 OSMO_ASSERT(gsupc);
Neels Hofmeyr82112ea2019-11-25 03:59:44 +0100293 *gsupc = (struct osmo_gsup_client){
294 .unit_name = (const char *)config->ipa_dev->unit_name, /* API backwards compat */
295 .ipa_dev = config->ipa_dev,
296 .read_cb = config->read_cb,
297 .up_down_cb = config->up_down_cb,
298 .data = config->data,
299 };
Harald Welteec6915a2018-07-23 14:25:33 +0200300
301 /* a NULL oapc_config will mark oap_state disabled. */
Neels Hofmeyr82112ea2019-11-25 03:59:44 +0100302 rc = osmo_oap_client_init(config->oapc_config, &gsupc->oap_state);
Harald Welteec6915a2018-07-23 14:25:33 +0200303 if (rc != 0)
304 goto failed;
305
Neels Hofmeyr4fa96532020-01-29 12:48:16 +0100306 gsupc->link = ipa_client_conn_create2(gsupc,
307 /* no e1inp */ NULL,
308 0,
309 /* no specific local IP:port */ NULL, 0,
Neels Hofmeyr82112ea2019-11-25 03:59:44 +0100310 config->ip_addr, config->tcp_port,
Neels Hofmeyr4fa96532020-01-29 12:48:16 +0100311 gsup_client_updown_cb,
312 gsup_client_read_cb,
313 /* default write_cb */ NULL,
314 gsupc);
Harald Welteec6915a2018-07-23 14:25:33 +0200315 if (!gsupc->link)
316 goto failed;
317
318 osmo_timer_setup(&gsupc->connect_timer, connect_timer_cb, gsupc);
319
320 rc = gsup_client_connect(gsupc);
321
322 if (rc < 0)
323 goto failed;
Harald Welteec6915a2018-07-23 14:25:33 +0200324 return gsupc;
325
326failed:
Harald Welte953d27c2018-07-23 11:11:22 +0200327 osmo_gsup_client_destroy(gsupc);
Harald Welteec6915a2018-07-23 14:25:33 +0200328 return NULL;
329}
330
Neels Hofmeyr82112ea2019-11-25 03:59:44 +0100331/*! Like osmo_gsup_client_create3() but without the up_down_cb and data arguments, and with the oapc_config argument in
332 * a different position.
333 */
334struct osmo_gsup_client *osmo_gsup_client_create2(void *talloc_ctx,
335 struct ipaccess_unit *ipa_dev,
336 const char *ip_addr,
337 unsigned int tcp_port,
338 osmo_gsup_client_read_cb_t read_cb,
339 struct osmo_oap_client_config *oapc_config)
340{
341 struct osmo_gsup_client_config cfg = {
342 .ipa_dev = ipa_dev,
343 .ip_addr = ip_addr,
344 .tcp_port = tcp_port,
345 .oapc_config = oapc_config,
346 .read_cb = read_cb,
347 };
348 return osmo_gsup_client_create3(talloc_ctx, &cfg);
349}
350
Stefan Sperling55f5efa2018-12-04 11:40:30 +0100351/**
352 * Like osmo_gsup_client_create2() except it expects a unit name instead
353 * of a full-blown ipacess_unit as the client-side identifier.
354 */
355struct osmo_gsup_client *osmo_gsup_client_create(void *talloc_ctx,
356 const char *unit_name,
357 const char *ip_addr,
358 unsigned int tcp_port,
359 osmo_gsup_client_read_cb_t read_cb,
360 struct osmo_oap_client_config *oapc_config)
361{
362 struct ipaccess_unit *ipa_dev = talloc_zero(talloc_ctx, struct ipaccess_unit);
363 ipa_dev->unit_name = talloc_strdup(ipa_dev, unit_name);
364 return osmo_gsup_client_create2(talloc_ctx, ipa_dev, ip_addr, tcp_port, read_cb, oapc_config);
365}
366
Harald Welte953d27c2018-07-23 11:11:22 +0200367void osmo_gsup_client_destroy(struct osmo_gsup_client *gsupc)
Harald Welteec6915a2018-07-23 14:25:33 +0200368{
369 osmo_timer_del(&gsupc->connect_timer);
370 osmo_timer_del(&gsupc->ping_timer);
371
372 if (gsupc->link) {
373 ipa_client_conn_close(gsupc->link);
374 ipa_client_conn_destroy(gsupc->link);
375 gsupc->link = NULL;
376 }
377 talloc_free(gsupc);
378}
379
Harald Welte953d27c2018-07-23 11:11:22 +0200380int osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg)
Harald Welteec6915a2018-07-23 14:25:33 +0200381{
382 if (!gsupc || !gsupc->is_connected) {
383 LOGP(DLGSUP, LOGL_ERROR, "GSUP not connected, unable to send %s\n", msgb_hexdump(msg));
384 msgb_free(msg);
385 return -ENOTCONN;
386 }
387
388 client_send(gsupc, IPAC_PROTO_EXT_GSUP, msg);
389
390 return 0;
391}
392
Vadim Yanitskiydf8d4542018-11-29 01:49:57 +0700393/*! Encode and send a GSUP message.
394 * \param[in] gsupc GSUP client.
395 * \param[in] gsup_msg GSUP message to be sent.
396 * \returns 0 in case of success, negative on error.
397 */
398int osmo_gsup_client_enc_send(struct osmo_gsup_client *gsupc,
399 const struct osmo_gsup_message *gsup_msg)
400{
401 struct msgb *gsup_msgb;
402 int rc;
403
404 gsup_msgb = osmo_gsup_client_msgb_alloc();
405 if (!gsup_msgb) {
406 LOGP(DLGSUP, LOGL_ERROR, "Couldn't allocate GSUP message\n");
407 return -ENOMEM;
408 }
409
410 rc = osmo_gsup_encode(gsup_msgb, gsup_msg);
411 if (rc) {
412 LOGP(DLGSUP, LOGL_ERROR, "Couldn't encode GSUP message\n");
413 goto error;
414 }
415
416 rc = osmo_gsup_client_send(gsupc, gsup_msgb);
417 if (rc) {
418 LOGP(DLGSUP, LOGL_ERROR, "Couldn't send GSUP message\n");
Neels Hofmeyr7d2843d2019-10-30 03:57:01 +0100419 /* Do not free, osmo_gsup_client_send() already has. */
420 return rc;
Vadim Yanitskiydf8d4542018-11-29 01:49:57 +0700421 }
422
423 return 0;
424
425error:
426 talloc_free(gsup_msgb);
427 return rc;
428}
429
Harald Welte953d27c2018-07-23 11:11:22 +0200430struct msgb *osmo_gsup_client_msgb_alloc(void)
Harald Welteec6915a2018-07-23 14:25:33 +0200431{
432 return msgb_alloc_headroom(4000, 64, __func__);
433}