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 | |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 103 | static void gsup_client_updown_cb(struct ipa_client_conn *link, int up) |
| 104 | { |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 105 | struct gprs_gsup_client *gsupc = link->data; |
| 106 | |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 107 | LOGP(DGPRS, LOGL_INFO, "GSUP link to %s:%d %s\n", |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 108 | link->addr, link->port, up ? "UP" : "DOWN"); |
| 109 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 110 | gsupc->is_connected = up; |
| 111 | |
| 112 | if (up) { |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 113 | start_test_procedure(gsupc); |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 114 | |
| 115 | osmo_timer_del(&gsupc->connect_timer); |
| 116 | } else { |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 117 | osmo_timer_del(&gsupc->ping_timer); |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 118 | |
| 119 | osmo_timer_schedule(&gsupc->connect_timer, |
| 120 | GPRS_GSUP_RECONNECT_INTERVAL, 0); |
| 121 | } |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg) |
| 125 | { |
| 126 | struct ipaccess_head *hh = (struct ipaccess_head *) msg->data; |
| 127 | struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg); |
| 128 | struct gprs_gsup_client *gsupc = (struct gprs_gsup_client *)link->data; |
Jacob Erlbeck | e154d8b | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 129 | int rc; |
| 130 | static struct ipaccess_unit ipa_dev = { |
| 131 | .unit_name = "SGSN" |
| 132 | }; |
| 133 | |
| 134 | msg->l2h = &hh->data[0]; |
| 135 | |
| 136 | rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg); |
| 137 | |
| 138 | if (rc < 0) { |
| 139 | LOGP(DGPRS, LOGL_NOTICE, |
| 140 | "GSUP received an invalid IPA/CCM message from %s:%d\n", |
| 141 | link->addr, link->port); |
| 142 | /* Link has been closed */ |
| 143 | gsupc->is_connected = 0; |
| 144 | msgb_free(msg); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | if (rc == 1) { |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 149 | uint8_t msg_type = *(msg->l2h); |
Jacob Erlbeck | e154d8b | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 150 | /* CCM message */ |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 151 | if (msg_type == IPAC_MSGT_PONG) { |
| 152 | LOGP(DGPRS, LOGL_DEBUG, "GSUP receiving PONG\n"); |
| 153 | gsupc->got_ipa_pong = 1; |
| 154 | } |
Jacob Erlbeck | e154d8b | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 155 | |
| 156 | msgb_free(msg); |
| 157 | return 0; |
| 158 | } |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 159 | |
| 160 | if (hh->proto != IPAC_PROTO_OSMO) |
| 161 | goto invalid; |
| 162 | |
| 163 | if (!he || msgb_l2len(msg) < sizeof(*he) || |
| 164 | he->proto != IPAC_PROTO_EXT_GSUP) |
| 165 | goto invalid; |
| 166 | |
| 167 | msg->l2h = &he->data[0]; |
| 168 | |
| 169 | OSMO_ASSERT(gsupc->read_cb != NULL); |
| 170 | gsupc->read_cb(gsupc, msg); |
| 171 | |
| 172 | /* Not freeing msg here, because that must be done by the read_cb. */ |
| 173 | return 0; |
| 174 | |
| 175 | invalid: |
| 176 | LOGP(DGPRS, LOGL_NOTICE, |
| 177 | "GSUP received an invalid IPA message from %s:%d, size = %d\n", |
| 178 | link->addr, link->port, msgb_length(msg)); |
| 179 | |
| 180 | msgb_free(msg); |
| 181 | return -1; |
| 182 | } |
| 183 | |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 184 | static void ping_timer_cb(void *gsupc_) |
| 185 | { |
| 186 | struct gprs_gsup_client *gsupc = gsupc_; |
| 187 | |
| 188 | LOGP(DGPRS, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n", |
| 189 | gsupc->is_connected ? "connected" : "not connected", |
| 190 | gsupc->got_ipa_pong ? "got" : "didn't get"); |
| 191 | |
| 192 | if (gsupc->got_ipa_pong) { |
| 193 | start_test_procedure(gsupc); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | LOGP(DGPRS, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n"); |
| 198 | ipa_client_conn_close(gsupc->link); |
| 199 | gsupc->is_connected = 0; |
| 200 | |
| 201 | gsup_client_connect(gsupc); |
| 202 | } |
| 203 | |
| 204 | static void start_test_procedure(struct gprs_gsup_client *gsupc) |
| 205 | { |
| 206 | gsupc->ping_timer.data = gsupc; |
| 207 | gsupc->ping_timer.cb = &ping_timer_cb; |
| 208 | |
| 209 | gsupc->got_ipa_pong = 0; |
| 210 | osmo_timer_schedule(&gsupc->ping_timer, GPRS_GSUP_PING_INTERVAL, 0); |
| 211 | LOGP(DGPRS, LOGL_DEBUG, "GSUP sending PING\n"); |
| 212 | gsup_client_send_ping(gsupc); |
| 213 | } |
| 214 | |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 215 | struct gprs_gsup_client *gprs_gsup_client_create(const char *ip_addr, |
| 216 | unsigned int tcp_port, |
| 217 | gprs_gsup_read_cb_t read_cb) |
| 218 | { |
| 219 | struct gprs_gsup_client *gsupc; |
| 220 | int rc; |
| 221 | |
| 222 | gsupc = talloc_zero(tall_bsc_ctx, struct gprs_gsup_client); |
| 223 | OSMO_ASSERT(gsupc); |
| 224 | |
| 225 | gsupc->link = ipa_client_conn_create(gsupc, |
| 226 | /* no e1inp */ NULL, |
| 227 | 0, |
| 228 | ip_addr, tcp_port, |
| 229 | gsup_client_updown_cb, |
| 230 | gsup_client_read_cb, |
| 231 | /* default write_cb */ NULL, |
| 232 | gsupc); |
| 233 | if (!gsupc->link) |
| 234 | goto failed; |
| 235 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 236 | gsupc->connect_timer.data = gsupc; |
| 237 | gsupc->connect_timer.cb = &connect_timer_cb; |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 238 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 239 | rc = gsup_client_connect(gsupc); |
| 240 | |
Jacob Erlbeck | 3ee67ff | 2015-01-26 09:22:39 +0100 | [diff] [blame] | 241 | if (rc < 0) |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 242 | goto failed; |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 243 | |
| 244 | gsupc->read_cb = read_cb; |
| 245 | |
| 246 | return gsupc; |
| 247 | |
| 248 | failed: |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 249 | gprs_gsup_client_destroy(gsupc); |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 250 | return NULL; |
| 251 | } |
| 252 | |
| 253 | void gprs_gsup_client_destroy(struct gprs_gsup_client *gsupc) |
| 254 | { |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 255 | osmo_timer_del(&gsupc->connect_timer); |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 256 | osmo_timer_del(&gsupc->ping_timer); |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 257 | |
| 258 | if (gsupc->link) { |
| 259 | ipa_client_conn_close(gsupc->link); |
| 260 | ipa_client_conn_destroy(gsupc->link); |
| 261 | gsupc->link = NULL; |
| 262 | } |
| 263 | talloc_free(gsupc); |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | int gprs_gsup_client_send(struct gprs_gsup_client *gsupc, struct msgb *msg) |
| 267 | { |
| 268 | if (!gsupc) { |
| 269 | msgb_free(msg); |
| 270 | return -ENOTCONN; |
| 271 | } |
| 272 | |
Jacob Erlbeck | 4188c30 | 2014-12-19 18:50:05 +0100 | [diff] [blame] | 273 | if (!gsupc->is_connected) { |
| 274 | msgb_free(msg); |
| 275 | return -EAGAIN; |
| 276 | } |
| 277 | |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 278 | ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_GSUP); |
| 279 | ipa_msg_push_header(msg, IPAC_PROTO_OSMO); |
| 280 | ipa_client_conn_send(gsupc->link, msg); |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | struct msgb *gprs_gsup_msgb_alloc(void) |
| 286 | { |
| 287 | return msgb_alloc_headroom(4000, 64, __func__); |
| 288 | } |