Harald Welte | 936f672 | 2016-05-03 18:51:18 +0200 | [diff] [blame] | 1 | /* (C) 2016 by Harald Welte <laforge@gnumonks.org> |
| 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU Affero General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU Affero General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Affero General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | */ |
| 19 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | |
| 22 | #include <osmocom/core/msgb.h> |
| 23 | #include <osmocom/core/logging.h> |
| 24 | #include <osmocom/core/linuxlist.h> |
| 25 | #include <osmocom/abis/ipa.h> |
| 26 | #include <osmocom/abis/ipaccess.h> |
| 27 | |
| 28 | #include "gsup_server.h" |
Neels Hofmeyr | 6eed322 | 2016-12-11 01:21:49 +0100 | [diff] [blame] | 29 | #include "gsup_router.h" |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 30 | |
| 31 | static void osmo_gsup_server_send(struct osmo_gsup_conn *conn, |
| 32 | int proto_ext, struct msgb *msg_tx) |
| 33 | { |
| 34 | ipa_prepend_header_ext(msg_tx, proto_ext); |
| 35 | ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO); |
| 36 | ipa_server_conn_send(conn->conn, msg_tx); |
| 37 | } |
| 38 | |
| 39 | int osmo_gsup_conn_send(struct osmo_gsup_conn *conn, struct msgb *msg) |
| 40 | { |
| 41 | if (!conn) { |
| 42 | msgb_free(msg); |
| 43 | return -ENOTCONN; |
| 44 | } |
| 45 | |
| 46 | osmo_gsup_server_send(conn, IPAC_PROTO_EXT_GSUP, msg); |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int osmo_gsup_conn_oap_handle(struct osmo_gsup_conn *conn, |
| 52 | struct msgb *msg_rx) |
| 53 | { |
Neels Hofmeyr | ec1b959 | 2016-12-11 01:22:23 +0100 | [diff] [blame] | 54 | #if 0 |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 55 | int rc; |
| 56 | struct msgb *msg_tx; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 57 | rc = oap_handle(&conn->oap_state, msg_rx, &msg_tx); |
| 58 | msgb_free(msg_rx); |
| 59 | if (rc < 0) |
| 60 | return rc; |
| 61 | |
| 62 | if (msg_tx) |
| 63 | osmo_gsup_conn_send(conn, IPAC_PROTO_EXT_OAP, msg_tx); |
| 64 | #endif |
| 65 | return 0; |
| 66 | } |
| 67 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 68 | /* Data from a given client has arrived over the socket */ |
| 69 | static int osmo_gsup_server_read_cb(struct ipa_server_conn *conn, |
| 70 | struct msgb *msg) |
| 71 | { |
| 72 | struct ipaccess_head *hh = (struct ipaccess_head *) msg->data; |
| 73 | struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg); |
| 74 | struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data; |
| 75 | int rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 76 | |
| 77 | msg->l2h = &hh->data[0]; |
| 78 | |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 79 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 80 | rc = ipa_server_conn_ccm(conn, msg); |
| 81 | if (rc < 0) { |
| 82 | /* conn is already invalid here! */ |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 83 | return -1; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 84 | } |
Harald Welte | 5341b5d | 2016-04-28 12:48:39 +0200 | [diff] [blame] | 85 | msgb_free(msg); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 86 | return 0; |
| 87 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 88 | |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 89 | if (hh->proto != IPAC_PROTO_OSMO) { |
| 90 | LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA stream ID 0x%02x\n", |
| 91 | hh->proto); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 92 | goto invalid; |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 93 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 94 | |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 95 | if (!he || msgb_l2len(msg) < sizeof(*he)) { |
| 96 | LOGP(DLGSUP, LOGL_NOTICE, "short IPA message\n"); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 97 | goto invalid; |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 98 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 99 | |
| 100 | msg->l2h = &he->data[0]; |
| 101 | |
| 102 | if (he->proto == IPAC_PROTO_EXT_GSUP) { |
| 103 | OSMO_ASSERT(clnt->server->read_cb != NULL); |
| 104 | clnt->server->read_cb(clnt, msg); |
| 105 | /* expecting read_cb() to free msg */ |
| 106 | } else if (he->proto == IPAC_PROTO_EXT_OAP) { |
| 107 | return osmo_gsup_conn_oap_handle(clnt, msg); |
| 108 | /* osmo_gsup_client_oap_handle frees msg */ |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 109 | } else { |
| 110 | LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA Osmo Proto 0x%02x\n", |
| 111 | hh->proto); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 112 | goto invalid; |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 113 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 114 | |
| 115 | return 0; |
| 116 | |
| 117 | invalid: |
| 118 | LOGP(DLGSUP, LOGL_NOTICE, |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 119 | "GSUP received an invalid IPA message from %s:%d: %s\n", |
| 120 | conn->addr, conn->port, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg))); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 121 | msgb_free(msg); |
| 122 | return -1; |
| 123 | |
| 124 | } |
| 125 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 126 | static void osmo_tlvp_dump(const struct tlv_parsed *tlvp, |
| 127 | int subsys, int level) |
| 128 | { |
| 129 | unsigned int i; |
| 130 | |
| 131 | for (i = 0; i < ARRAY_SIZE(tlvp->lv); i++) { |
| 132 | if (!TLVP_PRESENT(tlvp, i)) |
| 133 | continue; |
| 134 | |
| 135 | LOGP(subsys, level, "%u: %s\n", i, |
| 136 | TLVP_VAL(tlvp, i)); |
| 137 | LOGP(subsys, level, "%u: %s\n", i, |
| 138 | osmo_hexdump(TLVP_VAL(tlvp, i), |
| 139 | TLVP_LEN(tlvp, i))); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* FIXME: should this be parrt of ipas_server handling, not GSUP? */ |
| 144 | static void tlvp_copy(void *ctx, struct tlv_parsed *out, const struct tlv_parsed *in) |
| 145 | { |
| 146 | unsigned int i; |
| 147 | |
| 148 | for (i = 0; i < ARRAY_SIZE(out->lv); i++) { |
| 149 | if (!TLVP_PRESENT(in, i)) { |
| 150 | if (TLVP_PRESENT(out, i)) { |
| 151 | talloc_free((void *) out->lv[i].val); |
| 152 | out->lv[i].val = NULL; |
| 153 | out->lv[i].len = 0; |
| 154 | } |
| 155 | continue; |
| 156 | } |
| 157 | out->lv[i].val = talloc_memdup(ctx, in->lv[i].val, in->lv[i].len); |
| 158 | out->lv[i].len = in->lv[i].len; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | int osmo_gsup_conn_ccm_get(const struct osmo_gsup_conn *clnt, uint8_t **addr, |
| 163 | uint8_t tag) |
| 164 | { |
| 165 | if (!TLVP_PRESENT(&clnt->ccm, tag)) |
| 166 | return -ENODEV; |
| 167 | *addr = (uint8_t *) TLVP_VAL(&clnt->ccm, tag); |
| 168 | |
| 169 | return TLVP_LEN(&clnt->ccm, tag); |
| 170 | } |
| 171 | |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 172 | static int osmo_gsup_server_ccm_cb(struct ipa_server_conn *conn, |
| 173 | struct msgb *msg, struct tlv_parsed *tlvp, |
| 174 | struct ipaccess_unit *unit) |
| 175 | { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 176 | struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data; |
Neels Hofmeyr | ec1b959 | 2016-12-11 01:22:23 +0100 | [diff] [blame] | 177 | uint8_t *addr = NULL; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 178 | size_t addr_len; |
| 179 | |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 180 | LOGP(DLGSUP, LOGL_INFO, "CCM Callback\n"); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 181 | |
| 182 | /* FIXME: should this be parrt of ipas_server handling, not |
| 183 | * GSUP? */ |
| 184 | tlvp_copy(clnt, &clnt->ccm, tlvp); |
| 185 | osmo_tlvp_dump(tlvp, DLGSUP, LOGL_INFO); |
| 186 | |
| 187 | addr_len = osmo_gsup_conn_ccm_get(clnt, &addr, IPAC_IDTAG_SERNR); |
Neels Hofmeyr | 5ecdc56 | 2017-02-24 06:38:04 +0100 | [diff] [blame] | 188 | if (addr_len <= 0) { |
| 189 | LOGP(DLGSUP, LOGL_ERROR, "GSUP client %s:%u has no %s IE and" |
| 190 | " cannot be routed\n", |
| 191 | conn->addr, conn->port, |
| 192 | ipa_ccm_idtag_name(IPAC_IDTAG_SERNR)); |
| 193 | return -EINVAL; |
| 194 | } |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 195 | |
Neels Hofmeyr | 5ecdc56 | 2017-02-24 06:38:04 +0100 | [diff] [blame] | 196 | gsup_route_add(clnt, addr, addr_len); |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 200 | static int osmo_gsup_server_closed_cb(struct ipa_server_conn *conn) |
| 201 | { |
| 202 | struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data; |
| 203 | |
| 204 | LOGP(DLGSUP, LOGL_INFO, "Lost GSUP client %s:%d\n", |
| 205 | conn->addr, conn->port); |
| 206 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 207 | gsup_route_del_conn(clnt); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 208 | llist_del(&clnt->list); |
Harald Welte | a7617e9 | 2016-04-28 12:57:10 +0200 | [diff] [blame] | 209 | talloc_free(clnt); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 214 | /* Add conn to the clients list in a way that conn->auc_3g_ind takes the lowest |
| 215 | * unused integer and the list of clients remains sorted by auc_3g_ind. |
| 216 | * Keep this function non-static to allow linking in a unit test. */ |
| 217 | void osmo_gsup_server_add_conn(struct llist_head *clients, |
| 218 | struct osmo_gsup_conn *conn) |
| 219 | { |
| 220 | struct osmo_gsup_conn *c; |
| 221 | struct osmo_gsup_conn *prev_conn; |
| 222 | |
| 223 | c = llist_first_entry_or_null(clients, struct osmo_gsup_conn, list); |
| 224 | |
| 225 | /* Is the first index, 0, unused? */ |
| 226 | if (!c || c->auc_3g_ind > 0) { |
| 227 | conn->auc_3g_ind = 0; |
| 228 | llist_add(&conn->list, clients); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | /* Look for a gap later on */ |
| 233 | prev_conn = NULL; |
| 234 | llist_for_each_entry(c, clients, list) { |
| 235 | /* skip first item, we know it has auc_3g_ind == 0. */ |
| 236 | if (!prev_conn) { |
| 237 | prev_conn = c; |
| 238 | continue; |
| 239 | } |
| 240 | if (c->auc_3g_ind > prev_conn->auc_3g_ind + 1) |
| 241 | break; |
| 242 | prev_conn = c; |
| 243 | } |
| 244 | |
| 245 | OSMO_ASSERT(prev_conn); |
| 246 | |
| 247 | conn->auc_3g_ind = prev_conn->auc_3g_ind + 1; |
| 248 | llist_add(&conn->list, &prev_conn->list); |
| 249 | } |
| 250 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 251 | /* a client has connected to the server socket and we have accept()ed it */ |
| 252 | static int osmo_gsup_server_accept_cb(struct ipa_server_link *link, int fd) |
| 253 | { |
| 254 | struct osmo_gsup_conn *conn; |
| 255 | struct osmo_gsup_server *gsups = |
| 256 | (struct osmo_gsup_server *) link->data; |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 257 | int rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 258 | |
Harald Welte | a7617e9 | 2016-04-28 12:57:10 +0200 | [diff] [blame] | 259 | conn = talloc_zero(gsups, struct osmo_gsup_conn); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 260 | OSMO_ASSERT(conn); |
| 261 | |
Harald Welte | a7617e9 | 2016-04-28 12:57:10 +0200 | [diff] [blame] | 262 | conn->conn = ipa_server_conn_create(gsups, link, fd, |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 263 | osmo_gsup_server_read_cb, |
| 264 | osmo_gsup_server_closed_cb, conn); |
| 265 | OSMO_ASSERT(conn->conn); |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 266 | conn->conn->ccm_cb = osmo_gsup_server_ccm_cb; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 267 | |
| 268 | /* link data structure with server structure */ |
| 269 | conn->server = gsups; |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 270 | osmo_gsup_server_add_conn(&gsups->clients, conn); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 271 | |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 272 | LOGP(DLGSUP, LOGL_INFO, "New GSUP client %s:%d (IND=%u)\n", |
| 273 | conn->conn->addr, conn->conn->port, conn->auc_3g_ind); |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 274 | |
| 275 | /* request the identity of the client */ |
| 276 | rc = ipa_ccm_send_id_req(fd); |
| 277 | if (rc < 0) |
| 278 | goto failed; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 279 | #if 0 |
| 280 | rc = oap_init(&gsups->oap_config, &conn->oap_state); |
| 281 | if (rc != 0) |
| 282 | goto failed; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 283 | #endif |
| 284 | return 0; |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 285 | failed: |
| 286 | ipa_server_conn_destroy(conn->conn); |
| 287 | return -1; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | struct osmo_gsup_server * |
Max | 9cacb6f | 2017-02-20 17:22:56 +0100 | [diff] [blame] | 291 | osmo_gsup_server_create(void *ctx, const char *ip_addr, uint16_t tcp_port, |
| 292 | osmo_gsup_read_cb_t read_cb, |
| 293 | struct llist_head *lu_op_lst) |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 294 | { |
| 295 | struct osmo_gsup_server *gsups; |
| 296 | int rc; |
| 297 | |
| 298 | gsups = talloc_zero(ctx, struct osmo_gsup_server); |
| 299 | OSMO_ASSERT(gsups); |
| 300 | |
| 301 | INIT_LLIST_HEAD(&gsups->clients); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 302 | INIT_LLIST_HEAD(&gsups->routes); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 303 | |
| 304 | gsups->link = ipa_server_link_create(gsups, |
| 305 | /* no e1inp */ NULL, |
| 306 | ip_addr, tcp_port, |
| 307 | osmo_gsup_server_accept_cb, |
| 308 | gsups); |
| 309 | if (!gsups->link) |
| 310 | goto failed; |
| 311 | |
| 312 | gsups->read_cb = read_cb; |
| 313 | |
| 314 | rc = ipa_server_link_open(gsups->link); |
| 315 | if (rc < 0) |
| 316 | goto failed; |
| 317 | |
Max | 9cacb6f | 2017-02-20 17:22:56 +0100 | [diff] [blame] | 318 | gsups->luop = lu_op_lst; |
| 319 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 320 | return gsups; |
| 321 | |
| 322 | failed: |
| 323 | osmo_gsup_server_destroy(gsups); |
| 324 | return NULL; |
| 325 | } |
| 326 | |
| 327 | void osmo_gsup_server_destroy(struct osmo_gsup_server *gsups) |
| 328 | { |
| 329 | if (gsups->link) { |
| 330 | ipa_server_link_close(gsups->link); |
| 331 | ipa_server_link_destroy(gsups->link); |
| 332 | gsups->link = NULL; |
| 333 | } |
| 334 | talloc_free(gsups); |
| 335 | } |