Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 1 | /* Generic Subscriber Update Protocol client */ |
| 2 | |
| 3 | /* (C) 2018 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <osmocom/rspro/rspro_client.h> |
| 22 | |
| 23 | #include <osmocom/abis/ipa.h> |
| 24 | #include <osmocom/gsm/protocol/ipaccess.h> |
| 25 | #include <osmocom/core/msgb.h> |
| 26 | #include <osmocom/core/logging.h> |
| 27 | |
| 28 | #include <errno.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | static void start_test_procedure(struct osmo_rspro_client *rsproc); |
| 32 | |
| 33 | static void rspro_client_send_ping(struct osmo_rspro_client *rsproc) |
| 34 | { |
| 35 | struct msgb *msg = osmo_rspro_client_msgb_alloc(); |
| 36 | |
| 37 | msg->l2h = msgb_put(msg, 1); |
| 38 | msg->l2h[0] = IPAC_MSGT_PING; |
| 39 | ipa_msg_push_header(msg, IPAC_PROTO_IPACCESS); |
| 40 | ipa_client_conn_send(rsproc->link, msg); |
| 41 | } |
| 42 | |
| 43 | static int rspro_client_connect(struct osmo_rspro_client *rsproc) |
| 44 | { |
| 45 | int rc; |
| 46 | |
| 47 | if (rsproc->is_connected) |
| 48 | return 0; |
| 49 | |
| 50 | if (osmo_timer_pending(&rsproc->connect_timer)) { |
| 51 | LOGP(DLRSPRO, LOGL_DEBUG, |
| 52 | "RSPRO connect: connect timer already running\n"); |
| 53 | osmo_timer_del(&rsproc->connect_timer); |
| 54 | } |
| 55 | |
| 56 | if (osmo_timer_pending(&rsproc->ping_timer)) { |
| 57 | LOGP(DLRSPRO, LOGL_DEBUG, |
| 58 | "RSPRO connect: ping timer already running\n"); |
| 59 | osmo_timer_del(&rsproc->ping_timer); |
| 60 | } |
| 61 | |
| 62 | if (ipa_client_conn_clear_queue(rsproc->link) > 0) |
| 63 | LOGP(DLRSPRO, LOGL_DEBUG, "RSPRO connect: discarded stored messages\n"); |
| 64 | |
| 65 | rc = ipa_client_conn_open(rsproc->link); |
| 66 | |
| 67 | if (rc >= 0) { |
| 68 | LOGP(DLRSPRO, LOGL_NOTICE, "RSPRO connecting to %s:%d\n", |
| 69 | rsproc->link->addr, rsproc->link->port); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | LOGP(DLRSPRO, LOGL_ERROR, "RSPRO failed to connect to %s:%d: %s\n", |
| 74 | rsproc->link->addr, rsproc->link->port, strerror(-rc)); |
| 75 | |
| 76 | if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT || |
| 77 | rc == -EINVAL) |
| 78 | return rc; |
| 79 | |
| 80 | osmo_timer_schedule(&rsproc->connect_timer, |
| 81 | OSMO_RSPRO_CLIENT_RECONNECT_INTERVAL, 0); |
| 82 | |
| 83 | LOGP(DLRSPRO, LOGL_INFO, "Scheduled timer to retry RSPRO connect to %s:%d\n", |
| 84 | rsproc->link->addr, rsproc->link->port); |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static void connect_timer_cb(void *rsproc_) |
| 90 | { |
| 91 | struct osmo_rspro_client *rsproc = rsproc_; |
| 92 | |
| 93 | if (rsproc->is_connected) |
| 94 | return; |
| 95 | |
| 96 | rspro_client_connect(rsproc); |
| 97 | } |
| 98 | |
| 99 | static void client_send(struct osmo_rspro_client *rsproc, int proto_ext, |
| 100 | struct msgb *msg_tx) |
| 101 | { |
| 102 | ipa_prepend_header_ext(msg_tx, proto_ext); |
| 103 | ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO); |
| 104 | ipa_client_conn_send(rsproc->link, msg_tx); |
| 105 | /* msg_tx is now queued and will be freed. */ |
| 106 | } |
| 107 | |
| 108 | static void rspro_client_updown_cb(struct ipa_client_conn *link, int up) |
| 109 | { |
| 110 | struct osmo_rspro_client *rsproc = link->data; |
| 111 | |
| 112 | LOGP(DLRSPRO, LOGL_INFO, "RSPRO link to %s:%d %s\n", |
| 113 | link->addr, link->port, up ? "UP" : "DOWN"); |
| 114 | |
| 115 | rsproc->is_connected = up; |
| 116 | |
| 117 | if (up) { |
| 118 | start_test_procedure(rsproc); |
| 119 | osmo_timer_del(&rsproc->connect_timer); |
| 120 | } else { |
| 121 | osmo_timer_del(&rsproc->ping_timer); |
| 122 | |
| 123 | osmo_timer_schedule(&rsproc->connect_timer, |
| 124 | OSMO_RSPRO_CLIENT_RECONNECT_INTERVAL, 0); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static int rspro_client_read_cb(struct ipa_client_conn *link, struct msgb *msg) |
| 129 | { |
| 130 | struct ipaccess_head *hh = (struct ipaccess_head *) msg->data; |
| 131 | struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg); |
| 132 | struct osmo_rspro_client *rsproc = (struct osmo_rspro_client *)link->data; |
| 133 | int rc; |
| 134 | struct ipaccess_unit ipa_dev = { |
| 135 | /* see rspro_client_create() on const vs non-const */ |
| 136 | .unit_name = (char*)rsproc->unit_name, |
| 137 | }; |
| 138 | |
| 139 | OSMO_ASSERT(ipa_dev.unit_name); |
| 140 | |
| 141 | msg->l2h = &hh->data[0]; |
| 142 | |
| 143 | rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg); |
| 144 | |
| 145 | if (rc < 0) { |
| 146 | LOGP(DLRSPRO, LOGL_NOTICE, |
| 147 | "RSPRO received an invalid IPA/CCM message from %s:%d\n", |
| 148 | link->addr, link->port); |
| 149 | /* Link has been closed */ |
| 150 | rsproc->is_connected = 0; |
| 151 | msgb_free(msg); |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | if (rc == 1) { |
| 156 | uint8_t msg_type = *(msg->l2h); |
| 157 | /* CCM message */ |
| 158 | if (msg_type == IPAC_MSGT_PONG) { |
| 159 | LOGP(DLRSPRO, LOGL_DEBUG, "RSPRO receiving PONG\n"); |
| 160 | rsproc->got_ipa_pong = 1; |
| 161 | } |
| 162 | |
| 163 | msgb_free(msg); |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | if (hh->proto != IPAC_PROTO_OSMO) |
| 168 | goto invalid; |
| 169 | |
| 170 | if (!he || msgb_l2len(msg) < sizeof(*he)) |
| 171 | goto invalid; |
| 172 | |
| 173 | msg->l2h = &he->data[0]; |
| 174 | |
| 175 | if (he->proto == IPAC_PROTO_EXT_RSPRO) { |
| 176 | OSMO_ASSERT(rsproc->read_cb != NULL); |
| 177 | rsproc->read_cb(rsproc, msg); |
| 178 | /* expecting read_cb() to free msg */ |
| 179 | } else |
| 180 | goto invalid; |
| 181 | |
| 182 | return 0; |
| 183 | |
| 184 | invalid: |
| 185 | LOGP(DLRSPRO, LOGL_NOTICE, |
| 186 | "RSPRO received an invalid IPA message from %s:%d, size = %d\n", |
| 187 | link->addr, link->port, msgb_length(msg)); |
| 188 | |
| 189 | msgb_free(msg); |
| 190 | return -1; |
| 191 | } |
| 192 | |
| 193 | static void ping_timer_cb(void *rsproc_) |
| 194 | { |
| 195 | struct osmo_rspro_client *rsproc = rsproc_; |
| 196 | |
| 197 | LOGP(DLRSPRO, LOGL_INFO, "RSPRO ping callback (%s, %s PONG)\n", |
| 198 | rsproc->is_connected ? "connected" : "not connected", |
| 199 | rsproc->got_ipa_pong ? "got" : "didn't get"); |
| 200 | |
| 201 | if (rsproc->got_ipa_pong) { |
| 202 | start_test_procedure(rsproc); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | LOGP(DLRSPRO, LOGL_NOTICE, "RSPRO ping timed out, reconnecting\n"); |
| 207 | ipa_client_conn_close(rsproc->link); |
| 208 | rsproc->is_connected = 0; |
| 209 | |
| 210 | rspro_client_connect(rsproc); |
| 211 | } |
| 212 | |
| 213 | static void start_test_procedure(struct osmo_rspro_client *rsproc) |
| 214 | { |
| 215 | osmo_timer_setup(&rsproc->ping_timer, ping_timer_cb, rsproc); |
| 216 | |
| 217 | rsproc->got_ipa_pong = 0; |
| 218 | osmo_timer_schedule(&rsproc->ping_timer, OSMO_RSPRO_CLIENT_PING_INTERVAL, 0); |
| 219 | LOGP(DLRSPRO, LOGL_DEBUG, "RSPRO sending PING\n"); |
| 220 | rspro_client_send_ping(rsproc); |
| 221 | } |
| 222 | |
| 223 | struct osmo_rspro_client *osmo_rspro_client_create(void *talloc_ctx, |
| 224 | const char *unit_name, |
| 225 | const char *ip_addr, |
| 226 | unsigned int tcp_port, |
| 227 | osmo_rspro_client_read_cb_t read_cb) |
| 228 | { |
| 229 | struct osmo_rspro_client *rsproc; |
| 230 | int rc; |
| 231 | |
| 232 | rsproc = talloc_zero(talloc_ctx, struct osmo_rspro_client); |
| 233 | OSMO_ASSERT(rsproc); |
| 234 | |
| 235 | /* struct ipaccess_unit has a non-const unit_name, so let's copy to be |
| 236 | * able to have a non-const unit_name here as well. To not taint the |
| 237 | * public rspro_client API, let's store it in a const char* anyway. */ |
| 238 | rsproc->unit_name = talloc_strdup(rsproc, unit_name); |
| 239 | OSMO_ASSERT(rsproc->unit_name); |
| 240 | |
| 241 | rsproc->link = ipa_client_conn_create(rsproc, |
| 242 | /* no e1inp */ NULL, |
| 243 | 0, |
| 244 | ip_addr, tcp_port, |
| 245 | rspro_client_updown_cb, |
| 246 | rspro_client_read_cb, |
| 247 | /* default write_cb */ NULL, |
| 248 | rsproc); |
| 249 | if (!rsproc->link) |
| 250 | goto failed; |
| 251 | |
| 252 | osmo_timer_setup(&rsproc->connect_timer, connect_timer_cb, rsproc); |
| 253 | |
| 254 | rc = rspro_client_connect(rsproc); |
| 255 | if (rc < 0) |
| 256 | goto failed; |
| 257 | |
| 258 | rsproc->read_cb = read_cb; |
| 259 | |
| 260 | return rsproc; |
| 261 | |
| 262 | failed: |
| 263 | osmo_rspro_client_destroy(rsproc); |
| 264 | return NULL; |
| 265 | } |
| 266 | |
| 267 | void osmo_rspro_client_destroy(struct osmo_rspro_client *rsproc) |
| 268 | { |
| 269 | osmo_timer_del(&rsproc->connect_timer); |
| 270 | osmo_timer_del(&rsproc->ping_timer); |
| 271 | |
| 272 | if (rsproc->link) { |
| 273 | ipa_client_conn_close(rsproc->link); |
| 274 | ipa_client_conn_destroy(rsproc->link); |
| 275 | rsproc->link = NULL; |
| 276 | } |
| 277 | talloc_free(rsproc); |
| 278 | } |
| 279 | |
| 280 | int osmo_rspro_client_send(struct osmo_rspro_client *rsproc, struct msgb *msg) |
| 281 | { |
| 282 | if (!rsproc || !rsproc->is_connected) { |
| 283 | LOGP(DLRSPRO, LOGL_ERROR, "RSPRO not connected, unable to send %s\n", msgb_hexdump(msg)); |
| 284 | msgb_free(msg); |
| 285 | return -ENOTCONN; |
| 286 | } |
| 287 | |
| 288 | client_send(rsproc, IPAC_PROTO_EXT_RSPRO, msg); |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | struct msgb *osmo_rspro_client_msgb_alloc(void) |
| 294 | { |
| 295 | return msgb_alloc_headroom(4000, 64, __func__); |
| 296 | } |