blob: 1f9e34c83eb0115db93760999b3b4b2cf5d9883c [file] [log] [blame]
Jacob Erlbeck2b2dc9e2014-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 Erlbeck58f75c22014-12-18 15:00:29 +010032#include <string.h>
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +010033
34extern void *tall_bsc_ctx;
35
Jacob Erlbecka93c91c2014-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 Erlbeck58f75c22014-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 Erlbecka93c91c2014-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 Erlbeckad8b7e02014-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 Erlbeck58f75c22014-12-18 15:00:29 +010070 rc = ipa_client_conn_open(gsupc->link);
71
Jacob Erlbeck89a3b2b2014-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 Erlbeck58f75c22014-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 Erlbeck01c4da82015-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 Erlbeck58f75c22014-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 Hofmeyr427a4af2015-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
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100111static void gsup_client_updown_cb(struct ipa_client_conn *link, int up)
112{
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100113 struct gprs_gsup_client *gsupc = link->data;
114
Jacob Erlbecka93c91c2014-12-19 19:18:54 +0100115 LOGP(DGPRS, LOGL_INFO, "GSUP link to %s:%d %s\n",
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100116 link->addr, link->port, up ? "UP" : "DOWN");
117
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100118 gsupc->is_connected = up;
119
120 if (up) {
Jacob Erlbecka93c91c2014-12-19 19:18:54 +0100121 start_test_procedure(gsupc);
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100122
123 osmo_timer_del(&gsupc->connect_timer);
124 } else {
Jacob Erlbecka93c91c2014-12-19 19:18:54 +0100125 osmo_timer_del(&gsupc->ping_timer);
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100126
127 osmo_timer_schedule(&gsupc->connect_timer,
128 GPRS_GSUP_RECONNECT_INTERVAL, 0);
129 }
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100130}
131
132static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg)
133{
134 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
135 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
136 struct gprs_gsup_client *gsupc = (struct gprs_gsup_client *)link->data;
Jacob Erlbeckedb67a12014-12-19 19:15:55 +0100137 int rc;
138 static struct ipaccess_unit ipa_dev = {
139 .unit_name = "SGSN"
140 };
141
142 msg->l2h = &hh->data[0];
143
144 rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg);
145
146 if (rc < 0) {
147 LOGP(DGPRS, LOGL_NOTICE,
148 "GSUP received an invalid IPA/CCM message from %s:%d\n",
149 link->addr, link->port);
150 /* Link has been closed */
151 gsupc->is_connected = 0;
152 msgb_free(msg);
153 return -1;
154 }
155
156 if (rc == 1) {
Jacob Erlbecka93c91c2014-12-19 19:18:54 +0100157 uint8_t msg_type = *(msg->l2h);
Jacob Erlbeckedb67a12014-12-19 19:15:55 +0100158 /* CCM message */
Jacob Erlbecka93c91c2014-12-19 19:18:54 +0100159 if (msg_type == IPAC_MSGT_PONG) {
160 LOGP(DGPRS, LOGL_DEBUG, "GSUP receiving PONG\n");
161 gsupc->got_ipa_pong = 1;
162 }
Jacob Erlbeckedb67a12014-12-19 19:15:55 +0100163
164 msgb_free(msg);
165 return 0;
166 }
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100167
168 if (hh->proto != IPAC_PROTO_OSMO)
169 goto invalid;
170
171 if (!he || msgb_l2len(msg) < sizeof(*he) ||
172 he->proto != IPAC_PROTO_EXT_GSUP)
173 goto invalid;
174
175 msg->l2h = &he->data[0];
176
177 OSMO_ASSERT(gsupc->read_cb != NULL);
178 gsupc->read_cb(gsupc, msg);
179
180 /* Not freeing msg here, because that must be done by the read_cb. */
181 return 0;
182
183invalid:
184 LOGP(DGPRS, LOGL_NOTICE,
185 "GSUP received an invalid IPA message from %s:%d, size = %d\n",
186 link->addr, link->port, msgb_length(msg));
187
188 msgb_free(msg);
189 return -1;
190}
191
Jacob Erlbecka93c91c2014-12-19 19:18:54 +0100192static void ping_timer_cb(void *gsupc_)
193{
194 struct gprs_gsup_client *gsupc = gsupc_;
195
196 LOGP(DGPRS, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n",
197 gsupc->is_connected ? "connected" : "not connected",
198 gsupc->got_ipa_pong ? "got" : "didn't get");
199
200 if (gsupc->got_ipa_pong) {
201 start_test_procedure(gsupc);
202 return;
203 }
204
205 LOGP(DGPRS, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n");
206 ipa_client_conn_close(gsupc->link);
207 gsupc->is_connected = 0;
208
209 gsup_client_connect(gsupc);
210}
211
212static void start_test_procedure(struct gprs_gsup_client *gsupc)
213{
214 gsupc->ping_timer.data = gsupc;
215 gsupc->ping_timer.cb = &ping_timer_cb;
216
217 gsupc->got_ipa_pong = 0;
218 osmo_timer_schedule(&gsupc->ping_timer, GPRS_GSUP_PING_INTERVAL, 0);
219 LOGP(DGPRS, LOGL_DEBUG, "GSUP sending PING\n");
220 gsup_client_send_ping(gsupc);
221}
222
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100223struct gprs_gsup_client *gprs_gsup_client_create(const char *ip_addr,
224 unsigned int tcp_port,
225 gprs_gsup_read_cb_t read_cb)
226{
227 struct gprs_gsup_client *gsupc;
228 int rc;
229
230 gsupc = talloc_zero(tall_bsc_ctx, struct gprs_gsup_client);
231 OSMO_ASSERT(gsupc);
232
233 gsupc->link = ipa_client_conn_create(gsupc,
234 /* no e1inp */ NULL,
235 0,
236 ip_addr, tcp_port,
237 gsup_client_updown_cb,
238 gsup_client_read_cb,
239 /* default write_cb */ NULL,
240 gsupc);
241 if (!gsupc->link)
242 goto failed;
243
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100244 gsupc->connect_timer.data = gsupc;
245 gsupc->connect_timer.cb = &connect_timer_cb;
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100246
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100247 rc = gsup_client_connect(gsupc);
248
Jacob Erlbeck4e20fb32015-01-26 09:22:39 +0100249 if (rc < 0)
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100250 goto failed;
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100251
252 gsupc->read_cb = read_cb;
253
254 return gsupc;
255
256failed:
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100257 gprs_gsup_client_destroy(gsupc);
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100258 return NULL;
259}
260
261void gprs_gsup_client_destroy(struct gprs_gsup_client *gsupc)
262{
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100263 osmo_timer_del(&gsupc->connect_timer);
Jacob Erlbecka93c91c2014-12-19 19:18:54 +0100264 osmo_timer_del(&gsupc->ping_timer);
Jacob Erlbeck58f75c22014-12-18 15:00:29 +0100265
266 if (gsupc->link) {
267 ipa_client_conn_close(gsupc->link);
268 ipa_client_conn_destroy(gsupc->link);
269 gsupc->link = NULL;
270 }
271 talloc_free(gsupc);
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100272}
273
274int gprs_gsup_client_send(struct gprs_gsup_client *gsupc, struct msgb *msg)
275{
276 if (!gsupc) {
277 msgb_free(msg);
278 return -ENOTCONN;
279 }
280
Jacob Erlbeckad8b7e02014-12-19 18:50:05 +0100281 if (!gsupc->is_connected) {
282 msgb_free(msg);
283 return -EAGAIN;
284 }
285
Neels Hofmeyr427a4af2015-10-01 15:23:51 +0200286 gsup_client_send(gsupc, IPAC_PROTO_EXT_GSUP, msg);
Jacob Erlbeck2b2dc9e2014-12-18 12:28:21 +0100287
288 return 0;
289}
290
291struct msgb *gprs_gsup_msgb_alloc(void)
292{
293 return msgb_alloc_headroom(4000, 64, __func__);
294}