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 | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 36 | static int gsup_client_connect(struct gprs_gsup_client *gsupc) |
| 37 | { |
| 38 | int rc; |
| 39 | |
| 40 | if (gsupc->is_connected) |
| 41 | return 0; |
| 42 | |
| 43 | if (osmo_timer_pending(&gsupc->connect_timer)) { |
| 44 | LOGP(DLINP, LOGL_DEBUG, |
| 45 | "GSUP connect: connect timer already running\n"); |
| 46 | osmo_timer_del(&gsupc->connect_timer); |
| 47 | } |
| 48 | |
Jacob Erlbeck | 4188c30 | 2014-12-19 18:50:05 +0100 | [diff] [blame] | 49 | if (ipa_client_conn_clear_queue(gsupc->link) > 0) |
| 50 | LOGP(DLINP, LOGL_DEBUG, "GSUP connect: discarded stored messages\n"); |
| 51 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 52 | rc = ipa_client_conn_open(gsupc->link); |
| 53 | |
Jacob Erlbeck | 69e16b9 | 2014-12-19 19:00:56 +0100 | [diff] [blame^] | 54 | if (rc >= 0) { |
| 55 | LOGP(DGPRS, LOGL_INFO, "GSUP connecting to %s:%d\n", |
| 56 | gsupc->link->addr, gsupc->link->port); |
| 57 | return 0; |
| 58 | } |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 59 | |
| 60 | LOGP(DGPRS, LOGL_INFO, "GSUP failed to connect to %s:%d: %s\n", |
| 61 | gsupc->link->addr, gsupc->link->port, strerror(-rc)); |
| 62 | |
| 63 | if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT || |
| 64 | rc == -EINVAL) |
| 65 | return rc; |
| 66 | |
| 67 | osmo_timer_schedule(&gsupc->connect_timer, GPRS_GSUP_RECONNECT_INTERVAL, 0); |
| 68 | |
Jacob Erlbeck | 69e16b9 | 2014-12-19 19:00:56 +0100 | [diff] [blame^] | 69 | return rc; |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static void connect_timer_cb(void *gsupc_) |
| 73 | { |
| 74 | struct gprs_gsup_client *gsupc = gsupc_; |
| 75 | |
| 76 | if (gsupc->is_connected) |
| 77 | return; |
| 78 | |
| 79 | gsup_client_connect(gsupc); |
| 80 | } |
| 81 | |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 82 | static void gsup_client_updown_cb(struct ipa_client_conn *link, int up) |
| 83 | { |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 84 | struct gprs_gsup_client *gsupc = link->data; |
| 85 | |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 86 | LOGP(DGPRS, LOGL_NOTICE, "GSUP link to %s:%d %s\n", |
| 87 | link->addr, link->port, up ? "UP" : "DOWN"); |
| 88 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 89 | gsupc->is_connected = up; |
| 90 | |
| 91 | if (up) { |
| 92 | /* TODO: Start ping procedure */ |
| 93 | |
| 94 | osmo_timer_del(&gsupc->connect_timer); |
| 95 | } else { |
| 96 | /* TODO: Stop ping procedure */ |
| 97 | |
| 98 | osmo_timer_schedule(&gsupc->connect_timer, |
| 99 | GPRS_GSUP_RECONNECT_INTERVAL, 0); |
| 100 | } |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg) |
| 104 | { |
| 105 | struct ipaccess_head *hh = (struct ipaccess_head *) msg->data; |
| 106 | struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg); |
| 107 | struct gprs_gsup_client *gsupc = (struct gprs_gsup_client *)link->data; |
| 108 | |
| 109 | if (hh->proto != IPAC_PROTO_OSMO) |
| 110 | goto invalid; |
| 111 | |
| 112 | if (!he || msgb_l2len(msg) < sizeof(*he) || |
| 113 | he->proto != IPAC_PROTO_EXT_GSUP) |
| 114 | goto invalid; |
| 115 | |
| 116 | msg->l2h = &he->data[0]; |
| 117 | |
| 118 | OSMO_ASSERT(gsupc->read_cb != NULL); |
| 119 | gsupc->read_cb(gsupc, msg); |
| 120 | |
| 121 | /* Not freeing msg here, because that must be done by the read_cb. */ |
| 122 | return 0; |
| 123 | |
| 124 | invalid: |
| 125 | LOGP(DGPRS, LOGL_NOTICE, |
| 126 | "GSUP received an invalid IPA message from %s:%d, size = %d\n", |
| 127 | link->addr, link->port, msgb_length(msg)); |
| 128 | |
| 129 | msgb_free(msg); |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | struct gprs_gsup_client *gprs_gsup_client_create(const char *ip_addr, |
| 134 | unsigned int tcp_port, |
| 135 | gprs_gsup_read_cb_t read_cb) |
| 136 | { |
| 137 | struct gprs_gsup_client *gsupc; |
| 138 | int rc; |
| 139 | |
| 140 | gsupc = talloc_zero(tall_bsc_ctx, struct gprs_gsup_client); |
| 141 | OSMO_ASSERT(gsupc); |
| 142 | |
| 143 | gsupc->link = ipa_client_conn_create(gsupc, |
| 144 | /* no e1inp */ NULL, |
| 145 | 0, |
| 146 | ip_addr, tcp_port, |
| 147 | gsup_client_updown_cb, |
| 148 | gsup_client_read_cb, |
| 149 | /* default write_cb */ NULL, |
| 150 | gsupc); |
| 151 | if (!gsupc->link) |
| 152 | goto failed; |
| 153 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 154 | gsupc->connect_timer.data = gsupc; |
| 155 | gsupc->connect_timer.cb = &connect_timer_cb; |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 156 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 157 | rc = gsup_client_connect(gsupc); |
| 158 | |
| 159 | if (rc < 0 && rc != -EINPROGRESS) |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 160 | goto failed; |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 161 | |
| 162 | gsupc->read_cb = read_cb; |
| 163 | |
| 164 | return gsupc; |
| 165 | |
| 166 | failed: |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 167 | gprs_gsup_client_destroy(gsupc); |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | void gprs_gsup_client_destroy(struct gprs_gsup_client *gsupc) |
| 172 | { |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 173 | osmo_timer_del(&gsupc->connect_timer); |
| 174 | |
| 175 | if (gsupc->link) { |
| 176 | ipa_client_conn_close(gsupc->link); |
| 177 | ipa_client_conn_destroy(gsupc->link); |
| 178 | gsupc->link = NULL; |
| 179 | } |
| 180 | talloc_free(gsupc); |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | int gprs_gsup_client_send(struct gprs_gsup_client *gsupc, struct msgb *msg) |
| 184 | { |
| 185 | if (!gsupc) { |
| 186 | msgb_free(msg); |
| 187 | return -ENOTCONN; |
| 188 | } |
| 189 | |
Jacob Erlbeck | 4188c30 | 2014-12-19 18:50:05 +0100 | [diff] [blame] | 190 | if (!gsupc->is_connected) { |
| 191 | msgb_free(msg); |
| 192 | return -EAGAIN; |
| 193 | } |
| 194 | |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 195 | ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_GSUP); |
| 196 | ipa_msg_push_header(msg, IPAC_PROTO_OSMO); |
| 197 | ipa_client_conn_send(gsupc->link, msg); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | struct msgb *gprs_gsup_msgb_alloc(void) |
| 203 | { |
| 204 | return msgb_alloc_headroom(4000, 64, __func__); |
| 205 | } |