blob: a4aca098c4cc8c4e7f05116c560777f830586f11 [file] [log] [blame]
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +01001/* GPRS Subscriber Update Protocol client */
2
3/* (C) 2014 by Sysmocom s.f.m.c. GmbH
4 * All Rights Reserved
5 *
6 * Author: Jacob Erlbeck
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <openbsc/gprs_gsup_client.h>
24
25#include <osmocom/abis/ipa.h>
26#include <osmocom/gsm/protocol/ipaccess.h>
27#include <osmocom/core/msgb.h>
28
29#include <openbsc/debug.h>
30
31#include <errno.h>
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010032#include <string.h>
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +010033
34extern void *tall_bsc_ctx;
35
Jacob Erlbeck03b46302014-12-19 19:18:54 +010036static void start_test_procedure(struct gprs_gsup_client *gsupc);
37
38static void gsup_client_send_ping(struct gprs_gsup_client *gsupc)
39{
40 struct msgb *msg = gprs_gsup_msgb_alloc();
41
42 msg->l2h = msgb_put(msg, 1);
43 msg->l2h[0] = IPAC_MSGT_PING;
44 ipa_msg_push_header(msg, IPAC_PROTO_IPACCESS);
45 ipa_client_conn_send(gsupc->link, msg);
46}
47
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010048static int gsup_client_connect(struct gprs_gsup_client *gsupc)
49{
50 int rc;
51
52 if (gsupc->is_connected)
53 return 0;
54
55 if (osmo_timer_pending(&gsupc->connect_timer)) {
56 LOGP(DLINP, LOGL_DEBUG,
57 "GSUP connect: connect timer already running\n");
58 osmo_timer_del(&gsupc->connect_timer);
59 }
60
Jacob Erlbeck03b46302014-12-19 19:18:54 +010061 if (osmo_timer_pending(&gsupc->ping_timer)) {
62 LOGP(DLINP, LOGL_DEBUG,
63 "GSUP connect: ping timer already running\n");
64 osmo_timer_del(&gsupc->ping_timer);
65 }
66
Jacob Erlbeck4188c302014-12-19 18:50:05 +010067 if (ipa_client_conn_clear_queue(gsupc->link) > 0)
68 LOGP(DLINP, LOGL_DEBUG, "GSUP connect: discarded stored messages\n");
69
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010070 rc = ipa_client_conn_open(gsupc->link);
71
Jacob Erlbeck69e16b92014-12-19 19:00:56 +010072 if (rc >= 0) {
73 LOGP(DGPRS, LOGL_INFO, "GSUP connecting to %s:%d\n",
74 gsupc->link->addr, gsupc->link->port);
75 return 0;
76 }
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010077
78 LOGP(DGPRS, LOGL_INFO, "GSUP failed to connect to %s:%d: %s\n",
79 gsupc->link->addr, gsupc->link->port, strerror(-rc));
80
81 if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT ||
82 rc == -EINVAL)
83 return rc;
84
85 osmo_timer_schedule(&gsupc->connect_timer, GPRS_GSUP_RECONNECT_INTERVAL, 0);
86
Jacob Erlbeck37184902015-01-19 10:56:15 +010087 LOGP(DGPRS, LOGL_INFO, "Scheduled timer to retry GSUP connect to %s:%d\n",
88 gsupc->link->addr, gsupc->link->port);
89
90 return 0;
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010091}
92
93static void connect_timer_cb(void *gsupc_)
94{
95 struct gprs_gsup_client *gsupc = gsupc_;
96
97 if (gsupc->is_connected)
98 return;
99
100 gsup_client_connect(gsupc);
101}
102
Neels Hofmeyr06cfe002015-10-01 15:23:51 +0200103static void gsup_client_send(struct gprs_gsup_client *gsupc, int proto_ext, struct msgb *msg_tx)
104{
105 ipa_prepend_header_ext(msg_tx, proto_ext);
106 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
107 ipa_client_conn_send(gsupc->link, msg_tx);
108 /* msg_tx is now queued and will be freed. */
109}
110
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200111static void gsup_client_oap_register(struct gprs_gsup_client *gsupc)
112{
113 struct msgb *msg_tx;
114 int rc;
115 rc = oap_register(&gsupc->oap_state, &msg_tx);
116
117 if ((rc < 0) || (!msg_tx)) {
118 LOGP(DGPRS, LOGL_ERROR, "GSUP OAP set up, but cannot register.\n");
119 return;
120 }
121
122 gsup_client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
123}
124
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100125static void gsup_client_updown_cb(struct ipa_client_conn *link, int up)
126{
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100127 struct gprs_gsup_client *gsupc = link->data;
128
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100129 LOGP(DGPRS, LOGL_INFO, "GSUP link to %s:%d %s\n",
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100130 link->addr, link->port, up ? "UP" : "DOWN");
131
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100132 gsupc->is_connected = up;
133
134 if (up) {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100135 start_test_procedure(gsupc);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100136
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200137 if (gsupc->oap_state.state == OAP_INITIALIZED)
138 gsup_client_oap_register(gsupc);
139
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100140 osmo_timer_del(&gsupc->connect_timer);
141 } else {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100142 osmo_timer_del(&gsupc->ping_timer);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100143
144 osmo_timer_schedule(&gsupc->connect_timer,
145 GPRS_GSUP_RECONNECT_INTERVAL, 0);
146 }
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100147}
148
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200149static int gsup_client_oap_handle(struct gprs_gsup_client *gsupc, struct msgb *msg_rx)
150{
151 int rc;
152 struct msgb *msg_tx;
153
154 rc = oap_handle(&gsupc->oap_state, msg_rx, &msg_tx);
155 msgb_free(msg_rx);
156 if (rc < 0)
157 return rc;
158
159 if (msg_tx)
160 gsup_client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx);
161
162 return 0;
163}
164
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100165static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg)
166{
167 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
168 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
169 struct gprs_gsup_client *gsupc = (struct gprs_gsup_client *)link->data;
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100170 int rc;
171 static struct ipaccess_unit ipa_dev = {
172 .unit_name = "SGSN"
173 };
174
175 msg->l2h = &hh->data[0];
176
177 rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg);
178
179 if (rc < 0) {
180 LOGP(DGPRS, LOGL_NOTICE,
181 "GSUP received an invalid IPA/CCM message from %s:%d\n",
182 link->addr, link->port);
183 /* Link has been closed */
184 gsupc->is_connected = 0;
185 msgb_free(msg);
186 return -1;
187 }
188
189 if (rc == 1) {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100190 uint8_t msg_type = *(msg->l2h);
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100191 /* CCM message */
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100192 if (msg_type == IPAC_MSGT_PONG) {
193 LOGP(DGPRS, LOGL_DEBUG, "GSUP receiving PONG\n");
194 gsupc->got_ipa_pong = 1;
195 }
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100196
197 msgb_free(msg);
198 return 0;
199 }
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100200
201 if (hh->proto != IPAC_PROTO_OSMO)
202 goto invalid;
203
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200204 if (!he || msgb_l2len(msg) < sizeof(*he))
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100205 goto invalid;
206
207 msg->l2h = &he->data[0];
208
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200209 if (he->proto == IPAC_PROTO_EXT_GSUP) {
210 OSMO_ASSERT(gsupc->read_cb != NULL);
211 gsupc->read_cb(gsupc, msg);
212 /* expecting read_cb() to free msg */
213 } else if (he->proto == IPAC_PROTO_EXT_OAP) {
214 return gsup_client_oap_handle(gsupc, msg);
215 /* gsup_client_oap_handle frees msg */
216 } else
217 goto invalid;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100218
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100219 return 0;
220
221invalid:
222 LOGP(DGPRS, LOGL_NOTICE,
223 "GSUP received an invalid IPA message from %s:%d, size = %d\n",
224 link->addr, link->port, msgb_length(msg));
225
226 msgb_free(msg);
227 return -1;
228}
229
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100230static void ping_timer_cb(void *gsupc_)
231{
232 struct gprs_gsup_client *gsupc = gsupc_;
233
234 LOGP(DGPRS, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n",
235 gsupc->is_connected ? "connected" : "not connected",
236 gsupc->got_ipa_pong ? "got" : "didn't get");
237
238 if (gsupc->got_ipa_pong) {
239 start_test_procedure(gsupc);
240 return;
241 }
242
243 LOGP(DGPRS, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n");
244 ipa_client_conn_close(gsupc->link);
245 gsupc->is_connected = 0;
246
247 gsup_client_connect(gsupc);
248}
249
250static void start_test_procedure(struct gprs_gsup_client *gsupc)
251{
252 gsupc->ping_timer.data = gsupc;
253 gsupc->ping_timer.cb = &ping_timer_cb;
254
255 gsupc->got_ipa_pong = 0;
256 osmo_timer_schedule(&gsupc->ping_timer, GPRS_GSUP_PING_INTERVAL, 0);
257 LOGP(DGPRS, LOGL_DEBUG, "GSUP sending PING\n");
258 gsup_client_send_ping(gsupc);
259}
260
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100261struct gprs_gsup_client *gprs_gsup_client_create(const char *ip_addr,
262 unsigned int tcp_port,
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200263 gprs_gsup_read_cb_t read_cb,
264 struct oap_config *oap_config)
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100265{
266 struct gprs_gsup_client *gsupc;
267 int rc;
268
269 gsupc = talloc_zero(tall_bsc_ctx, struct gprs_gsup_client);
270 OSMO_ASSERT(gsupc);
271
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +0200272 rc = oap_init(oap_config, &gsupc->oap_state);
273 if (rc != 0)
274 goto failed;
275
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100276 gsupc->link = ipa_client_conn_create(gsupc,
277 /* no e1inp */ NULL,
278 0,
279 ip_addr, tcp_port,
280 gsup_client_updown_cb,
281 gsup_client_read_cb,
282 /* default write_cb */ NULL,
283 gsupc);
284 if (!gsupc->link)
285 goto failed;
286
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100287 gsupc->connect_timer.data = gsupc;
288 gsupc->connect_timer.cb = &connect_timer_cb;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100289
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100290 rc = gsup_client_connect(gsupc);
291
Jacob Erlbeck3ee67ff2015-01-26 09:22:39 +0100292 if (rc < 0)
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100293 goto failed;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100294
295 gsupc->read_cb = read_cb;
296
297 return gsupc;
298
299failed:
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100300 gprs_gsup_client_destroy(gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100301 return NULL;
302}
303
304void gprs_gsup_client_destroy(struct gprs_gsup_client *gsupc)
305{
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100306 osmo_timer_del(&gsupc->connect_timer);
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100307 osmo_timer_del(&gsupc->ping_timer);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100308
309 if (gsupc->link) {
310 ipa_client_conn_close(gsupc->link);
311 ipa_client_conn_destroy(gsupc->link);
312 gsupc->link = NULL;
313 }
314 talloc_free(gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100315}
316
317int gprs_gsup_client_send(struct gprs_gsup_client *gsupc, struct msgb *msg)
318{
319 if (!gsupc) {
320 msgb_free(msg);
321 return -ENOTCONN;
322 }
323
Jacob Erlbeck4188c302014-12-19 18:50:05 +0100324 if (!gsupc->is_connected) {
325 msgb_free(msg);
326 return -EAGAIN;
327 }
328
Neels Hofmeyr06cfe002015-10-01 15:23:51 +0200329 gsup_client_send(gsupc, IPAC_PROTO_EXT_GSUP, msg);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100330
331 return 0;
332}
333
334struct msgb *gprs_gsup_msgb_alloc(void)
335{
336 return msgb_alloc_headroom(4000, 64, __func__);
337}