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