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