Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 1 | /* Generic Subscriber Update Protocol client */ |
| 2 | |
| 3 | /* (C) 2014-2016 by Sysmocom s.f.m.c. GmbH |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * Author: Jacob Erlbeck |
| 7 | * Author: Neels Hofmeyr |
| 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 <osmocom/gsupclient/gsup_client.h> |
| 25 | |
| 26 | #include <osmocom/abis/ipa.h> |
| 27 | #include <osmocom/gsm/oap_client.h> |
| 28 | #include <osmocom/gsm/protocol/ipaccess.h> |
| 29 | #include <osmocom/core/msgb.h> |
| 30 | #include <osmocom/core/logging.h> |
| 31 | |
| 32 | #include <errno.h> |
| 33 | #include <string.h> |
| 34 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 35 | static void start_test_procedure(struct osmo_gsup_client *gsupc); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 36 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 37 | static void gsup_client_send_ping(struct osmo_gsup_client *gsupc) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 38 | { |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 39 | struct msgb *msg = osmo_gsup_client_msgb_alloc(); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 40 | |
| 41 | msg->l2h = msgb_put(msg, 1); |
| 42 | msg->l2h[0] = IPAC_MSGT_PING; |
| 43 | ipa_msg_push_header(msg, IPAC_PROTO_IPACCESS); |
| 44 | ipa_client_conn_send(gsupc->link, msg); |
| 45 | } |
| 46 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 47 | static int gsup_client_connect(struct osmo_gsup_client *gsupc) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 48 | { |
| 49 | int rc; |
| 50 | |
| 51 | if (gsupc->is_connected) |
| 52 | return 0; |
| 53 | |
| 54 | if (osmo_timer_pending(&gsupc->connect_timer)) { |
| 55 | LOGP(DLGSUP, LOGL_DEBUG, |
| 56 | "GSUP connect: connect timer already running\n"); |
| 57 | osmo_timer_del(&gsupc->connect_timer); |
| 58 | } |
| 59 | |
| 60 | if (osmo_timer_pending(&gsupc->ping_timer)) { |
| 61 | LOGP(DLGSUP, LOGL_DEBUG, |
| 62 | "GSUP connect: ping timer already running\n"); |
| 63 | osmo_timer_del(&gsupc->ping_timer); |
| 64 | } |
| 65 | |
| 66 | if (ipa_client_conn_clear_queue(gsupc->link) > 0) |
| 67 | LOGP(DLGSUP, LOGL_DEBUG, "GSUP connect: discarded stored messages\n"); |
| 68 | |
| 69 | rc = ipa_client_conn_open(gsupc->link); |
| 70 | |
| 71 | if (rc >= 0) { |
| 72 | LOGP(DLGSUP, LOGL_NOTICE, "GSUP connecting to %s:%d\n", |
| 73 | gsupc->link->addr, gsupc->link->port); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | LOGP(DLGSUP, LOGL_ERROR, "GSUP failed to connect to %s:%d: %s\n", |
| 78 | gsupc->link->addr, gsupc->link->port, strerror(-rc)); |
| 79 | |
| 80 | if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT || |
| 81 | rc == -EINVAL) |
| 82 | return rc; |
| 83 | |
| 84 | osmo_timer_schedule(&gsupc->connect_timer, |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 85 | OSMO_GSUP_CLIENT_RECONNECT_INTERVAL, 0); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 86 | |
| 87 | LOGP(DLGSUP, LOGL_INFO, "Scheduled timer to retry GSUP connect to %s:%d\n", |
| 88 | gsupc->link->addr, gsupc->link->port); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static void connect_timer_cb(void *gsupc_) |
| 94 | { |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 95 | struct osmo_gsup_client *gsupc = gsupc_; |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 96 | |
| 97 | if (gsupc->is_connected) |
| 98 | return; |
| 99 | |
| 100 | gsup_client_connect(gsupc); |
| 101 | } |
| 102 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 103 | static void client_send(struct osmo_gsup_client *gsupc, int proto_ext, |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 104 | 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 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 112 | static void gsup_client_oap_register(struct osmo_gsup_client *gsupc) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 113 | { |
| 114 | struct msgb *msg_tx; |
| 115 | int rc; |
| 116 | rc = osmo_oap_client_register(&gsupc->oap_state, &msg_tx); |
| 117 | |
| 118 | if ((rc < 0) || (!msg_tx)) { |
| 119 | LOGP(DLGSUP, LOGL_ERROR, "GSUP OAP set up, but cannot register.\n"); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx); |
| 124 | } |
| 125 | |
| 126 | static void gsup_client_updown_cb(struct ipa_client_conn *link, int up) |
| 127 | { |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 128 | struct osmo_gsup_client *gsupc = link->data; |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 129 | |
| 130 | LOGP(DLGSUP, LOGL_INFO, "GSUP link to %s:%d %s\n", |
| 131 | link->addr, link->port, up ? "UP" : "DOWN"); |
| 132 | |
| 133 | gsupc->is_connected = up; |
| 134 | |
| 135 | if (up) { |
| 136 | start_test_procedure(gsupc); |
| 137 | |
| 138 | if (gsupc->oap_state.state == OSMO_OAP_INITIALIZED) |
| 139 | gsup_client_oap_register(gsupc); |
| 140 | |
| 141 | osmo_timer_del(&gsupc->connect_timer); |
| 142 | } else { |
| 143 | osmo_timer_del(&gsupc->ping_timer); |
| 144 | |
| 145 | osmo_timer_schedule(&gsupc->connect_timer, |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 146 | OSMO_GSUP_CLIENT_RECONNECT_INTERVAL, 0); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 150 | static int gsup_client_oap_handle(struct osmo_gsup_client *gsupc, struct msgb *msg_rx) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 151 | { |
| 152 | int rc; |
| 153 | struct msgb *msg_tx; |
| 154 | |
| 155 | /* If the oap_state is disabled, this will reject the messages. */ |
| 156 | rc = osmo_oap_client_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 | client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx); |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 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); |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 171 | struct osmo_gsup_client *gsupc = (struct osmo_gsup_client *)link->data; |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 172 | int rc; |
| 173 | struct ipaccess_unit ipa_dev = { |
| 174 | /* see gsup_client_create() on const vs non-const */ |
| 175 | .unit_name = (char*)gsupc->unit_name, |
| 176 | }; |
| 177 | |
| 178 | OSMO_ASSERT(ipa_dev.unit_name); |
| 179 | |
| 180 | msg->l2h = &hh->data[0]; |
| 181 | |
| 182 | rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg); |
| 183 | |
| 184 | if (rc < 0) { |
| 185 | LOGP(DLGSUP, LOGL_NOTICE, |
| 186 | "GSUP received an invalid IPA/CCM message from %s:%d\n", |
| 187 | link->addr, link->port); |
| 188 | /* Link has been closed */ |
| 189 | gsupc->is_connected = 0; |
| 190 | msgb_free(msg); |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | if (rc == 1) { |
| 195 | uint8_t msg_type = *(msg->l2h); |
| 196 | /* CCM message */ |
| 197 | if (msg_type == IPAC_MSGT_PONG) { |
| 198 | LOGP(DLGSUP, LOGL_DEBUG, "GSUP receiving PONG\n"); |
| 199 | gsupc->got_ipa_pong = 1; |
| 200 | } |
| 201 | |
| 202 | msgb_free(msg); |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | if (hh->proto != IPAC_PROTO_OSMO) |
| 207 | goto invalid; |
| 208 | |
| 209 | if (!he || msgb_l2len(msg) < sizeof(*he)) |
| 210 | goto invalid; |
| 211 | |
| 212 | msg->l2h = &he->data[0]; |
| 213 | |
| 214 | if (he->proto == IPAC_PROTO_EXT_GSUP) { |
| 215 | OSMO_ASSERT(gsupc->read_cb != NULL); |
| 216 | gsupc->read_cb(gsupc, msg); |
| 217 | /* expecting read_cb() to free msg */ |
| 218 | } else if (he->proto == IPAC_PROTO_EXT_OAP) { |
| 219 | return gsup_client_oap_handle(gsupc, msg); |
| 220 | /* gsup_client_oap_handle frees msg */ |
| 221 | } else |
| 222 | goto invalid; |
| 223 | |
| 224 | return 0; |
| 225 | |
| 226 | invalid: |
| 227 | LOGP(DLGSUP, LOGL_NOTICE, |
| 228 | "GSUP received an invalid IPA message from %s:%d, size = %d\n", |
| 229 | link->addr, link->port, msgb_length(msg)); |
| 230 | |
| 231 | msgb_free(msg); |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | static void ping_timer_cb(void *gsupc_) |
| 236 | { |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 237 | struct osmo_gsup_client *gsupc = gsupc_; |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 238 | |
| 239 | LOGP(DLGSUP, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n", |
| 240 | gsupc->is_connected ? "connected" : "not connected", |
| 241 | gsupc->got_ipa_pong ? "got" : "didn't get"); |
| 242 | |
| 243 | if (gsupc->got_ipa_pong) { |
| 244 | start_test_procedure(gsupc); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | LOGP(DLGSUP, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n"); |
| 249 | ipa_client_conn_close(gsupc->link); |
| 250 | gsupc->is_connected = 0; |
| 251 | |
| 252 | gsup_client_connect(gsupc); |
| 253 | } |
| 254 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 255 | static void start_test_procedure(struct osmo_gsup_client *gsupc) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 256 | { |
| 257 | osmo_timer_setup(&gsupc->ping_timer, ping_timer_cb, gsupc); |
| 258 | |
| 259 | gsupc->got_ipa_pong = 0; |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 260 | osmo_timer_schedule(&gsupc->ping_timer, OSMO_GSUP_CLIENT_PING_INTERVAL, 0); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 261 | LOGP(DLGSUP, LOGL_DEBUG, "GSUP sending PING\n"); |
| 262 | gsup_client_send_ping(gsupc); |
| 263 | } |
| 264 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 265 | struct osmo_gsup_client *osmo_gsup_client_create(void *talloc_ctx, |
| 266 | const char *unit_name, |
| 267 | const char *ip_addr, |
| 268 | unsigned int tcp_port, |
| 269 | osmo_gsup_client_read_cb_t read_cb, |
| 270 | struct osmo_oap_client_config *oapc_config) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 271 | { |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 272 | struct osmo_gsup_client *gsupc; |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 273 | int rc; |
| 274 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 275 | gsupc = talloc_zero(talloc_ctx, struct osmo_gsup_client); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 276 | OSMO_ASSERT(gsupc); |
| 277 | |
| 278 | /* struct ipaccess_unit has a non-const unit_name, so let's copy to be |
| 279 | * able to have a non-const unit_name here as well. To not taint the |
| 280 | * public gsup_client API, let's store it in a const char* anyway. */ |
| 281 | gsupc->unit_name = talloc_strdup(gsupc, unit_name); |
| 282 | OSMO_ASSERT(gsupc->unit_name); |
| 283 | |
| 284 | /* a NULL oapc_config will mark oap_state disabled. */ |
| 285 | rc = osmo_oap_client_init(oapc_config, &gsupc->oap_state); |
| 286 | if (rc != 0) |
| 287 | goto failed; |
| 288 | |
| 289 | gsupc->link = ipa_client_conn_create(gsupc, |
| 290 | /* no e1inp */ NULL, |
| 291 | 0, |
| 292 | ip_addr, tcp_port, |
| 293 | gsup_client_updown_cb, |
| 294 | gsup_client_read_cb, |
| 295 | /* default write_cb */ NULL, |
| 296 | gsupc); |
| 297 | if (!gsupc->link) |
| 298 | goto failed; |
| 299 | |
| 300 | osmo_timer_setup(&gsupc->connect_timer, connect_timer_cb, gsupc); |
| 301 | |
| 302 | rc = gsup_client_connect(gsupc); |
| 303 | |
| 304 | if (rc < 0) |
| 305 | goto failed; |
| 306 | |
| 307 | gsupc->read_cb = read_cb; |
| 308 | |
| 309 | return gsupc; |
| 310 | |
| 311 | failed: |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 312 | osmo_gsup_client_destroy(gsupc); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 313 | return NULL; |
| 314 | } |
| 315 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 316 | void osmo_gsup_client_destroy(struct osmo_gsup_client *gsupc) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 317 | { |
| 318 | osmo_timer_del(&gsupc->connect_timer); |
| 319 | osmo_timer_del(&gsupc->ping_timer); |
| 320 | |
| 321 | if (gsupc->link) { |
| 322 | ipa_client_conn_close(gsupc->link); |
| 323 | ipa_client_conn_destroy(gsupc->link); |
| 324 | gsupc->link = NULL; |
| 325 | } |
| 326 | talloc_free(gsupc); |
| 327 | } |
| 328 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 329 | int osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 330 | { |
| 331 | if (!gsupc || !gsupc->is_connected) { |
| 332 | LOGP(DLGSUP, LOGL_ERROR, "GSUP not connected, unable to send %s\n", msgb_hexdump(msg)); |
| 333 | msgb_free(msg); |
| 334 | return -ENOTCONN; |
| 335 | } |
| 336 | |
| 337 | client_send(gsupc, IPAC_PROTO_EXT_GSUP, msg); |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 342 | struct msgb *osmo_gsup_client_msgb_alloc(void) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 343 | { |
| 344 | return msgb_alloc_headroom(4000, 64, __func__); |
| 345 | } |