blob: 523c541b7805e676bd38630f1cfeedbd6e99040b [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 Erlbeck69e16b92014-12-19 19:00:56 +010087 return rc;
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010088}
89
90static void connect_timer_cb(void *gsupc_)
91{
92 struct gprs_gsup_client *gsupc = gsupc_;
93
94 if (gsupc->is_connected)
95 return;
96
97 gsup_client_connect(gsupc);
98}
99
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100100static void gsup_client_updown_cb(struct ipa_client_conn *link, int up)
101{
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100102 struct gprs_gsup_client *gsupc = link->data;
103
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100104 LOGP(DGPRS, LOGL_INFO, "GSUP link to %s:%d %s\n",
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100105 link->addr, link->port, up ? "UP" : "DOWN");
106
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100107 gsupc->is_connected = up;
108
109 if (up) {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100110 start_test_procedure(gsupc);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100111
112 osmo_timer_del(&gsupc->connect_timer);
113 } else {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100114 osmo_timer_del(&gsupc->ping_timer);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100115
116 osmo_timer_schedule(&gsupc->connect_timer,
117 GPRS_GSUP_RECONNECT_INTERVAL, 0);
118 }
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100119}
120
121static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg)
122{
123 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
124 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
125 struct gprs_gsup_client *gsupc = (struct gprs_gsup_client *)link->data;
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100126 int rc;
127 static struct ipaccess_unit ipa_dev = {
128 .unit_name = "SGSN"
129 };
130
131 msg->l2h = &hh->data[0];
132
133 rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg);
134
135 if (rc < 0) {
136 LOGP(DGPRS, LOGL_NOTICE,
137 "GSUP received an invalid IPA/CCM message from %s:%d\n",
138 link->addr, link->port);
139 /* Link has been closed */
140 gsupc->is_connected = 0;
141 msgb_free(msg);
142 return -1;
143 }
144
145 if (rc == 1) {
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100146 uint8_t msg_type = *(msg->l2h);
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100147 /* CCM message */
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100148 if (msg_type == IPAC_MSGT_PONG) {
149 LOGP(DGPRS, LOGL_DEBUG, "GSUP receiving PONG\n");
150 gsupc->got_ipa_pong = 1;
151 }
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100152
153 msgb_free(msg);
154 return 0;
155 }
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100156
157 if (hh->proto != IPAC_PROTO_OSMO)
158 goto invalid;
159
160 if (!he || msgb_l2len(msg) < sizeof(*he) ||
161 he->proto != IPAC_PROTO_EXT_GSUP)
162 goto invalid;
163
164 msg->l2h = &he->data[0];
165
166 OSMO_ASSERT(gsupc->read_cb != NULL);
167 gsupc->read_cb(gsupc, msg);
168
169 /* Not freeing msg here, because that must be done by the read_cb. */
170 return 0;
171
172invalid:
173 LOGP(DGPRS, LOGL_NOTICE,
174 "GSUP received an invalid IPA message from %s:%d, size = %d\n",
175 link->addr, link->port, msgb_length(msg));
176
177 msgb_free(msg);
178 return -1;
179}
180
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100181static void ping_timer_cb(void *gsupc_)
182{
183 struct gprs_gsup_client *gsupc = gsupc_;
184
185 LOGP(DGPRS, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n",
186 gsupc->is_connected ? "connected" : "not connected",
187 gsupc->got_ipa_pong ? "got" : "didn't get");
188
189 if (gsupc->got_ipa_pong) {
190 start_test_procedure(gsupc);
191 return;
192 }
193
194 LOGP(DGPRS, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n");
195 ipa_client_conn_close(gsupc->link);
196 gsupc->is_connected = 0;
197
198 gsup_client_connect(gsupc);
199}
200
201static void start_test_procedure(struct gprs_gsup_client *gsupc)
202{
203 gsupc->ping_timer.data = gsupc;
204 gsupc->ping_timer.cb = &ping_timer_cb;
205
206 gsupc->got_ipa_pong = 0;
207 osmo_timer_schedule(&gsupc->ping_timer, GPRS_GSUP_PING_INTERVAL, 0);
208 LOGP(DGPRS, LOGL_DEBUG, "GSUP sending PING\n");
209 gsup_client_send_ping(gsupc);
210}
211
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100212struct gprs_gsup_client *gprs_gsup_client_create(const char *ip_addr,
213 unsigned int tcp_port,
214 gprs_gsup_read_cb_t read_cb)
215{
216 struct gprs_gsup_client *gsupc;
217 int rc;
218
219 gsupc = talloc_zero(tall_bsc_ctx, struct gprs_gsup_client);
220 OSMO_ASSERT(gsupc);
221
222 gsupc->link = ipa_client_conn_create(gsupc,
223 /* no e1inp */ NULL,
224 0,
225 ip_addr, tcp_port,
226 gsup_client_updown_cb,
227 gsup_client_read_cb,
228 /* default write_cb */ NULL,
229 gsupc);
230 if (!gsupc->link)
231 goto failed;
232
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100233 gsupc->connect_timer.data = gsupc;
234 gsupc->connect_timer.cb = &connect_timer_cb;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100235
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100236 rc = gsup_client_connect(gsupc);
237
238 if (rc < 0 && rc != -EINPROGRESS)
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100239 goto failed;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100240
241 gsupc->read_cb = read_cb;
242
243 return gsupc;
244
245failed:
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100246 gprs_gsup_client_destroy(gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100247 return NULL;
248}
249
250void gprs_gsup_client_destroy(struct gprs_gsup_client *gsupc)
251{
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100252 osmo_timer_del(&gsupc->connect_timer);
Jacob Erlbeck03b46302014-12-19 19:18:54 +0100253 osmo_timer_del(&gsupc->ping_timer);
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100254
255 if (gsupc->link) {
256 ipa_client_conn_close(gsupc->link);
257 ipa_client_conn_destroy(gsupc->link);
258 gsupc->link = NULL;
259 }
260 talloc_free(gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100261}
262
263int gprs_gsup_client_send(struct gprs_gsup_client *gsupc, struct msgb *msg)
264{
265 if (!gsupc) {
266 msgb_free(msg);
267 return -ENOTCONN;
268 }
269
Jacob Erlbeck4188c302014-12-19 18:50:05 +0100270 if (!gsupc->is_connected) {
271 msgb_free(msg);
272 return -EAGAIN;
273 }
274
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100275 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_GSUP);
276 ipa_msg_push_header(msg, IPAC_PROTO_OSMO);
277 ipa_client_conn_send(gsupc->link, msg);
278
279 return 0;
280}
281
282struct msgb *gprs_gsup_msgb_alloc(void)
283{
284 return msgb_alloc_headroom(4000, 64, __func__);
285}