blob: 814d5a2f889800fb2a661937349472875bd66e0b [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
100 gsup_client_connect(gsupc);
101}
102
Harald Welte953d27c2018-07-23 11:11:22 +0200103static void client_send(struct osmo_gsup_client *gsupc, int proto_ext,
Harald Welteec6915a2018-07-23 14:25:33 +0200104 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
Harald Welte953d27c2018-07-23 11:11:22 +0200112static void gsup_client_oap_register(struct osmo_gsup_client *gsupc)
Harald Welteec6915a2018-07-23 14:25:33 +0200113{
114 struct msgb *msg_tx;
115 int rc;
116 rc = osmo_oap_client_register(&gsupc->oap_state, &msg_tx);
117
118 if ((rc < 0) || (!msg_tx)) {
119 LOGP(DLGSUP, LOGL_ERROR, "GSUP OAP set up, but cannot register.\n");
120 return;
121 }
122
123 client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
124}
125
126static void gsup_client_updown_cb(struct ipa_client_conn *link, int up)
127{
Harald Welte953d27c2018-07-23 11:11:22 +0200128 struct osmo_gsup_client *gsupc = link->data;
Harald Welteec6915a2018-07-23 14:25:33 +0200129
130 LOGP(DLGSUP, LOGL_INFO, "GSUP link to %s:%d %s\n",
131 link->addr, link->port, up ? "UP" : "DOWN");
132
133 gsupc->is_connected = up;
134
135 if (up) {
136 start_test_procedure(gsupc);
137
138 if (gsupc->oap_state.state == OSMO_OAP_INITIALIZED)
139 gsup_client_oap_register(gsupc);
140
141 osmo_timer_del(&gsupc->connect_timer);
142 } else {
143 osmo_timer_del(&gsupc->ping_timer);
144
145 osmo_timer_schedule(&gsupc->connect_timer,
Harald Welte953d27c2018-07-23 11:11:22 +0200146 OSMO_GSUP_CLIENT_RECONNECT_INTERVAL, 0);
Harald Welteec6915a2018-07-23 14:25:33 +0200147 }
148}
149
Harald Welte953d27c2018-07-23 11:11:22 +0200150static int gsup_client_oap_handle(struct osmo_gsup_client *gsupc, struct msgb *msg_rx)
Harald Welteec6915a2018-07-23 14:25:33 +0200151{
152 int rc;
153 struct msgb *msg_tx;
154
155 /* If the oap_state is disabled, this will reject the messages. */
156 rc = osmo_oap_client_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 client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
163
164 return 0;
165}
166
167static 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);
Harald Welte953d27c2018-07-23 11:11:22 +0200171 struct osmo_gsup_client *gsupc = (struct osmo_gsup_client *)link->data;
Harald Welteec6915a2018-07-23 14:25:33 +0200172 int rc;
Harald Welteec6915a2018-07-23 14:25:33 +0200173
Stefan Sperling55f5efa2018-12-04 11:40:30 +0100174 OSMO_ASSERT(gsupc->unit_name);
Harald Welteec6915a2018-07-23 14:25:33 +0200175
176 msg->l2h = &hh->data[0];
177
Stefan Sperling55f5efa2018-12-04 11:40:30 +0100178 rc = ipaccess_bts_handle_ccm(link, gsupc->ipa_dev, msg);
Harald Welteec6915a2018-07-23 14:25:33 +0200179
180 if (rc < 0) {
181 LOGP(DLGSUP, 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) {
191 uint8_t msg_type = *(msg->l2h);
192 /* CCM message */
193 if (msg_type == IPAC_MSGT_PONG) {
194 LOGP(DLGSUP, LOGL_DEBUG, "GSUP receiving PONG\n");
195 gsupc->got_ipa_pong = 1;
196 }
197
198 msgb_free(msg);
199 return 0;
200 }
201
202 if (hh->proto != IPAC_PROTO_OSMO)
203 goto invalid;
204
205 if (!he || msgb_l2len(msg) < sizeof(*he))
206 goto invalid;
207
208 msg->l2h = &he->data[0];
209
210 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;
219
220 return 0;
221
222invalid:
223 LOGP(DLGSUP, 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
231static void ping_timer_cb(void *gsupc_)
232{
Harald Welte953d27c2018-07-23 11:11:22 +0200233 struct osmo_gsup_client *gsupc = gsupc_;
Harald Welteec6915a2018-07-23 14:25:33 +0200234
235 LOGP(DLGSUP, 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(DLGSUP, 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
Harald Welte953d27c2018-07-23 11:11:22 +0200251static void start_test_procedure(struct osmo_gsup_client *gsupc)
Harald Welteec6915a2018-07-23 14:25:33 +0200252{
253 osmo_timer_setup(&gsupc->ping_timer, ping_timer_cb, gsupc);
254
255 gsupc->got_ipa_pong = 0;
Harald Welte953d27c2018-07-23 11:11:22 +0200256 osmo_timer_schedule(&gsupc->ping_timer, OSMO_GSUP_CLIENT_PING_INTERVAL, 0);
Harald Welteec6915a2018-07-23 14:25:33 +0200257 LOGP(DLGSUP, LOGL_DEBUG, "GSUP sending PING\n");
258 gsup_client_send_ping(gsupc);
259}
260
Stefan Sperling55f5efa2018-12-04 11:40:30 +0100261/*!
262 * Create a gsup client connecting to the specified IP address and TCP port.
263 * Use the provided ipaccess unit as the client-side identifier; ipa_dev should
264 * be allocated in talloc_ctx talloc_ctx as well.
265 * \param[in] talloc_ctx talloc context.
266 * \param[in] ipa_dev IP access unit which contains client identification information; must be allocated
267 * in talloc_ctx as well to ensure it lives throughout the lifetime of the connection.
268 * \param[in] ip_addr GSUP server IP address.
269 * \param[in] tcp_port GSUP server TCP port.
270 * \param[in] read_cb callback for reading from the GSUP connection.
271 * \param[in] oapc_config OPA client configuration.
272 * \returns a GSUP client connection or NULL on failure.
273 */
274struct osmo_gsup_client *osmo_gsup_client_create2(void *talloc_ctx,
275 struct ipaccess_unit *ipa_dev,
276 const char *ip_addr,
277 unsigned int tcp_port,
278 osmo_gsup_client_read_cb_t read_cb,
279 struct osmo_oap_client_config *oapc_config)
Harald Welteec6915a2018-07-23 14:25:33 +0200280{
Harald Welte953d27c2018-07-23 11:11:22 +0200281 struct osmo_gsup_client *gsupc;
Harald Welteec6915a2018-07-23 14:25:33 +0200282 int rc;
283
Harald Welte953d27c2018-07-23 11:11:22 +0200284 gsupc = talloc_zero(talloc_ctx, struct osmo_gsup_client);
Harald Welteec6915a2018-07-23 14:25:33 +0200285 OSMO_ASSERT(gsupc);
Stefan Sperling55f5efa2018-12-04 11:40:30 +0100286 gsupc->unit_name = (const char *)ipa_dev->unit_name; /* API backwards compat */
287 gsupc->ipa_dev = ipa_dev;
Harald Welteec6915a2018-07-23 14:25:33 +0200288
289 /* a NULL oapc_config will mark oap_state disabled. */
290 rc = osmo_oap_client_init(oapc_config, &gsupc->oap_state);
291 if (rc != 0)
292 goto failed;
293
294 gsupc->link = ipa_client_conn_create(gsupc,
295 /* no e1inp */ NULL,
296 0,
297 ip_addr, tcp_port,
298 gsup_client_updown_cb,
299 gsup_client_read_cb,
300 /* default write_cb */ NULL,
301 gsupc);
302 if (!gsupc->link)
303 goto failed;
304
305 osmo_timer_setup(&gsupc->connect_timer, connect_timer_cb, gsupc);
306
307 rc = gsup_client_connect(gsupc);
308
309 if (rc < 0)
310 goto failed;
311
312 gsupc->read_cb = read_cb;
313
314 return gsupc;
315
316failed:
Harald Welte953d27c2018-07-23 11:11:22 +0200317 osmo_gsup_client_destroy(gsupc);
Harald Welteec6915a2018-07-23 14:25:33 +0200318 return NULL;
319}
320
Stefan Sperling55f5efa2018-12-04 11:40:30 +0100321/**
322 * Like osmo_gsup_client_create2() except it expects a unit name instead
323 * of a full-blown ipacess_unit as the client-side identifier.
324 */
325struct osmo_gsup_client *osmo_gsup_client_create(void *talloc_ctx,
326 const char *unit_name,
327 const char *ip_addr,
328 unsigned int tcp_port,
329 osmo_gsup_client_read_cb_t read_cb,
330 struct osmo_oap_client_config *oapc_config)
331{
332 struct ipaccess_unit *ipa_dev = talloc_zero(talloc_ctx, struct ipaccess_unit);
333 ipa_dev->unit_name = talloc_strdup(ipa_dev, unit_name);
334 return osmo_gsup_client_create2(talloc_ctx, ipa_dev, ip_addr, tcp_port, read_cb, oapc_config);
335}
336
Harald Welte953d27c2018-07-23 11:11:22 +0200337void osmo_gsup_client_destroy(struct osmo_gsup_client *gsupc)
Harald Welteec6915a2018-07-23 14:25:33 +0200338{
339 osmo_timer_del(&gsupc->connect_timer);
340 osmo_timer_del(&gsupc->ping_timer);
341
342 if (gsupc->link) {
343 ipa_client_conn_close(gsupc->link);
344 ipa_client_conn_destroy(gsupc->link);
345 gsupc->link = NULL;
346 }
347 talloc_free(gsupc);
348}
349
Harald Welte953d27c2018-07-23 11:11:22 +0200350int osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg)
Harald Welteec6915a2018-07-23 14:25:33 +0200351{
352 if (!gsupc || !gsupc->is_connected) {
353 LOGP(DLGSUP, LOGL_ERROR, "GSUP not connected, unable to send %s\n", msgb_hexdump(msg));
354 msgb_free(msg);
355 return -ENOTCONN;
356 }
357
358 client_send(gsupc, IPAC_PROTO_EXT_GSUP, msg);
359
360 return 0;
361}
362
Vadim Yanitskiydf8d4542018-11-29 01:49:57 +0700363/*! Encode and send a GSUP message.
364 * \param[in] gsupc GSUP client.
365 * \param[in] gsup_msg GSUP message to be sent.
366 * \returns 0 in case of success, negative on error.
367 */
368int osmo_gsup_client_enc_send(struct osmo_gsup_client *gsupc,
369 const struct osmo_gsup_message *gsup_msg)
370{
371 struct msgb *gsup_msgb;
372 int rc;
373
374 gsup_msgb = osmo_gsup_client_msgb_alloc();
375 if (!gsup_msgb) {
376 LOGP(DLGSUP, LOGL_ERROR, "Couldn't allocate GSUP message\n");
377 return -ENOMEM;
378 }
379
380 rc = osmo_gsup_encode(gsup_msgb, gsup_msg);
381 if (rc) {
382 LOGP(DLGSUP, LOGL_ERROR, "Couldn't encode GSUP message\n");
383 goto error;
384 }
385
386 rc = osmo_gsup_client_send(gsupc, gsup_msgb);
387 if (rc) {
388 LOGP(DLGSUP, LOGL_ERROR, "Couldn't send GSUP message\n");
Neels Hofmeyr7d2843d2019-10-30 03:57:01 +0100389 /* Do not free, osmo_gsup_client_send() already has. */
390 return rc;
Vadim Yanitskiydf8d4542018-11-29 01:49:57 +0700391 }
392
393 return 0;
394
395error:
396 talloc_free(gsup_msgb);
397 return rc;
398}
399
Harald Welte953d27c2018-07-23 11:11:22 +0200400struct msgb *osmo_gsup_client_msgb_alloc(void)
Harald Welteec6915a2018-07-23 14:25:33 +0200401{
402 return msgb_alloc_headroom(4000, 64, __func__);
403}