blob: 39dc181b423d0047e536dab714f2d46c6c83c19b [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>
Harald Weltea44b9702018-07-30 17:53:28 +020028#include <osmocom/gsm/oap_client.h>
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +010029#include <osmocom/core/msgb.h>
Harald Welte4f8e34b2016-05-06 23:27:38 +020030#include <osmocom/core/logging.h>
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +010031
Neels Hofmeyr90843962017-09-04 15:04:35 +020032#include <osmocom/msc/debug.h>
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +010033
34#include <errno.h>
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010035#include <string.h>
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +010036
Neels Hofmeyr814fef02016-12-08 21:19:57 +010037static void start_test_procedure(struct gsup_client *gsupc);
Jacob Erlbeck03b46302014-12-19 19:18:54 +010038
Neels Hofmeyr814fef02016-12-08 21:19:57 +010039static void gsup_client_send_ping(struct gsup_client *gsupc)
Jacob Erlbeck03b46302014-12-19 19:18:54 +010040{
Neels Hofmeyr814fef02016-12-08 21:19:57 +010041 struct msgb *msg = gsup_client_msgb_alloc();
Jacob Erlbeck03b46302014-12-19 19:18:54 +010042
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
Neels Hofmeyr814fef02016-12-08 21:19:57 +010049static int gsup_client_connect(struct gsup_client *gsupc)
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010050{
51 int rc;
52
53 if (gsupc->is_connected)
54 return 0;
55
56 if (osmo_timer_pending(&gsupc->connect_timer)) {
Neels Hofmeyref022782016-12-08 21:28:29 +010057 LOGP(DLGSUP, LOGL_DEBUG,
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010058 "GSUP connect: connect timer already running\n");
59 osmo_timer_del(&gsupc->connect_timer);
60 }
61
Jacob Erlbeck03b46302014-12-19 19:18:54 +010062 if (osmo_timer_pending(&gsupc->ping_timer)) {
Neels Hofmeyref022782016-12-08 21:28:29 +010063 LOGP(DLGSUP, LOGL_DEBUG,
Jacob Erlbeck03b46302014-12-19 19:18:54 +010064 "GSUP connect: ping timer already running\n");
65 osmo_timer_del(&gsupc->ping_timer);
66 }
67
Jacob Erlbeck4188c302014-12-19 18:50:05 +010068 if (ipa_client_conn_clear_queue(gsupc->link) > 0)
Neels Hofmeyref022782016-12-08 21:28:29 +010069 LOGP(DLGSUP, LOGL_DEBUG, "GSUP connect: discarded stored messages\n");
Jacob Erlbeck4188c302014-12-19 18:50:05 +010070
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010071 rc = ipa_client_conn_open(gsupc->link);
72
Jacob Erlbeck69e16b92014-12-19 19:00:56 +010073 if (rc >= 0) {
Harald Welte2483f1b2016-06-19 18:06:02 +020074 LOGP(DLGSUP, LOGL_NOTICE, "GSUP connecting to %s:%d\n",
Jacob Erlbeck69e16b92014-12-19 19:00:56 +010075 gsupc->link->addr, gsupc->link->port);
76 return 0;
77 }
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010078
Harald Welte2483f1b2016-06-19 18:06:02 +020079 LOGP(DLGSUP, LOGL_ERROR, "GSUP failed to connect to %s:%d: %s\n",
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010080 gsupc->link->addr, gsupc->link->port, strerror(-rc));
81
82 if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT ||
83 rc == -EINVAL)
84 return rc;
85
Neels Hofmeyr814fef02016-12-08 21:19:57 +010086 osmo_timer_schedule(&gsupc->connect_timer,
87 GSUP_CLIENT_RECONNECT_INTERVAL, 0);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010088
Neels Hofmeyref022782016-12-08 21:28:29 +010089 LOGP(DLGSUP, LOGL_INFO, "Scheduled timer to retry GSUP connect to %s:%d\n",
Jacob Erlbeck37184902015-01-19 10:56:15 +010090 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{
Neels Hofmeyr814fef02016-12-08 21:19:57 +010097 struct gsup_client *gsupc = gsupc_;
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010098
99 if (gsupc->is_connected)
100 return;
101
102 gsup_client_connect(gsupc);
103}
104
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100105static void client_send(struct gsup_client *gsupc, int proto_ext,
106 struct msgb *msg_tx)
Neels Hofmeyr06cfe002015-10-01 15:23:51 +0200107{
108 ipa_prepend_header_ext(msg_tx, proto_ext);
109 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
110 ipa_client_conn_send(gsupc->link, msg_tx);
111 /* msg_tx is now queued and will be freed. */
112}
113
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100114static void gsup_client_oap_register(struct gsup_client *gsupc)
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200115{
116 struct msgb *msg_tx;
117 int rc;
Harald Weltea44b9702018-07-30 17:53:28 +0200118 rc = osmo_oap_client_register(&gsupc->oap_state, &msg_tx);
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200119
120 if ((rc < 0) || (!msg_tx)) {
Neels Hofmeyref022782016-12-08 21:28:29 +0100121 LOGP(DLGSUP, LOGL_ERROR, "GSUP OAP set up, but cannot register.\n");
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200122 return;
123 }
124
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100125 client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200126}
127
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100128static void gsup_client_updown_cb(struct ipa_client_conn *link, int up)
129{
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100130 struct gsup_client *gsupc = link->data;
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100131
Neels Hofmeyref022782016-12-08 21:28:29 +0100132 LOGP(DLGSUP, LOGL_INFO, "GSUP link to %s:%d %s\n",
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100133 link->addr, link->port, up ? "UP" : "DOWN");
134
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100135 gsupc->is_connected = up;
136
137 if (up) {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100138 start_test_procedure(gsupc);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100139
Harald Weltea44b9702018-07-30 17:53:28 +0200140 if (gsupc->oap_state.state == OSMO_OAP_INITIALIZED)
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200141 gsup_client_oap_register(gsupc);
142
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100143 osmo_timer_del(&gsupc->connect_timer);
144 } else {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100145 osmo_timer_del(&gsupc->ping_timer);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100146
147 osmo_timer_schedule(&gsupc->connect_timer,
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100148 GSUP_CLIENT_RECONNECT_INTERVAL, 0);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100149 }
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100150}
151
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100152static int gsup_client_oap_handle(struct gsup_client *gsupc, struct msgb *msg_rx)
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200153{
154 int rc;
155 struct msgb *msg_tx;
156
Neels Hofmeyr2fa74fa2016-12-08 23:12:17 +0100157 /* If the oap_state is disabled, this will reject the messages. */
Harald Weltea44b9702018-07-30 17:53:28 +0200158 rc = osmo_oap_client_handle(&gsupc->oap_state, msg_rx, &msg_tx);
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200159 msgb_free(msg_rx);
160 if (rc < 0)
161 return rc;
162
163 if (msg_tx)
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100164 client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200165
166 return 0;
167}
168
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100169static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg)
170{
171 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
172 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100173 struct gsup_client *gsupc = (struct gsup_client *)link->data;
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100174 int rc;
Neels Hofmeyrf2ba8132017-03-04 03:15:53 +0100175 struct ipaccess_unit ipa_dev = {
176 /* see gsup_client_create() on const vs non-const */
177 .unit_name = (char*)gsupc->unit_name,
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100178 };
179
Neels Hofmeyrf2ba8132017-03-04 03:15:53 +0100180 OSMO_ASSERT(ipa_dev.unit_name);
181
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100182 msg->l2h = &hh->data[0];
183
184 rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg);
185
186 if (rc < 0) {
Neels Hofmeyref022782016-12-08 21:28:29 +0100187 LOGP(DLGSUP, LOGL_NOTICE,
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100188 "GSUP received an invalid IPA/CCM message from %s:%d\n",
189 link->addr, link->port);
190 /* Link has been closed */
191 gsupc->is_connected = 0;
192 msgb_free(msg);
193 return -1;
194 }
195
196 if (rc == 1) {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100197 uint8_t msg_type = *(msg->l2h);
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100198 /* CCM message */
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100199 if (msg_type == IPAC_MSGT_PONG) {
Neels Hofmeyref022782016-12-08 21:28:29 +0100200 LOGP(DLGSUP, LOGL_DEBUG, "GSUP receiving PONG\n");
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100201 gsupc->got_ipa_pong = 1;
202 }
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100203
204 msgb_free(msg);
205 return 0;
206 }
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100207
208 if (hh->proto != IPAC_PROTO_OSMO)
209 goto invalid;
210
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200211 if (!he || msgb_l2len(msg) < sizeof(*he))
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100212 goto invalid;
213
214 msg->l2h = &he->data[0];
215
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200216 if (he->proto == IPAC_PROTO_EXT_GSUP) {
217 OSMO_ASSERT(gsupc->read_cb != NULL);
218 gsupc->read_cb(gsupc, msg);
219 /* expecting read_cb() to free msg */
220 } else if (he->proto == IPAC_PROTO_EXT_OAP) {
221 return gsup_client_oap_handle(gsupc, msg);
222 /* gsup_client_oap_handle frees msg */
223 } else
224 goto invalid;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100225
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100226 return 0;
227
228invalid:
Neels Hofmeyref022782016-12-08 21:28:29 +0100229 LOGP(DLGSUP, LOGL_NOTICE,
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100230 "GSUP received an invalid IPA message from %s:%d, size = %d\n",
231 link->addr, link->port, msgb_length(msg));
232
233 msgb_free(msg);
234 return -1;
235}
236
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100237static void ping_timer_cb(void *gsupc_)
238{
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100239 struct gsup_client *gsupc = gsupc_;
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100240
Neels Hofmeyref022782016-12-08 21:28:29 +0100241 LOGP(DLGSUP, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n",
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100242 gsupc->is_connected ? "connected" : "not connected",
243 gsupc->got_ipa_pong ? "got" : "didn't get");
244
245 if (gsupc->got_ipa_pong) {
246 start_test_procedure(gsupc);
247 return;
248 }
249
Neels Hofmeyref022782016-12-08 21:28:29 +0100250 LOGP(DLGSUP, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n");
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100251 ipa_client_conn_close(gsupc->link);
252 gsupc->is_connected = 0;
253
254 gsup_client_connect(gsupc);
255}
256
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100257static void start_test_procedure(struct gsup_client *gsupc)
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100258{
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200259 osmo_timer_setup(&gsupc->ping_timer, ping_timer_cb, gsupc);
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100260
261 gsupc->got_ipa_pong = 0;
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100262 osmo_timer_schedule(&gsupc->ping_timer, GSUP_CLIENT_PING_INTERVAL, 0);
Neels Hofmeyref022782016-12-08 21:28:29 +0100263 LOGP(DLGSUP, LOGL_DEBUG, "GSUP sending PING\n");
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100264 gsup_client_send_ping(gsupc);
265}
266
Neels Hofmeyrc01e9092018-03-22 15:56:49 +0100267struct gsup_client *gsup_client_create(void *talloc_ctx,
268 const char *unit_name,
Neels Hofmeyrf2ba8132017-03-04 03:15:53 +0100269 const char *ip_addr,
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100270 unsigned int tcp_port,
271 gsup_client_read_cb_t read_cb,
Harald Weltea44b9702018-07-30 17:53:28 +0200272 struct osmo_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 Hofmeyrc01e9092018-03-22 15:56:49 +0100277 gsupc = talloc_zero(talloc_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. */
Harald Weltea44b9702018-07-30 17:53:28 +0200287 rc = osmo_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}