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