blob: 7bf67b7d9d6b2dd3ca3f97ea75b0985016849d83 [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;
108
109 if (hh->proto != IPAC_PROTO_OSMO)
110 goto invalid;
111
112 if (!he || msgb_l2len(msg) < sizeof(*he) ||
113 he->proto != IPAC_PROTO_EXT_GSUP)
114 goto invalid;
115
116 msg->l2h = &he->data[0];
117
118 OSMO_ASSERT(gsupc->read_cb != NULL);
119 gsupc->read_cb(gsupc, msg);
120
121 /* Not freeing msg here, because that must be done by the read_cb. */
122 return 0;
123
124invalid:
125 LOGP(DGPRS, LOGL_NOTICE,
126 "GSUP received an invalid IPA message from %s:%d, size = %d\n",
127 link->addr, link->port, msgb_length(msg));
128
129 msgb_free(msg);
130 return -1;
131}
132
133struct gprs_gsup_client *gprs_gsup_client_create(const char *ip_addr,
134 unsigned int tcp_port,
135 gprs_gsup_read_cb_t read_cb)
136{
137 struct gprs_gsup_client *gsupc;
138 int rc;
139
140 gsupc = talloc_zero(tall_bsc_ctx, struct gprs_gsup_client);
141 OSMO_ASSERT(gsupc);
142
143 gsupc->link = ipa_client_conn_create(gsupc,
144 /* no e1inp */ NULL,
145 0,
146 ip_addr, tcp_port,
147 gsup_client_updown_cb,
148 gsup_client_read_cb,
149 /* default write_cb */ NULL,
150 gsupc);
151 if (!gsupc->link)
152 goto failed;
153
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100154 gsupc->connect_timer.data = gsupc;
155 gsupc->connect_timer.cb = &connect_timer_cb;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100156
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100157 rc = gsup_client_connect(gsupc);
158
159 if (rc < 0 && rc != -EINPROGRESS)
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100160 goto failed;
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100161
162 gsupc->read_cb = read_cb;
163
164 return gsupc;
165
166failed:
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100167 gprs_gsup_client_destroy(gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100168 return NULL;
169}
170
171void gprs_gsup_client_destroy(struct gprs_gsup_client *gsupc)
172{
Jacob Erlbeck849d0a82014-12-18 15:00:29 +0100173 osmo_timer_del(&gsupc->connect_timer);
174
175 if (gsupc->link) {
176 ipa_client_conn_close(gsupc->link);
177 ipa_client_conn_destroy(gsupc->link);
178 gsupc->link = NULL;
179 }
180 talloc_free(gsupc);
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100181}
182
183int gprs_gsup_client_send(struct gprs_gsup_client *gsupc, struct msgb *msg)
184{
185 if (!gsupc) {
186 msgb_free(msg);
187 return -ENOTCONN;
188 }
189
Jacob Erlbeck4188c302014-12-19 18:50:05 +0100190 if (!gsupc->is_connected) {
191 msgb_free(msg);
192 return -EAGAIN;
193 }
194
Jacob Erlbeckbb23dc12014-12-18 12:28:21 +0100195 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_GSUP);
196 ipa_msg_push_header(msg, IPAC_PROTO_OSMO);
197 ipa_client_conn_send(gsupc->link, msg);
198
199 return 0;
200}
201
202struct msgb *gprs_gsup_msgb_alloc(void)
203{
204 return msgb_alloc_headroom(4000, 64, __func__);
205}