blob: 98654295314b540ee319a4378642c383ba7f417e [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 Erlbeck849d0a82014-12-18 15:00:29 +010036static int gsup_client_connect(struct gprs_gsup_client *gsupc)
37{
38 int rc;
39
40 if (gsupc->is_connected)
41 return 0;
42
43 if (osmo_timer_pending(&gsupc->connect_timer)) {
44 LOGP(DLINP, LOGL_DEBUG,
45 "GSUP connect: connect timer already running\n");
46 osmo_timer_del(&gsupc->connect_timer);
47 }
48
Jacob Erlbeck4188c302014-12-19 18:50:05 +010049 if (ipa_client_conn_clear_queue(gsupc->link) > 0)
50 LOGP(DLINP, LOGL_DEBUG, "GSUP connect: discarded stored messages\n");
51
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010052 rc = ipa_client_conn_open(gsupc->link);
53
Jacob Erlbeck69e16b92014-12-19 19:00:56 +010054 if (rc >= 0) {
55 LOGP(DGPRS, LOGL_INFO, "GSUP connecting to %s:%d\n",
56 gsupc->link->addr, gsupc->link->port);
57 return 0;
58 }
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010059
60 LOGP(DGPRS, LOGL_INFO, "GSUP failed to connect to %s:%d: %s\n",
61 gsupc->link->addr, gsupc->link->port, strerror(-rc));
62
63 if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT ||
64 rc == -EINVAL)
65 return rc;
66
67 osmo_timer_schedule(&gsupc->connect_timer, GPRS_GSUP_RECONNECT_INTERVAL, 0);
68
Jacob Erlbeck69e16b92014-12-19 19:00:56 +010069 return rc;
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010070}
71
72static void connect_timer_cb(void *gsupc_)
73{
74 struct gprs_gsup_client *gsupc = gsupc_;
75
76 if (gsupc->is_connected)
77 return;
78
79 gsup_client_connect(gsupc);
80}
81
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +010082static void gsup_client_updown_cb(struct ipa_client_conn *link, int up)
83{
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010084 struct gprs_gsup_client *gsupc = link->data;
85
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +010086 LOGP(DGPRS, LOGL_NOTICE, "GSUP link to %s:%d %s\n",
87 link->addr, link->port, up ? "UP" : "DOWN");
88
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010089 gsupc->is_connected = up;
90
91 if (up) {
92 /* TODO: Start ping procedure */
93
94 osmo_timer_del(&gsupc->connect_timer);
95 } else {
96 /* TODO: Stop ping procedure */
97
98 osmo_timer_schedule(&gsupc->connect_timer,
99 GPRS_GSUP_RECONNECT_INTERVAL, 0);
100 }
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100101}
102
103static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg)
104{
105 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
106 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
107 struct gprs_gsup_client *gsupc = (struct gprs_gsup_client *)link->data;
Jacob Erlbecke154d8b2014-12-19 19:15:55 +0100108 int rc;
109 static struct ipaccess_unit ipa_dev = {
110 .unit_name = "SGSN"
111 };
112
113 msg->l2h = &hh->data[0];
114
115 rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg);
116
117 if (rc < 0) {
118 LOGP(DGPRS, LOGL_NOTICE,
119 "GSUP received an invalid IPA/CCM message from %s:%d\n",
120 link->addr, link->port);
121 /* Link has been closed */
122 gsupc->is_connected = 0;
123 msgb_free(msg);
124 return -1;
125 }
126
127 if (rc == 1) {
128 /* CCM message */
129
130 msgb_free(msg);
131 return 0;
132 }
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100133
134 if (hh->proto != IPAC_PROTO_OSMO)
135 goto invalid;
136
137 if (!he || msgb_l2len(msg) < sizeof(*he) ||
138 he->proto != IPAC_PROTO_EXT_GSUP)
139 goto invalid;
140
141 msg->l2h = &he->data[0];
142
143 OSMO_ASSERT(gsupc->read_cb != NULL);
144 gsupc->read_cb(gsupc, msg);
145
146 /* Not freeing msg here, because that must be done by the read_cb. */
147 return 0;
148
149invalid:
150 LOGP(DGPRS, LOGL_NOTICE,
151 "GSUP received an invalid IPA message from %s:%d, size = %d\n",
152 link->addr, link->port, msgb_length(msg));
153
154 msgb_free(msg);
155 return -1;
156}
157
158struct gprs_gsup_client *gprs_gsup_client_create(const char *ip_addr,
159 unsigned int tcp_port,
160 gprs_gsup_read_cb_t read_cb)
161{
162 struct gprs_gsup_client *gsupc;
163 int rc;
164
165 gsupc = talloc_zero(tall_bsc_ctx, struct gprs_gsup_client);
166 OSMO_ASSERT(gsupc);
167
168 gsupc->link = ipa_client_conn_create(gsupc,
169 /* no e1inp */ NULL,
170 0,
171 ip_addr, tcp_port,
172 gsup_client_updown_cb,
173 gsup_client_read_cb,
174 /* default write_cb */ NULL,
175 gsupc);
176 if (!gsupc->link)
177 goto failed;
178
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100179 gsupc->connect_timer.data = gsupc;
180 gsupc->connect_timer.cb = &connect_timer_cb;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100181
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100182 rc = gsup_client_connect(gsupc);
183
184 if (rc < 0 && rc != -EINPROGRESS)
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100185 goto failed;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100186
187 gsupc->read_cb = read_cb;
188
189 return gsupc;
190
191failed:
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100192 gprs_gsup_client_destroy(gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100193 return NULL;
194}
195
196void gprs_gsup_client_destroy(struct gprs_gsup_client *gsupc)
197{
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100198 osmo_timer_del(&gsupc->connect_timer);
199
200 if (gsupc->link) {
201 ipa_client_conn_close(gsupc->link);
202 ipa_client_conn_destroy(gsupc->link);
203 gsupc->link = NULL;
204 }
205 talloc_free(gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100206}
207
208int gprs_gsup_client_send(struct gprs_gsup_client *gsupc, struct msgb *msg)
209{
210 if (!gsupc) {
211 msgb_free(msg);
212 return -ENOTCONN;
213 }
214
Jacob Erlbeck4188c302014-12-19 18:50:05 +0100215 if (!gsupc->is_connected) {
216 msgb_free(msg);
217 return -EAGAIN;
218 }
219
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100220 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_GSUP);
221 ipa_msg_push_header(msg, IPAC_PROTO_OSMO);
222 ipa_client_conn_send(gsupc->link, msg);
223
224 return 0;
225}
226
227struct msgb *gprs_gsup_msgb_alloc(void)
228{
229 return msgb_alloc_headroom(4000, 64, __func__);
230}