blob: 807b0c11378b5c9c3b0af6e536e2c702551abfb6 [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
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100103static void gsup_client_updown_cb(struct ipa_client_conn *link, int up)
104{
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100105 struct gprs_gsup_client *gsupc = link->data;
106
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100107 LOGP(DGPRS, LOGL_INFO, "GSUP link to %s:%d %s\n",
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100108 link->addr, link->port, up ? "UP" : "DOWN");
109
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100110 gsupc->is_connected = up;
111
112 if (up) {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100113 start_test_procedure(gsupc);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100114
115 osmo_timer_del(&gsupc->connect_timer);
116 } else {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100117 osmo_timer_del(&gsupc->ping_timer);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100118
119 osmo_timer_schedule(&gsupc->connect_timer,
120 GPRS_GSUP_RECONNECT_INTERVAL, 0);
121 }
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100122}
123
124static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg)
125{
126 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
127 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
128 struct gprs_gsup_client *gsupc = (struct gprs_gsup_client *)link->data;
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100129 int rc;
130 static struct ipaccess_unit ipa_dev = {
131 .unit_name = "SGSN"
132 };
133
134 msg->l2h = &hh->data[0];
135
136 rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg);
137
138 if (rc < 0) {
139 LOGP(DGPRS, LOGL_NOTICE,
140 "GSUP received an invalid IPA/CCM message from %s:%d\n",
141 link->addr, link->port);
142 /* Link has been closed */
143 gsupc->is_connected = 0;
144 msgb_free(msg);
145 return -1;
146 }
147
148 if (rc == 1) {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100149 uint8_t msg_type = *(msg->l2h);
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100150 /* CCM message */
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100151 if (msg_type == IPAC_MSGT_PONG) {
152 LOGP(DGPRS, LOGL_DEBUG, "GSUP receiving PONG\n");
153 gsupc->got_ipa_pong = 1;
154 }
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100155
156 msgb_free(msg);
157 return 0;
158 }
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100159
160 if (hh->proto != IPAC_PROTO_OSMO)
161 goto invalid;
162
163 if (!he || msgb_l2len(msg) < sizeof(*he) ||
164 he->proto != IPAC_PROTO_EXT_GSUP)
165 goto invalid;
166
167 msg->l2h = &he->data[0];
168
169 OSMO_ASSERT(gsupc->read_cb != NULL);
170 gsupc->read_cb(gsupc, msg);
171
172 /* Not freeing msg here, because that must be done by the read_cb. */
173 return 0;
174
175invalid:
176 LOGP(DGPRS, LOGL_NOTICE,
177 "GSUP received an invalid IPA message from %s:%d, size = %d\n",
178 link->addr, link->port, msgb_length(msg));
179
180 msgb_free(msg);
181 return -1;
182}
183
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100184static void ping_timer_cb(void *gsupc_)
185{
186 struct gprs_gsup_client *gsupc = gsupc_;
187
188 LOGP(DGPRS, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n",
189 gsupc->is_connected ? "connected" : "not connected",
190 gsupc->got_ipa_pong ? "got" : "didn't get");
191
192 if (gsupc->got_ipa_pong) {
193 start_test_procedure(gsupc);
194 return;
195 }
196
197 LOGP(DGPRS, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n");
198 ipa_client_conn_close(gsupc->link);
199 gsupc->is_connected = 0;
200
201 gsup_client_connect(gsupc);
202}
203
204static void start_test_procedure(struct gprs_gsup_client *gsupc)
205{
206 gsupc->ping_timer.data = gsupc;
207 gsupc->ping_timer.cb = &ping_timer_cb;
208
209 gsupc->got_ipa_pong = 0;
210 osmo_timer_schedule(&gsupc->ping_timer, GPRS_GSUP_PING_INTERVAL, 0);
211 LOGP(DGPRS, LOGL_DEBUG, "GSUP sending PING\n");
212 gsup_client_send_ping(gsupc);
213}
214
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100215struct gprs_gsup_client *gprs_gsup_client_create(const char *ip_addr,
216 unsigned int tcp_port,
217 gprs_gsup_read_cb_t read_cb)
218{
219 struct gprs_gsup_client *gsupc;
220 int rc;
221
222 gsupc = talloc_zero(tall_bsc_ctx, struct gprs_gsup_client);
223 OSMO_ASSERT(gsupc);
224
225 gsupc->link = ipa_client_conn_create(gsupc,
226 /* no e1inp */ NULL,
227 0,
228 ip_addr, tcp_port,
229 gsup_client_updown_cb,
230 gsup_client_read_cb,
231 /* default write_cb */ NULL,
232 gsupc);
233 if (!gsupc->link)
234 goto failed;
235
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100236 gsupc->connect_timer.data = gsupc;
237 gsupc->connect_timer.cb = &connect_timer_cb;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100238
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100239 rc = gsup_client_connect(gsupc);
240
Jacob Erlbeck3ee67ff2015-01-26 09:22:39 +0100241 if (rc < 0)
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100242 goto failed;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100243
244 gsupc->read_cb = read_cb;
245
246 return gsupc;
247
248failed:
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100249 gprs_gsup_client_destroy(gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100250 return NULL;
251}
252
253void gprs_gsup_client_destroy(struct gprs_gsup_client *gsupc)
254{
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100255 osmo_timer_del(&gsupc->connect_timer);
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100256 osmo_timer_del(&gsupc->ping_timer);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100257
258 if (gsupc->link) {
259 ipa_client_conn_close(gsupc->link);
260 ipa_client_conn_destroy(gsupc->link);
261 gsupc->link = NULL;
262 }
263 talloc_free(gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100264}
265
266int gprs_gsup_client_send(struct gprs_gsup_client *gsupc, struct msgb *msg)
267{
268 if (!gsupc) {
269 msgb_free(msg);
270 return -ENOTCONN;
271 }
272
Jacob Erlbeck4188c302014-12-19 18:50:05 +0100273 if (!gsupc->is_connected) {
274 msgb_free(msg);
275 return -EAGAIN;
276 }
277
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100278 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_GSUP);
279 ipa_msg_push_header(msg, IPAC_PROTO_OSMO);
280 ipa_client_conn_send(gsupc->link, msg);
281
282 return 0;
283}
284
285struct msgb *gprs_gsup_msgb_alloc(void)
286{
287 return msgb_alloc_headroom(4000, 64, __func__);
288}