blob: d34a22deecfc38ffa0dac32dc51809121fa23d2e [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;
173 struct ipaccess_unit ipa_dev = {
174 /* see gsup_client_create() on const vs non-const */
175 .unit_name = (char*)gsupc->unit_name,
176 };
177
178 OSMO_ASSERT(ipa_dev.unit_name);
179
180 msg->l2h = &hh->data[0];
181
182 rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg);
183
184 if (rc < 0) {
185 LOGP(DLGSUP, LOGL_NOTICE,
186 "GSUP received an invalid IPA/CCM message from %s:%d\n",
187 link->addr, link->port);
188 /* Link has been closed */
189 gsupc->is_connected = 0;
190 msgb_free(msg);
191 return -1;
192 }
193
194 if (rc == 1) {
195 uint8_t msg_type = *(msg->l2h);
196 /* CCM message */
197 if (msg_type == IPAC_MSGT_PONG) {
198 LOGP(DLGSUP, LOGL_DEBUG, "GSUP receiving PONG\n");
199 gsupc->got_ipa_pong = 1;
200 }
201
202 msgb_free(msg);
203 return 0;
204 }
205
206 if (hh->proto != IPAC_PROTO_OSMO)
207 goto invalid;
208
209 if (!he || msgb_l2len(msg) < sizeof(*he))
210 goto invalid;
211
212 msg->l2h = &he->data[0];
213
214 if (he->proto == IPAC_PROTO_EXT_GSUP) {
215 OSMO_ASSERT(gsupc->read_cb != NULL);
216 gsupc->read_cb(gsupc, msg);
217 /* expecting read_cb() to free msg */
218 } else if (he->proto == IPAC_PROTO_EXT_OAP) {
219 return gsup_client_oap_handle(gsupc, msg);
220 /* gsup_client_oap_handle frees msg */
221 } else
222 goto invalid;
223
224 return 0;
225
226invalid:
227 LOGP(DLGSUP, LOGL_NOTICE,
228 "GSUP received an invalid IPA message from %s:%d, size = %d\n",
229 link->addr, link->port, msgb_length(msg));
230
231 msgb_free(msg);
232 return -1;
233}
234
235static void ping_timer_cb(void *gsupc_)
236{
Harald Welte953d27c2018-07-23 11:11:22 +0200237 struct osmo_gsup_client *gsupc = gsupc_;
Harald Welteec6915a2018-07-23 14:25:33 +0200238
239 LOGP(DLGSUP, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n",
240 gsupc->is_connected ? "connected" : "not connected",
241 gsupc->got_ipa_pong ? "got" : "didn't get");
242
243 if (gsupc->got_ipa_pong) {
244 start_test_procedure(gsupc);
245 return;
246 }
247
248 LOGP(DLGSUP, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n");
249 ipa_client_conn_close(gsupc->link);
250 gsupc->is_connected = 0;
251
252 gsup_client_connect(gsupc);
253}
254
Harald Welte953d27c2018-07-23 11:11:22 +0200255static void start_test_procedure(struct osmo_gsup_client *gsupc)
Harald Welteec6915a2018-07-23 14:25:33 +0200256{
257 osmo_timer_setup(&gsupc->ping_timer, ping_timer_cb, gsupc);
258
259 gsupc->got_ipa_pong = 0;
Harald Welte953d27c2018-07-23 11:11:22 +0200260 osmo_timer_schedule(&gsupc->ping_timer, OSMO_GSUP_CLIENT_PING_INTERVAL, 0);
Harald Welteec6915a2018-07-23 14:25:33 +0200261 LOGP(DLGSUP, LOGL_DEBUG, "GSUP sending PING\n");
262 gsup_client_send_ping(gsupc);
263}
264
Harald Welte953d27c2018-07-23 11:11:22 +0200265struct osmo_gsup_client *osmo_gsup_client_create(void *talloc_ctx,
266 const char *unit_name,
267 const char *ip_addr,
268 unsigned int tcp_port,
269 osmo_gsup_client_read_cb_t read_cb,
270 struct osmo_oap_client_config *oapc_config)
Harald Welteec6915a2018-07-23 14:25:33 +0200271{
Harald Welte953d27c2018-07-23 11:11:22 +0200272 struct osmo_gsup_client *gsupc;
Harald Welteec6915a2018-07-23 14:25:33 +0200273 int rc;
274
Harald Welte953d27c2018-07-23 11:11:22 +0200275 gsupc = talloc_zero(talloc_ctx, struct osmo_gsup_client);
Harald Welteec6915a2018-07-23 14:25:33 +0200276 OSMO_ASSERT(gsupc);
277
278 /* struct ipaccess_unit has a non-const unit_name, so let's copy to be
279 * able to have a non-const unit_name here as well. To not taint the
280 * public gsup_client API, let's store it in a const char* anyway. */
281 gsupc->unit_name = talloc_strdup(gsupc, unit_name);
282 OSMO_ASSERT(gsupc->unit_name);
283
284 /* a NULL oapc_config will mark oap_state disabled. */
285 rc = osmo_oap_client_init(oapc_config, &gsupc->oap_state);
286 if (rc != 0)
287 goto failed;
288
289 gsupc->link = ipa_client_conn_create(gsupc,
290 /* no e1inp */ NULL,
291 0,
292 ip_addr, tcp_port,
293 gsup_client_updown_cb,
294 gsup_client_read_cb,
295 /* default write_cb */ NULL,
296 gsupc);
297 if (!gsupc->link)
298 goto failed;
299
300 osmo_timer_setup(&gsupc->connect_timer, connect_timer_cb, gsupc);
301
302 rc = gsup_client_connect(gsupc);
303
304 if (rc < 0)
305 goto failed;
306
307 gsupc->read_cb = read_cb;
308
309 return gsupc;
310
311failed:
Harald Welte953d27c2018-07-23 11:11:22 +0200312 osmo_gsup_client_destroy(gsupc);
Harald Welteec6915a2018-07-23 14:25:33 +0200313 return NULL;
314}
315
Harald Welte953d27c2018-07-23 11:11:22 +0200316void osmo_gsup_client_destroy(struct osmo_gsup_client *gsupc)
Harald Welteec6915a2018-07-23 14:25:33 +0200317{
318 osmo_timer_del(&gsupc->connect_timer);
319 osmo_timer_del(&gsupc->ping_timer);
320
321 if (gsupc->link) {
322 ipa_client_conn_close(gsupc->link);
323 ipa_client_conn_destroy(gsupc->link);
324 gsupc->link = NULL;
325 }
326 talloc_free(gsupc);
327}
328
Harald Welte953d27c2018-07-23 11:11:22 +0200329int osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg)
Harald Welteec6915a2018-07-23 14:25:33 +0200330{
331 if (!gsupc || !gsupc->is_connected) {
332 LOGP(DLGSUP, LOGL_ERROR, "GSUP not connected, unable to send %s\n", msgb_hexdump(msg));
333 msgb_free(msg);
334 return -ENOTCONN;
335 }
336
337 client_send(gsupc, IPAC_PROTO_EXT_GSUP, msg);
338
339 return 0;
340}
341
Harald Welte953d27c2018-07-23 11:11:22 +0200342struct msgb *osmo_gsup_client_msgb_alloc(void)
Harald Welteec6915a2018-07-23 14:25:33 +0200343{
344 return msgb_alloc_headroom(4000, 64, __func__);
345}