blob: 48357b475a59bcff97d29094b09643aea2319afc [file] [log] [blame]
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +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
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 Hofmeyr396f2e62017-09-04 15:13:25 +020024#include <osmocom/sgsn/gsup_client.h>
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020025
26#include <osmocom/abis/ipa.h>
27#include <osmocom/gsm/protocol/ipaccess.h>
28#include <osmocom/core/msgb.h>
29#include <osmocom/core/logging.h>
30
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020031#include <osmocom/sgsn/debug.h>
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020032
33#include <errno.h>
34#include <string.h>
35
36extern void *tall_bsc_ctx;
37
38static void start_test_procedure(struct gsup_client *gsupc);
39
40static void gsup_client_send_ping(struct gsup_client *gsupc)
41{
42 struct msgb *msg = gsup_client_msgb_alloc();
43
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
50static int gsup_client_connect(struct gsup_client *gsupc)
51{
52 int rc;
53
54 if (gsupc->is_connected)
55 return 0;
56
57 if (osmo_timer_pending(&gsupc->connect_timer)) {
58 LOGP(DLGSUP, LOGL_DEBUG,
59 "GSUP connect: connect timer already running\n");
60 osmo_timer_del(&gsupc->connect_timer);
61 }
62
63 if (osmo_timer_pending(&gsupc->ping_timer)) {
64 LOGP(DLGSUP, LOGL_DEBUG,
65 "GSUP connect: ping timer already running\n");
66 osmo_timer_del(&gsupc->ping_timer);
67 }
68
69 if (ipa_client_conn_clear_queue(gsupc->link) > 0)
70 LOGP(DLGSUP, LOGL_DEBUG, "GSUP connect: discarded stored messages\n");
71
72 rc = ipa_client_conn_open(gsupc->link);
73
74 if (rc >= 0) {
75 LOGP(DLGSUP, LOGL_NOTICE, "GSUP connecting to %s:%d\n",
76 gsupc->link->addr, gsupc->link->port);
77 return 0;
78 }
79
80 LOGP(DLGSUP, LOGL_ERROR, "GSUP failed to connect to %s:%d: %s\n",
81 gsupc->link->addr, gsupc->link->port, strerror(-rc));
82
83 if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT ||
84 rc == -EINVAL)
85 return rc;
86
87 osmo_timer_schedule(&gsupc->connect_timer,
88 GSUP_CLIENT_RECONNECT_INTERVAL, 0);
89
90 LOGP(DLGSUP, LOGL_INFO, "Scheduled timer to retry GSUP connect to %s:%d\n",
91 gsupc->link->addr, gsupc->link->port);
92
93 return 0;
94}
95
96static void connect_timer_cb(void *gsupc_)
97{
98 struct gsup_client *gsupc = gsupc_;
99
100 if (gsupc->is_connected)
101 return;
102
103 gsup_client_connect(gsupc);
104}
105
106static void client_send(struct gsup_client *gsupc, int proto_ext,
107 struct msgb *msg_tx)
108{
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
115static void gsup_client_oap_register(struct gsup_client *gsupc)
116{
117 struct msgb *msg_tx;
118 int rc;
119 rc = oap_client_register(&gsupc->oap_state, &msg_tx);
120
121 if ((rc < 0) || (!msg_tx)) {
122 LOGP(DLGSUP, LOGL_ERROR, "GSUP OAP set up, but cannot register.\n");
123 return;
124 }
125
126 client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
127}
128
129static void gsup_client_updown_cb(struct ipa_client_conn *link, int up)
130{
131 struct gsup_client *gsupc = link->data;
132
133 LOGP(DLGSUP, LOGL_INFO, "GSUP link to %s:%d %s\n",
134 link->addr, link->port, up ? "UP" : "DOWN");
135
136 gsupc->is_connected = up;
137
138 if (up) {
139 start_test_procedure(gsupc);
140
141 if (gsupc->oap_state.state == OAP_INITIALIZED)
142 gsup_client_oap_register(gsupc);
143
144 osmo_timer_del(&gsupc->connect_timer);
145 } else {
146 osmo_timer_del(&gsupc->ping_timer);
147
148 osmo_timer_schedule(&gsupc->connect_timer,
149 GSUP_CLIENT_RECONNECT_INTERVAL, 0);
150 }
151}
152
153static int gsup_client_oap_handle(struct gsup_client *gsupc, struct msgb *msg_rx)
154{
155 int rc;
156 struct msgb *msg_tx;
157
158 /* If the oap_state is disabled, this will reject the messages. */
159 rc = oap_client_handle(&gsupc->oap_state, msg_rx, &msg_tx);
160 msgb_free(msg_rx);
161 if (rc < 0)
162 return rc;
163
164 if (msg_tx)
165 client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
166
167 return 0;
168}
169
170static 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);
174 struct gsup_client *gsupc = (struct gsup_client *)link->data;
175 int rc;
176 struct ipaccess_unit ipa_dev = {
177 /* see gsup_client_create() on const vs non-const */
178 .unit_name = (char*)gsupc->unit_name,
179 };
180
181 OSMO_ASSERT(ipa_dev.unit_name);
182
183 msg->l2h = &hh->data[0];
184
185 rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg);
186
187 if (rc < 0) {
188 LOGP(DLGSUP, LOGL_NOTICE,
189 "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) {
198 uint8_t msg_type = *(msg->l2h);
199 /* CCM message */
200 if (msg_type == IPAC_MSGT_PONG) {
201 LOGP(DLGSUP, LOGL_DEBUG, "GSUP receiving PONG\n");
202 gsupc->got_ipa_pong = 1;
203 }
204
205 msgb_free(msg);
206 return 0;
207 }
208
209 if (hh->proto != IPAC_PROTO_OSMO)
210 goto invalid;
211
212 if (!he || msgb_l2len(msg) < sizeof(*he))
213 goto invalid;
214
215 msg->l2h = &he->data[0];
216
217 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;
226
227 return 0;
228
229invalid:
230 LOGP(DLGSUP, LOGL_NOTICE,
231 "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
238static void ping_timer_cb(void *gsupc_)
239{
240 struct gsup_client *gsupc = gsupc_;
241
242 LOGP(DLGSUP, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n",
243 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
251 LOGP(DLGSUP, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n");
252 ipa_client_conn_close(gsupc->link);
253 gsupc->is_connected = 0;
254
255 gsup_client_connect(gsupc);
256}
257
258static void start_test_procedure(struct gsup_client *gsupc)
259{
260 osmo_timer_setup(&gsupc->ping_timer, ping_timer_cb, gsupc);
261
262 gsupc->got_ipa_pong = 0;
263 osmo_timer_schedule(&gsupc->ping_timer, GSUP_CLIENT_PING_INTERVAL, 0);
264 LOGP(DLGSUP, LOGL_DEBUG, "GSUP sending PING\n");
265 gsup_client_send_ping(gsupc);
266}
267
268struct gsup_client *gsup_client_create(const char *unit_name,
269 const char *ip_addr,
270 unsigned int tcp_port,
271 gsup_client_read_cb_t read_cb,
272 struct oap_client_config *oapc_config)
273{
274 struct gsup_client *gsupc;
275 int rc;
276
277 gsupc = talloc_zero(tall_bsc_ctx, struct gsup_client);
278 OSMO_ASSERT(gsupc);
279
280 /* 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
286 /* a NULL oapc_config will mark oap_state disabled. */
287 rc = oap_client_init(oapc_config, &gsupc->oap_state);
288 if (rc != 0)
289 goto failed;
290
291 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
302 osmo_timer_setup(&gsupc->connect_timer, connect_timer_cb, gsupc);
303
304 rc = gsup_client_connect(gsupc);
305
306 if (rc < 0)
307 goto failed;
308
309 gsupc->read_cb = read_cb;
310
311 return gsupc;
312
313failed:
314 gsup_client_destroy(gsupc);
315 return NULL;
316}
317
318void gsup_client_destroy(struct gsup_client *gsupc)
319{
320 osmo_timer_del(&gsupc->connect_timer);
321 osmo_timer_del(&gsupc->ping_timer);
322
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);
329}
330
331int gsup_client_send(struct gsup_client *gsupc, struct msgb *msg)
332{
333 if (!gsupc) {
334 LOGP(DGPRS, LOGL_NOTICE, "No GSUP client, unable to "
335 "send %s\n", msgb_hexdump(msg));
336 msgb_free(msg);
337 return -ENOTCONN;
338 }
339
340 if (!gsupc->is_connected) {
341 LOGP(DGPRS, LOGL_NOTICE, "GSUP not connected, unable to "
342 "send %s\n", msgb_hexdump(msg));
343 msgb_free(msg);
344 return -EAGAIN;
345 }
346
347 client_send(gsupc, IPAC_PROTO_EXT_GSUP, msg);
348
349 return 0;
350}
351
352struct msgb *gsup_client_msgb_alloc(void)
353{
354 return msgb_alloc_headroom(4000, 64, __func__);
355}