blob: 0335e210bdd4df49247d02cca6bb7c1e94ad0716 [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
Neels Hofmeyr90843962017-09-04 15:04:35 +020024#include <osmocom/msc/gsup_client.h>
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +010025
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
Neels Hofmeyr90843962017-09-04 15:04:35 +020031#include <osmocom/msc/debug.h>
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +010032
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
Neels Hofmeyr814fef02016-12-08 21:19:57 +010038static void start_test_procedure(struct gsup_client *gsupc);
Jacob Erlbeck03b46302014-12-19 19:18:54 +010039
Neels Hofmeyr814fef02016-12-08 21:19:57 +010040static void gsup_client_send_ping(struct gsup_client *gsupc)
Jacob Erlbeck03b46302014-12-19 19:18:54 +010041{
Neels Hofmeyr814fef02016-12-08 21:19:57 +010042 struct msgb *msg = gsup_client_msgb_alloc();
Jacob Erlbeck03b46302014-12-19 19:18:54 +010043
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
Neels Hofmeyr814fef02016-12-08 21:19:57 +010050static int gsup_client_connect(struct gsup_client *gsupc)
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010051{
52 int rc;
53
54 if (gsupc->is_connected)
55 return 0;
56
57 if (osmo_timer_pending(&gsupc->connect_timer)) {
Neels Hofmeyref022782016-12-08 21:28:29 +010058 LOGP(DLGSUP, LOGL_DEBUG,
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010059 "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)) {
Neels Hofmeyref022782016-12-08 21:28:29 +010064 LOGP(DLGSUP, LOGL_DEBUG,
Jacob Erlbeck03b46302014-12-19 19:18:54 +010065 "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)
Neels Hofmeyref022782016-12-08 21:28:29 +010070 LOGP(DLGSUP, LOGL_DEBUG, "GSUP connect: discarded stored messages\n");
Jacob Erlbeck4188c302014-12-19 18:50:05 +010071
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) {
Harald Welte2483f1b2016-06-19 18:06:02 +020075 LOGP(DLGSUP, LOGL_NOTICE, "GSUP connecting to %s:%d\n",
Jacob Erlbeck69e16b92014-12-19 19:00:56 +010076 gsupc->link->addr, gsupc->link->port);
77 return 0;
78 }
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010079
Harald Welte2483f1b2016-06-19 18:06:02 +020080 LOGP(DLGSUP, LOGL_ERROR, "GSUP failed to connect to %s:%d: %s\n",
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010081 gsupc->link->addr, gsupc->link->port, strerror(-rc));
82
83 if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT ||
84 rc == -EINVAL)
85 return rc;
86
Neels Hofmeyr814fef02016-12-08 21:19:57 +010087 osmo_timer_schedule(&gsupc->connect_timer,
88 GSUP_CLIENT_RECONNECT_INTERVAL, 0);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010089
Neels Hofmeyref022782016-12-08 21:28:29 +010090 LOGP(DLGSUP, LOGL_INFO, "Scheduled timer to retry GSUP connect to %s:%d\n",
Jacob Erlbeck37184902015-01-19 10:56:15 +010091 gsupc->link->addr, gsupc->link->port);
92
93 return 0;
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010094}
95
96static void connect_timer_cb(void *gsupc_)
97{
Neels Hofmeyr814fef02016-12-08 21:19:57 +010098 struct gsup_client *gsupc = gsupc_;
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010099
100 if (gsupc->is_connected)
101 return;
102
103 gsup_client_connect(gsupc);
104}
105
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100106static void client_send(struct gsup_client *gsupc, int proto_ext,
107 struct msgb *msg_tx)
Neels Hofmeyr06cfe002015-10-01 15:23:51 +0200108{
109 ipa_prepend_header_ext(msg_tx, proto_ext);
110 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
111 ipa_client_conn_send(gsupc->link, msg_tx);
112 /* msg_tx is now queued and will be freed. */
113}
114
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100115static void gsup_client_oap_register(struct gsup_client *gsupc)
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200116{
117 struct msgb *msg_tx;
118 int rc;
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100119 rc = oap_client_register(&gsupc->oap_state, &msg_tx);
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200120
121 if ((rc < 0) || (!msg_tx)) {
Neels Hofmeyref022782016-12-08 21:28:29 +0100122 LOGP(DLGSUP, LOGL_ERROR, "GSUP OAP set up, but cannot register.\n");
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200123 return;
124 }
125
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100126 client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200127}
128
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100129static void gsup_client_updown_cb(struct ipa_client_conn *link, int up)
130{
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100131 struct gsup_client *gsupc = link->data;
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100132
Neels Hofmeyref022782016-12-08 21:28:29 +0100133 LOGP(DLGSUP, LOGL_INFO, "GSUP link to %s:%d %s\n",
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100134 link->addr, link->port, up ? "UP" : "DOWN");
135
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100136 gsupc->is_connected = up;
137
138 if (up) {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100139 start_test_procedure(gsupc);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100140
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200141 if (gsupc->oap_state.state == OAP_INITIALIZED)
142 gsup_client_oap_register(gsupc);
143
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100144 osmo_timer_del(&gsupc->connect_timer);
145 } else {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100146 osmo_timer_del(&gsupc->ping_timer);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100147
148 osmo_timer_schedule(&gsupc->connect_timer,
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100149 GSUP_CLIENT_RECONNECT_INTERVAL, 0);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100150 }
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100151}
152
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100153static int gsup_client_oap_handle(struct gsup_client *gsupc, struct msgb *msg_rx)
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200154{
155 int rc;
156 struct msgb *msg_tx;
157
Neels Hofmeyr2fa74fa2016-12-08 23:12:17 +0100158 /* If the oap_state is disabled, this will reject the messages. */
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100159 rc = oap_client_handle(&gsupc->oap_state, msg_rx, &msg_tx);
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200160 msgb_free(msg_rx);
161 if (rc < 0)
162 return rc;
163
164 if (msg_tx)
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100165 client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200166
167 return 0;
168}
169
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100170static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg)
171{
172 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
173 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100174 struct gsup_client *gsupc = (struct gsup_client *)link->data;
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100175 int rc;
Neels Hofmeyrf2ba8132017-03-04 03:15:53 +0100176 struct ipaccess_unit ipa_dev = {
177 /* see gsup_client_create() on const vs non-const */
178 .unit_name = (char*)gsupc->unit_name,
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100179 };
180
Neels Hofmeyrf2ba8132017-03-04 03:15:53 +0100181 OSMO_ASSERT(ipa_dev.unit_name);
182
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100183 msg->l2h = &hh->data[0];
184
185 rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg);
186
187 if (rc < 0) {
Neels Hofmeyref022782016-12-08 21:28:29 +0100188 LOGP(DLGSUP, LOGL_NOTICE,
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100189 "GSUP received an invalid IPA/CCM message from %s:%d\n",
190 link->addr, link->port);
191 /* Link has been closed */
192 gsupc->is_connected = 0;
193 msgb_free(msg);
194 return -1;
195 }
196
197 if (rc == 1) {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100198 uint8_t msg_type = *(msg->l2h);
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100199 /* CCM message */
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100200 if (msg_type == IPAC_MSGT_PONG) {
Neels Hofmeyref022782016-12-08 21:28:29 +0100201 LOGP(DLGSUP, LOGL_DEBUG, "GSUP receiving PONG\n");
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100202 gsupc->got_ipa_pong = 1;
203 }
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100204
205 msgb_free(msg);
206 return 0;
207 }
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100208
209 if (hh->proto != IPAC_PROTO_OSMO)
210 goto invalid;
211
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200212 if (!he || msgb_l2len(msg) < sizeof(*he))
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100213 goto invalid;
214
215 msg->l2h = &he->data[0];
216
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200217 if (he->proto == IPAC_PROTO_EXT_GSUP) {
218 OSMO_ASSERT(gsupc->read_cb != NULL);
219 gsupc->read_cb(gsupc, msg);
220 /* expecting read_cb() to free msg */
221 } else if (he->proto == IPAC_PROTO_EXT_OAP) {
222 return gsup_client_oap_handle(gsupc, msg);
223 /* gsup_client_oap_handle frees msg */
224 } else
225 goto invalid;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100226
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100227 return 0;
228
229invalid:
Neels Hofmeyref022782016-12-08 21:28:29 +0100230 LOGP(DLGSUP, LOGL_NOTICE,
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100231 "GSUP received an invalid IPA message from %s:%d, size = %d\n",
232 link->addr, link->port, msgb_length(msg));
233
234 msgb_free(msg);
235 return -1;
236}
237
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100238static void ping_timer_cb(void *gsupc_)
239{
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100240 struct gsup_client *gsupc = gsupc_;
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100241
Neels Hofmeyref022782016-12-08 21:28:29 +0100242 LOGP(DLGSUP, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n",
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100243 gsupc->is_connected ? "connected" : "not connected",
244 gsupc->got_ipa_pong ? "got" : "didn't get");
245
246 if (gsupc->got_ipa_pong) {
247 start_test_procedure(gsupc);
248 return;
249 }
250
Neels Hofmeyref022782016-12-08 21:28:29 +0100251 LOGP(DLGSUP, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n");
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100252 ipa_client_conn_close(gsupc->link);
253 gsupc->is_connected = 0;
254
255 gsup_client_connect(gsupc);
256}
257
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100258static void start_test_procedure(struct gsup_client *gsupc)
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100259{
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200260 osmo_timer_setup(&gsupc->ping_timer, ping_timer_cb, gsupc);
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100261
262 gsupc->got_ipa_pong = 0;
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100263 osmo_timer_schedule(&gsupc->ping_timer, GSUP_CLIENT_PING_INTERVAL, 0);
Neels Hofmeyref022782016-12-08 21:28:29 +0100264 LOGP(DLGSUP, LOGL_DEBUG, "GSUP sending PING\n");
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100265 gsup_client_send_ping(gsupc);
266}
267
Neels Hofmeyrf2ba8132017-03-04 03:15:53 +0100268struct gsup_client *gsup_client_create(const char *unit_name,
269 const char *ip_addr,
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100270 unsigned int tcp_port,
271 gsup_client_read_cb_t read_cb,
Neels Hofmeyr37f92522016-12-08 23:58:31 +0100272 struct oap_client_config *oapc_config)
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100273{
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100274 struct gsup_client *gsupc;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100275 int rc;
276
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100277 gsupc = talloc_zero(tall_bsc_ctx, struct gsup_client);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100278 OSMO_ASSERT(gsupc);
279
Neels Hofmeyrf2ba8132017-03-04 03:15:53 +0100280 /* struct ipaccess_unit has a non-const unit_name, so let's copy to be
281 * able to have a non-const unit_name here as well. To not taint the
282 * public gsup_client API, let's store it in a const char* anyway. */
283 gsupc->unit_name = talloc_strdup(gsupc, unit_name);
284 OSMO_ASSERT(gsupc->unit_name);
285
Neels Hofmeyr37f92522016-12-08 23:58:31 +0100286 /* a NULL oapc_config will mark oap_state disabled. */
287 rc = oap_client_init(oapc_config, &gsupc->oap_state);
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200288 if (rc != 0)
289 goto failed;
290
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100291 gsupc->link = ipa_client_conn_create(gsupc,
292 /* no e1inp */ NULL,
293 0,
294 ip_addr, tcp_port,
295 gsup_client_updown_cb,
296 gsup_client_read_cb,
297 /* default write_cb */ NULL,
298 gsupc);
299 if (!gsupc->link)
300 goto failed;
301
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200302 osmo_timer_setup(&gsupc->connect_timer, connect_timer_cb, gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100303
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100304 rc = gsup_client_connect(gsupc);
305
Jacob Erlbeck3ee67ff2015-01-26 09:22:39 +0100306 if (rc < 0)
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100307 goto failed;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100308
309 gsupc->read_cb = read_cb;
310
311 return gsupc;
312
313failed:
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100314 gsup_client_destroy(gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100315 return NULL;
316}
317
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100318void gsup_client_destroy(struct gsup_client *gsupc)
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100319{
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100320 osmo_timer_del(&gsupc->connect_timer);
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100321 osmo_timer_del(&gsupc->ping_timer);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100322
323 if (gsupc->link) {
324 ipa_client_conn_close(gsupc->link);
325 ipa_client_conn_destroy(gsupc->link);
326 gsupc->link = NULL;
327 }
328 talloc_free(gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100329}
330
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100331int gsup_client_send(struct gsup_client *gsupc, struct msgb *msg)
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100332{
Neels Hofmeyrbc5dd492017-07-27 14:06:21 +0200333 if (!gsupc || !gsupc->is_connected) {
Harald Welte703f2ec2018-01-25 00:36:42 +0100334 LOGP(DLGSUP, LOGL_ERROR, "GSUP not connected, unable to send %s\n", msgb_hexdump(msg));
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100335 msgb_free(msg);
336 return -ENOTCONN;
337 }
338
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100339 client_send(gsupc, IPAC_PROTO_EXT_GSUP, msg);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100340
341 return 0;
342}
343
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100344struct msgb *gsup_client_msgb_alloc(void)
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100345{
346 return msgb_alloc_headroom(4000, 64, __func__);
347}