blob: aaf301755ad879d261efc5b6f7b1b6cd93f6f14d [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
54 if (rc >= 0)
55 return rc;
56
57 LOGP(DGPRS, LOGL_INFO, "GSUP failed to connect to %s:%d: %s\n",
58 gsupc->link->addr, gsupc->link->port, strerror(-rc));
59
60 if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT ||
61 rc == -EINVAL)
62 return rc;
63
64 osmo_timer_schedule(&gsupc->connect_timer, GPRS_GSUP_RECONNECT_INTERVAL, 0);
65
66 return 0;
67}
68
69static void connect_timer_cb(void *gsupc_)
70{
71 struct gprs_gsup_client *gsupc = gsupc_;
72
73 if (gsupc->is_connected)
74 return;
75
76 gsup_client_connect(gsupc);
77}
78
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +010079static void gsup_client_updown_cb(struct ipa_client_conn *link, int up)
80{
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010081 struct gprs_gsup_client *gsupc = link->data;
82
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +010083 LOGP(DGPRS, LOGL_NOTICE, "GSUP link to %s:%d %s\n",
84 link->addr, link->port, up ? "UP" : "DOWN");
85
Jacob Erlbeck849d0a82014-12-18 15:00:29 +010086 gsupc->is_connected = up;
87
88 if (up) {
89 /* TODO: Start ping procedure */
90
91 osmo_timer_del(&gsupc->connect_timer);
92 } else {
93 /* TODO: Stop ping procedure */
94
95 osmo_timer_schedule(&gsupc->connect_timer,
96 GPRS_GSUP_RECONNECT_INTERVAL, 0);
97 }
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +010098}
99
100static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg)
101{
102 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
103 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
104 struct gprs_gsup_client *gsupc = (struct gprs_gsup_client *)link->data;
105
106 if (hh->proto != IPAC_PROTO_OSMO)
107 goto invalid;
108
109 if (!he || msgb_l2len(msg) < sizeof(*he) ||
110 he->proto != IPAC_PROTO_EXT_GSUP)
111 goto invalid;
112
113 msg->l2h = &he->data[0];
114
115 OSMO_ASSERT(gsupc->read_cb != NULL);
116 gsupc->read_cb(gsupc, msg);
117
118 /* Not freeing msg here, because that must be done by the read_cb. */
119 return 0;
120
121invalid:
122 LOGP(DGPRS, LOGL_NOTICE,
123 "GSUP received an invalid IPA message from %s:%d, size = %d\n",
124 link->addr, link->port, msgb_length(msg));
125
126 msgb_free(msg);
127 return -1;
128}
129
130struct gprs_gsup_client *gprs_gsup_client_create(const char *ip_addr,
131 unsigned int tcp_port,
132 gprs_gsup_read_cb_t read_cb)
133{
134 struct gprs_gsup_client *gsupc;
135 int rc;
136
137 gsupc = talloc_zero(tall_bsc_ctx, struct gprs_gsup_client);
138 OSMO_ASSERT(gsupc);
139
140 gsupc->link = ipa_client_conn_create(gsupc,
141 /* no e1inp */ NULL,
142 0,
143 ip_addr, tcp_port,
144 gsup_client_updown_cb,
145 gsup_client_read_cb,
146 /* default write_cb */ NULL,
147 gsupc);
148 if (!gsupc->link)
149 goto failed;
150
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100151 gsupc->connect_timer.data = gsupc;
152 gsupc->connect_timer.cb = &connect_timer_cb;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100153
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100154 rc = gsup_client_connect(gsupc);
155
156 if (rc < 0 && rc != -EINPROGRESS)
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100157 goto failed;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100158
159 gsupc->read_cb = read_cb;
160
161 return gsupc;
162
163failed:
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100164 gprs_gsup_client_destroy(gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100165 return NULL;
166}
167
168void gprs_gsup_client_destroy(struct gprs_gsup_client *gsupc)
169{
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100170 osmo_timer_del(&gsupc->connect_timer);
171
172 if (gsupc->link) {
173 ipa_client_conn_close(gsupc->link);
174 ipa_client_conn_destroy(gsupc->link);
175 gsupc->link = NULL;
176 }
177 talloc_free(gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100178}
179
180int gprs_gsup_client_send(struct gprs_gsup_client *gsupc, struct msgb *msg)
181{
182 if (!gsupc) {
183 msgb_free(msg);
184 return -ENOTCONN;
185 }
186
Jacob Erlbeck4188c302014-12-19 18:50:05 +0100187 if (!gsupc->is_connected) {
188 msgb_free(msg);
189 return -EAGAIN;
190 }
191
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100192 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_GSUP);
193 ipa_msg_push_header(msg, IPAC_PROTO_OSMO);
194 ipa_client_conn_send(gsupc->link, msg);
195
196 return 0;
197}
198
199struct msgb *gprs_gsup_msgb_alloc(void)
200{
201 return msgb_alloc_headroom(4000, 64, __func__);
202}