Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 1 | /* 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> |
Harald Welte | 4f8e34b | 2016-05-06 23:27:38 +0200 | [diff] [blame^] | 28 | #include <osmocom/core/logging.h> |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 29 | |
| 30 | #include <openbsc/debug.h> |
| 31 | |
| 32 | #include <errno.h> |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 33 | #include <string.h> |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 34 | |
| 35 | extern void *tall_bsc_ctx; |
| 36 | |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 37 | static void start_test_procedure(struct gprs_gsup_client *gsupc); |
| 38 | |
| 39 | static void gsup_client_send_ping(struct gprs_gsup_client *gsupc) |
| 40 | { |
| 41 | struct msgb *msg = gprs_gsup_msgb_alloc(); |
| 42 | |
| 43 | msg->l2h = msgb_put(msg, 1); |
| 44 | msg->l2h[0] = IPAC_MSGT_PING; |
| 45 | ipa_msg_push_header(msg, IPAC_PROTO_IPACCESS); |
| 46 | ipa_client_conn_send(gsupc->link, msg); |
| 47 | } |
| 48 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 49 | static int gsup_client_connect(struct gprs_gsup_client *gsupc) |
| 50 | { |
| 51 | int rc; |
| 52 | |
| 53 | if (gsupc->is_connected) |
| 54 | return 0; |
| 55 | |
| 56 | if (osmo_timer_pending(&gsupc->connect_timer)) { |
| 57 | LOGP(DLINP, LOGL_DEBUG, |
| 58 | "GSUP connect: connect timer already running\n"); |
| 59 | osmo_timer_del(&gsupc->connect_timer); |
| 60 | } |
| 61 | |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 62 | if (osmo_timer_pending(&gsupc->ping_timer)) { |
| 63 | LOGP(DLINP, LOGL_DEBUG, |
| 64 | "GSUP connect: ping timer already running\n"); |
| 65 | osmo_timer_del(&gsupc->ping_timer); |
| 66 | } |
| 67 | |
Jacob Erlbeck | 4188c30 | 2014-12-19 18:50:05 +0100 | [diff] [blame] | 68 | if (ipa_client_conn_clear_queue(gsupc->link) > 0) |
| 69 | LOGP(DLINP, LOGL_DEBUG, "GSUP connect: discarded stored messages\n"); |
| 70 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 71 | rc = ipa_client_conn_open(gsupc->link); |
| 72 | |
Jacob Erlbeck | 69e16b9 | 2014-12-19 19:00:56 +0100 | [diff] [blame] | 73 | if (rc >= 0) { |
| 74 | LOGP(DGPRS, LOGL_INFO, "GSUP connecting to %s:%d\n", |
| 75 | gsupc->link->addr, gsupc->link->port); |
| 76 | return 0; |
| 77 | } |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 78 | |
| 79 | LOGP(DGPRS, LOGL_INFO, "GSUP failed to connect to %s:%d: %s\n", |
| 80 | gsupc->link->addr, gsupc->link->port, strerror(-rc)); |
| 81 | |
| 82 | if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT || |
| 83 | rc == -EINVAL) |
| 84 | return rc; |
| 85 | |
| 86 | osmo_timer_schedule(&gsupc->connect_timer, GPRS_GSUP_RECONNECT_INTERVAL, 0); |
| 87 | |
Jacob Erlbeck | 3718490 | 2015-01-19 10:56:15 +0100 | [diff] [blame] | 88 | LOGP(DGPRS, LOGL_INFO, "Scheduled timer to retry GSUP connect to %s:%d\n", |
| 89 | gsupc->link->addr, gsupc->link->port); |
| 90 | |
| 91 | return 0; |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static void connect_timer_cb(void *gsupc_) |
| 95 | { |
| 96 | struct gprs_gsup_client *gsupc = gsupc_; |
| 97 | |
| 98 | if (gsupc->is_connected) |
| 99 | return; |
| 100 | |
| 101 | gsup_client_connect(gsupc); |
| 102 | } |
| 103 | |
Neels Hofmeyr | 06cfe00 | 2015-10-01 15:23:51 +0200 | [diff] [blame] | 104 | static void gsup_client_send(struct gprs_gsup_client *gsupc, int proto_ext, struct msgb *msg_tx) |
| 105 | { |
| 106 | ipa_prepend_header_ext(msg_tx, proto_ext); |
| 107 | ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO); |
| 108 | ipa_client_conn_send(gsupc->link, msg_tx); |
| 109 | /* msg_tx is now queued and will be freed. */ |
| 110 | } |
| 111 | |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 112 | static void gsup_client_oap_register(struct gprs_gsup_client *gsupc) |
| 113 | { |
| 114 | struct msgb *msg_tx; |
| 115 | int rc; |
| 116 | rc = oap_register(&gsupc->oap_state, &msg_tx); |
| 117 | |
| 118 | if ((rc < 0) || (!msg_tx)) { |
| 119 | LOGP(DGPRS, LOGL_ERROR, "GSUP OAP set up, but cannot register.\n"); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | gsup_client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx); |
| 124 | } |
| 125 | |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 126 | static void gsup_client_updown_cb(struct ipa_client_conn *link, int up) |
| 127 | { |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 128 | struct gprs_gsup_client *gsupc = link->data; |
| 129 | |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 130 | LOGP(DGPRS, LOGL_INFO, "GSUP link to %s:%d %s\n", |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 131 | link->addr, link->port, up ? "UP" : "DOWN"); |
| 132 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 133 | gsupc->is_connected = up; |
| 134 | |
| 135 | if (up) { |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 136 | start_test_procedure(gsupc); |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 137 | |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 138 | if (gsupc->oap_state.state == OAP_INITIALIZED) |
| 139 | gsup_client_oap_register(gsupc); |
| 140 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 141 | osmo_timer_del(&gsupc->connect_timer); |
| 142 | } else { |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 143 | osmo_timer_del(&gsupc->ping_timer); |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 144 | |
| 145 | osmo_timer_schedule(&gsupc->connect_timer, |
| 146 | GPRS_GSUP_RECONNECT_INTERVAL, 0); |
| 147 | } |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 150 | static int gsup_client_oap_handle(struct gprs_gsup_client *gsupc, struct msgb *msg_rx) |
| 151 | { |
| 152 | int rc; |
| 153 | struct msgb *msg_tx; |
| 154 | |
| 155 | rc = oap_handle(&gsupc->oap_state, msg_rx, &msg_tx); |
| 156 | msgb_free(msg_rx); |
| 157 | if (rc < 0) |
| 158 | return rc; |
| 159 | |
| 160 | if (msg_tx) |
| 161 | gsup_client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 166 | static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg) |
| 167 | { |
| 168 | struct ipaccess_head *hh = (struct ipaccess_head *) msg->data; |
| 169 | struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg); |
| 170 | struct gprs_gsup_client *gsupc = (struct gprs_gsup_client *)link->data; |
Jacob Erlbeck | e154d8b | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 171 | int rc; |
| 172 | static struct ipaccess_unit ipa_dev = { |
| 173 | .unit_name = "SGSN" |
| 174 | }; |
| 175 | |
| 176 | msg->l2h = &hh->data[0]; |
| 177 | |
| 178 | rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg); |
| 179 | |
| 180 | if (rc < 0) { |
| 181 | LOGP(DGPRS, LOGL_NOTICE, |
| 182 | "GSUP received an invalid IPA/CCM message from %s:%d\n", |
| 183 | link->addr, link->port); |
| 184 | /* Link has been closed */ |
| 185 | gsupc->is_connected = 0; |
| 186 | msgb_free(msg); |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | if (rc == 1) { |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 191 | uint8_t msg_type = *(msg->l2h); |
Jacob Erlbeck | e154d8b | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 192 | /* CCM message */ |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 193 | if (msg_type == IPAC_MSGT_PONG) { |
| 194 | LOGP(DGPRS, LOGL_DEBUG, "GSUP receiving PONG\n"); |
| 195 | gsupc->got_ipa_pong = 1; |
| 196 | } |
Jacob Erlbeck | e154d8b | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 197 | |
| 198 | msgb_free(msg); |
| 199 | return 0; |
| 200 | } |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 201 | |
| 202 | if (hh->proto != IPAC_PROTO_OSMO) |
| 203 | goto invalid; |
| 204 | |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 205 | if (!he || msgb_l2len(msg) < sizeof(*he)) |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 206 | goto invalid; |
| 207 | |
| 208 | msg->l2h = &he->data[0]; |
| 209 | |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 210 | if (he->proto == IPAC_PROTO_EXT_GSUP) { |
| 211 | OSMO_ASSERT(gsupc->read_cb != NULL); |
| 212 | gsupc->read_cb(gsupc, msg); |
| 213 | /* expecting read_cb() to free msg */ |
| 214 | } else if (he->proto == IPAC_PROTO_EXT_OAP) { |
| 215 | return gsup_client_oap_handle(gsupc, msg); |
| 216 | /* gsup_client_oap_handle frees msg */ |
| 217 | } else |
| 218 | goto invalid; |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 219 | |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 220 | return 0; |
| 221 | |
| 222 | invalid: |
| 223 | LOGP(DGPRS, LOGL_NOTICE, |
| 224 | "GSUP received an invalid IPA message from %s:%d, size = %d\n", |
| 225 | link->addr, link->port, msgb_length(msg)); |
| 226 | |
| 227 | msgb_free(msg); |
| 228 | return -1; |
| 229 | } |
| 230 | |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 231 | static void ping_timer_cb(void *gsupc_) |
| 232 | { |
| 233 | struct gprs_gsup_client *gsupc = gsupc_; |
| 234 | |
| 235 | LOGP(DGPRS, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n", |
| 236 | gsupc->is_connected ? "connected" : "not connected", |
| 237 | gsupc->got_ipa_pong ? "got" : "didn't get"); |
| 238 | |
| 239 | if (gsupc->got_ipa_pong) { |
| 240 | start_test_procedure(gsupc); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | LOGP(DGPRS, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n"); |
| 245 | ipa_client_conn_close(gsupc->link); |
| 246 | gsupc->is_connected = 0; |
| 247 | |
| 248 | gsup_client_connect(gsupc); |
| 249 | } |
| 250 | |
| 251 | static void start_test_procedure(struct gprs_gsup_client *gsupc) |
| 252 | { |
| 253 | gsupc->ping_timer.data = gsupc; |
| 254 | gsupc->ping_timer.cb = &ping_timer_cb; |
| 255 | |
| 256 | gsupc->got_ipa_pong = 0; |
| 257 | osmo_timer_schedule(&gsupc->ping_timer, GPRS_GSUP_PING_INTERVAL, 0); |
| 258 | LOGP(DGPRS, LOGL_DEBUG, "GSUP sending PING\n"); |
| 259 | gsup_client_send_ping(gsupc); |
| 260 | } |
| 261 | |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 262 | struct gprs_gsup_client *gprs_gsup_client_create(const char *ip_addr, |
| 263 | unsigned int tcp_port, |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 264 | gprs_gsup_read_cb_t read_cb, |
| 265 | struct oap_config *oap_config) |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 266 | { |
| 267 | struct gprs_gsup_client *gsupc; |
| 268 | int rc; |
| 269 | |
| 270 | gsupc = talloc_zero(tall_bsc_ctx, struct gprs_gsup_client); |
| 271 | OSMO_ASSERT(gsupc); |
| 272 | |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 273 | rc = oap_init(oap_config, &gsupc->oap_state); |
| 274 | if (rc != 0) |
| 275 | goto failed; |
| 276 | |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 277 | gsupc->link = ipa_client_conn_create(gsupc, |
| 278 | /* no e1inp */ NULL, |
| 279 | 0, |
| 280 | ip_addr, tcp_port, |
| 281 | gsup_client_updown_cb, |
| 282 | gsup_client_read_cb, |
| 283 | /* default write_cb */ NULL, |
| 284 | gsupc); |
| 285 | if (!gsupc->link) |
| 286 | goto failed; |
| 287 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 288 | gsupc->connect_timer.data = gsupc; |
| 289 | gsupc->connect_timer.cb = &connect_timer_cb; |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 290 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 291 | rc = gsup_client_connect(gsupc); |
| 292 | |
Jacob Erlbeck | 3ee67ff | 2015-01-26 09:22:39 +0100 | [diff] [blame] | 293 | if (rc < 0) |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 294 | goto failed; |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 295 | |
| 296 | gsupc->read_cb = read_cb; |
| 297 | |
| 298 | return gsupc; |
| 299 | |
| 300 | failed: |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 301 | gprs_gsup_client_destroy(gsupc); |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 302 | return NULL; |
| 303 | } |
| 304 | |
| 305 | void gprs_gsup_client_destroy(struct gprs_gsup_client *gsupc) |
| 306 | { |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 307 | osmo_timer_del(&gsupc->connect_timer); |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 308 | osmo_timer_del(&gsupc->ping_timer); |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 309 | |
| 310 | if (gsupc->link) { |
| 311 | ipa_client_conn_close(gsupc->link); |
| 312 | ipa_client_conn_destroy(gsupc->link); |
| 313 | gsupc->link = NULL; |
| 314 | } |
| 315 | talloc_free(gsupc); |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | int gprs_gsup_client_send(struct gprs_gsup_client *gsupc, struct msgb *msg) |
| 319 | { |
| 320 | if (!gsupc) { |
| 321 | msgb_free(msg); |
| 322 | return -ENOTCONN; |
| 323 | } |
| 324 | |
Jacob Erlbeck | 4188c30 | 2014-12-19 18:50:05 +0100 | [diff] [blame] | 325 | if (!gsupc->is_connected) { |
| 326 | msgb_free(msg); |
| 327 | return -EAGAIN; |
| 328 | } |
| 329 | |
Neels Hofmeyr | 06cfe00 | 2015-10-01 15:23:51 +0200 | [diff] [blame] | 330 | gsup_client_send(gsupc, IPAC_PROTO_EXT_GSUP, msg); |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | struct msgb *gprs_gsup_msgb_alloc(void) |
| 336 | { |
| 337 | return msgb_alloc_headroom(4000, 64, __func__); |
| 338 | } |