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> |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 27 | #include <osmocom/gsm/gsm48_ie.h> |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 28 | #include <osmocom/gsm/apn.h> |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 29 | #include <osmocom/gsm/gsm23003.h> |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 30 | |
Neels Hofmeyr | 2f75803 | 2019-11-20 00:37:07 +0100 | [diff] [blame] | 31 | #include <osmocom/hlr/gsup_server.h> |
| 32 | #include <osmocom/hlr/gsup_router.h> |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 33 | |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 34 | #define LOG_GSUP_CONN(conn, level, fmt, args...) \ |
| 35 | LOGP(DLGSUP, level, "GSUP peer %s: " fmt, \ |
| 36 | (conn) ? osmo_ipa_name_to_str(&(conn)->peer_name) : "NULL", ##args) |
| 37 | |
Neels Hofmeyr | a7d0f87 | 2019-10-30 02:08:28 +0100 | [diff] [blame] | 38 | struct msgb *osmo_gsup_msgb_alloc(const char *label) |
| 39 | { |
| 40 | struct msgb *msg = msgb_alloc_headroom(1024+16, 16, label); |
| 41 | OSMO_ASSERT(msg); |
| 42 | return msg; |
| 43 | } |
| 44 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 45 | static void osmo_gsup_server_send(struct osmo_gsup_conn *conn, |
| 46 | int proto_ext, struct msgb *msg_tx) |
| 47 | { |
| 48 | ipa_prepend_header_ext(msg_tx, proto_ext); |
| 49 | ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO); |
| 50 | ipa_server_conn_send(conn->conn, msg_tx); |
| 51 | } |
| 52 | |
| 53 | int osmo_gsup_conn_send(struct osmo_gsup_conn *conn, struct msgb *msg) |
| 54 | { |
| 55 | if (!conn) { |
| 56 | msgb_free(msg); |
| 57 | return -ENOTCONN; |
| 58 | } |
| 59 | |
| 60 | osmo_gsup_server_send(conn, IPAC_PROTO_EXT_GSUP, msg); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 65 | static void gsup_server_send_req_response(struct osmo_gsup_req *req, struct osmo_gsup_message *response) |
| 66 | { |
| 67 | struct osmo_gsup_server *server = req->cb_data; |
| 68 | struct osmo_gsup_conn *conn; |
| 69 | struct msgb *msg = osmo_gsup_msgb_alloc("GSUP Tx"); |
| 70 | int rc; |
| 71 | |
| 72 | conn = gsup_route_find_by_ipa_name(server, &req->source_name); |
| 73 | if (!conn) { |
| 74 | LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP client that sent this request was disconnected, cannot respond\n"); |
| 75 | msgb_free(msg); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | rc = osmo_gsup_encode(msg, response); |
| 80 | if (rc) { |
| 81 | LOG_GSUP_REQ(req, LOGL_ERROR, "Unable to encode: {%s}\n", |
| 82 | osmo_gsup_message_to_str_c(OTC_SELECT, response)); |
| 83 | msgb_free(msg); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | rc = osmo_gsup_conn_send(conn, msg); |
| 88 | if (rc) |
| 89 | LOG_GSUP_CONN(conn, LOGL_ERROR, "Unable to send: %s\n", osmo_gsup_message_to_str_c(OTC_SELECT, response)); |
| 90 | } |
| 91 | |
| 92 | struct osmo_gsup_req *osmo_gsup_conn_rx(struct osmo_gsup_conn *conn, struct msgb *msg) |
| 93 | { |
| 94 | struct osmo_gsup_req *req = osmo_gsup_req_new(conn->server, &conn->peer_name, msg, gsup_server_send_req_response, |
| 95 | conn->server, NULL); |
| 96 | if (!req) |
| 97 | return NULL; |
| 98 | |
| 99 | if (req->via_proxy.len) { |
| 100 | /* The source of the GSUP message is not the immediate GSUP peer, but that peer is our proxy for that |
| 101 | * source. Add it to the routes for this conn (so we can route responses back). */ |
| 102 | if (gsup_route_add_ipa_name(conn, &req->source_name)) { |
| 103 | LOG_GSUP_REQ(req, LOGL_ERROR, |
| 104 | "GSUP message received from %s via peer %s, but there already exists a" |
| 105 | " different route to this source, message is not routable\n", |
| 106 | osmo_ipa_name_to_str(&req->source_name), |
| 107 | osmo_ipa_name_to_str(&conn->peer_name)); |
| 108 | osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true); |
| 109 | return NULL; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return req; |
| 114 | } |
| 115 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 116 | static int osmo_gsup_conn_oap_handle(struct osmo_gsup_conn *conn, |
| 117 | struct msgb *msg_rx) |
| 118 | { |
Neels Hofmeyr | ec1b959 | 2016-12-11 01:22:23 +0100 | [diff] [blame] | 119 | #if 0 |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 120 | int rc; |
| 121 | struct msgb *msg_tx; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 122 | rc = oap_handle(&conn->oap_state, msg_rx, &msg_tx); |
| 123 | msgb_free(msg_rx); |
| 124 | if (rc < 0) |
| 125 | return rc; |
| 126 | |
| 127 | if (msg_tx) |
| 128 | osmo_gsup_conn_send(conn, IPAC_PROTO_EXT_OAP, msg_tx); |
| 129 | #endif |
| 130 | return 0; |
| 131 | } |
| 132 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 133 | /* Data from a given client has arrived over the socket */ |
| 134 | static int osmo_gsup_server_read_cb(struct ipa_server_conn *conn, |
| 135 | struct msgb *msg) |
| 136 | { |
| 137 | struct ipaccess_head *hh = (struct ipaccess_head *) msg->data; |
| 138 | struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg); |
| 139 | struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data; |
| 140 | int rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 141 | |
| 142 | msg->l2h = &hh->data[0]; |
| 143 | |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 144 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 145 | rc = ipa_server_conn_ccm(conn, msg); |
| 146 | if (rc < 0) { |
| 147 | /* conn is already invalid here! */ |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 148 | return -1; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 149 | } |
Harald Welte | 5341b5d | 2016-04-28 12:48:39 +0200 | [diff] [blame] | 150 | msgb_free(msg); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 151 | return 0; |
| 152 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 153 | |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 154 | if (hh->proto != IPAC_PROTO_OSMO) { |
| 155 | LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA stream ID 0x%02x\n", |
| 156 | hh->proto); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 157 | goto invalid; |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 158 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 159 | |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 160 | if (!he || msgb_l2len(msg) < sizeof(*he)) { |
| 161 | LOGP(DLGSUP, LOGL_NOTICE, "short IPA message\n"); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 162 | goto invalid; |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 163 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 164 | |
| 165 | msg->l2h = &he->data[0]; |
| 166 | |
| 167 | if (he->proto == IPAC_PROTO_EXT_GSUP) { |
| 168 | OSMO_ASSERT(clnt->server->read_cb != NULL); |
| 169 | clnt->server->read_cb(clnt, msg); |
| 170 | /* expecting read_cb() to free msg */ |
| 171 | } else if (he->proto == IPAC_PROTO_EXT_OAP) { |
| 172 | return osmo_gsup_conn_oap_handle(clnt, msg); |
| 173 | /* osmo_gsup_client_oap_handle frees msg */ |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 174 | } else { |
| 175 | LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA Osmo Proto 0x%02x\n", |
| 176 | hh->proto); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 177 | goto invalid; |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 178 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 179 | |
| 180 | return 0; |
| 181 | |
| 182 | invalid: |
| 183 | LOGP(DLGSUP, LOGL_NOTICE, |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 184 | "GSUP received an invalid IPA message from %s:%d: %s\n", |
| 185 | conn->addr, conn->port, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg))); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 186 | msgb_free(msg); |
| 187 | return -1; |
| 188 | |
| 189 | } |
| 190 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 191 | static void osmo_tlvp_dump(const struct tlv_parsed *tlvp, |
| 192 | int subsys, int level) |
| 193 | { |
| 194 | unsigned int i; |
| 195 | |
| 196 | for (i = 0; i < ARRAY_SIZE(tlvp->lv); i++) { |
| 197 | if (!TLVP_PRESENT(tlvp, i)) |
| 198 | continue; |
| 199 | |
| 200 | LOGP(subsys, level, "%u: %s\n", i, |
| 201 | TLVP_VAL(tlvp, i)); |
| 202 | LOGP(subsys, level, "%u: %s\n", i, |
| 203 | osmo_hexdump(TLVP_VAL(tlvp, i), |
| 204 | TLVP_LEN(tlvp, i))); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /* FIXME: should this be parrt of ipas_server handling, not GSUP? */ |
| 209 | static void tlvp_copy(void *ctx, struct tlv_parsed *out, const struct tlv_parsed *in) |
| 210 | { |
| 211 | unsigned int i; |
| 212 | |
| 213 | for (i = 0; i < ARRAY_SIZE(out->lv); i++) { |
| 214 | if (!TLVP_PRESENT(in, i)) { |
| 215 | if (TLVP_PRESENT(out, i)) { |
| 216 | talloc_free((void *) out->lv[i].val); |
| 217 | out->lv[i].val = NULL; |
| 218 | out->lv[i].len = 0; |
| 219 | } |
| 220 | continue; |
| 221 | } |
| 222 | out->lv[i].val = talloc_memdup(ctx, in->lv[i].val, in->lv[i].len); |
| 223 | out->lv[i].len = in->lv[i].len; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | int osmo_gsup_conn_ccm_get(const struct osmo_gsup_conn *clnt, uint8_t **addr, |
| 228 | uint8_t tag) |
| 229 | { |
| 230 | if (!TLVP_PRESENT(&clnt->ccm, tag)) |
| 231 | return -ENODEV; |
| 232 | *addr = (uint8_t *) TLVP_VAL(&clnt->ccm, tag); |
| 233 | |
| 234 | return TLVP_LEN(&clnt->ccm, tag); |
| 235 | } |
| 236 | |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 237 | static int osmo_gsup_server_ccm_cb(struct ipa_server_conn *conn, |
| 238 | struct msgb *msg, struct tlv_parsed *tlvp, |
| 239 | struct ipaccess_unit *unit) |
| 240 | { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 241 | struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data; |
Neels Hofmeyr | ec1b959 | 2016-12-11 01:22:23 +0100 | [diff] [blame] | 242 | uint8_t *addr = NULL; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 243 | size_t addr_len; |
| 244 | |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 245 | LOGP(DLGSUP, LOGL_INFO, "CCM Callback\n"); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 246 | |
| 247 | /* FIXME: should this be parrt of ipas_server handling, not |
| 248 | * GSUP? */ |
| 249 | tlvp_copy(clnt, &clnt->ccm, tlvp); |
| 250 | osmo_tlvp_dump(tlvp, DLGSUP, LOGL_INFO); |
| 251 | |
| 252 | addr_len = osmo_gsup_conn_ccm_get(clnt, &addr, IPAC_IDTAG_SERNR); |
Neels Hofmeyr | 5ecdc56 | 2017-02-24 06:38:04 +0100 | [diff] [blame] | 253 | if (addr_len <= 0) { |
| 254 | LOGP(DLGSUP, LOGL_ERROR, "GSUP client %s:%u has no %s IE and" |
| 255 | " cannot be routed\n", |
| 256 | conn->addr, conn->port, |
| 257 | ipa_ccm_idtag_name(IPAC_IDTAG_SERNR)); |
| 258 | return -EINVAL; |
| 259 | } |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 260 | |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 261 | osmo_ipa_name_set(&clnt->peer_name, addr, addr_len); |
| 262 | gsup_route_add_ipa_name(clnt, &clnt->peer_name); |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 266 | static void osmo_gsup_conn_free(struct osmo_gsup_conn *conn) |
| 267 | { |
| 268 | gsup_route_del_conn(conn); |
| 269 | llist_del(&conn->list); |
| 270 | talloc_free(conn); |
| 271 | } |
| 272 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 273 | static int osmo_gsup_server_closed_cb(struct ipa_server_conn *conn) |
| 274 | { |
| 275 | struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data; |
| 276 | |
| 277 | LOGP(DLGSUP, LOGL_INFO, "Lost GSUP client %s:%d\n", |
| 278 | conn->addr, conn->port); |
| 279 | |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 280 | osmo_gsup_conn_free(clnt); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 281 | return 0; |
| 282 | } |
| 283 | |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 284 | /* Add conn to the clients list in a way that conn->auc_3g_ind takes the lowest |
| 285 | * unused integer and the list of clients remains sorted by auc_3g_ind. |
| 286 | * Keep this function non-static to allow linking in a unit test. */ |
| 287 | void osmo_gsup_server_add_conn(struct llist_head *clients, |
| 288 | struct osmo_gsup_conn *conn) |
| 289 | { |
| 290 | struct osmo_gsup_conn *c; |
| 291 | struct osmo_gsup_conn *prev_conn; |
| 292 | |
| 293 | c = llist_first_entry_or_null(clients, struct osmo_gsup_conn, list); |
| 294 | |
| 295 | /* Is the first index, 0, unused? */ |
| 296 | if (!c || c->auc_3g_ind > 0) { |
| 297 | conn->auc_3g_ind = 0; |
| 298 | llist_add(&conn->list, clients); |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | /* Look for a gap later on */ |
| 303 | prev_conn = NULL; |
| 304 | llist_for_each_entry(c, clients, list) { |
| 305 | /* skip first item, we know it has auc_3g_ind == 0. */ |
| 306 | if (!prev_conn) { |
| 307 | prev_conn = c; |
| 308 | continue; |
| 309 | } |
| 310 | if (c->auc_3g_ind > prev_conn->auc_3g_ind + 1) |
| 311 | break; |
| 312 | prev_conn = c; |
| 313 | } |
| 314 | |
| 315 | OSMO_ASSERT(prev_conn); |
| 316 | |
| 317 | conn->auc_3g_ind = prev_conn->auc_3g_ind + 1; |
| 318 | llist_add(&conn->list, &prev_conn->list); |
| 319 | } |
| 320 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 321 | /* a client has connected to the server socket and we have accept()ed it */ |
| 322 | static int osmo_gsup_server_accept_cb(struct ipa_server_link *link, int fd) |
| 323 | { |
| 324 | struct osmo_gsup_conn *conn; |
| 325 | struct osmo_gsup_server *gsups = |
| 326 | (struct osmo_gsup_server *) link->data; |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 327 | int rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 328 | |
Harald Welte | a7617e9 | 2016-04-28 12:57:10 +0200 | [diff] [blame] | 329 | conn = talloc_zero(gsups, struct osmo_gsup_conn); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 330 | OSMO_ASSERT(conn); |
| 331 | |
Harald Welte | a7617e9 | 2016-04-28 12:57:10 +0200 | [diff] [blame] | 332 | conn->conn = ipa_server_conn_create(gsups, link, fd, |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 333 | osmo_gsup_server_read_cb, |
| 334 | osmo_gsup_server_closed_cb, conn); |
| 335 | OSMO_ASSERT(conn->conn); |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 336 | conn->conn->ccm_cb = osmo_gsup_server_ccm_cb; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 337 | |
| 338 | /* link data structure with server structure */ |
| 339 | conn->server = gsups; |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 340 | osmo_gsup_server_add_conn(&gsups->clients, conn); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 341 | |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 342 | LOGP(DLGSUP, LOGL_INFO, "New GSUP client %s:%d (IND=%u)\n", |
| 343 | conn->conn->addr, conn->conn->port, conn->auc_3g_ind); |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 344 | |
| 345 | /* request the identity of the client */ |
| 346 | rc = ipa_ccm_send_id_req(fd); |
| 347 | if (rc < 0) |
| 348 | goto failed; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 349 | #if 0 |
| 350 | rc = oap_init(&gsups->oap_config, &conn->oap_state); |
| 351 | if (rc != 0) |
| 352 | goto failed; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 353 | #endif |
| 354 | return 0; |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 355 | failed: |
| 356 | ipa_server_conn_destroy(conn->conn); |
| 357 | return -1; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | struct osmo_gsup_server * |
Max | 9cacb6f | 2017-02-20 17:22:56 +0100 | [diff] [blame] | 361 | osmo_gsup_server_create(void *ctx, const char *ip_addr, uint16_t tcp_port, |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 362 | osmo_gsup_read_cb_t read_cb, void *priv) |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 363 | { |
| 364 | struct osmo_gsup_server *gsups; |
| 365 | int rc; |
| 366 | |
| 367 | gsups = talloc_zero(ctx, struct osmo_gsup_server); |
| 368 | OSMO_ASSERT(gsups); |
| 369 | |
| 370 | INIT_LLIST_HEAD(&gsups->clients); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 371 | INIT_LLIST_HEAD(&gsups->routes); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 372 | |
| 373 | gsups->link = ipa_server_link_create(gsups, |
| 374 | /* no e1inp */ NULL, |
| 375 | ip_addr, tcp_port, |
| 376 | osmo_gsup_server_accept_cb, |
| 377 | gsups); |
| 378 | if (!gsups->link) |
| 379 | goto failed; |
| 380 | |
| 381 | gsups->read_cb = read_cb; |
Harald Welte | 32acace | 2018-06-16 17:07:28 +0200 | [diff] [blame] | 382 | gsups->priv = priv; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 383 | |
| 384 | rc = ipa_server_link_open(gsups->link); |
| 385 | if (rc < 0) |
| 386 | goto failed; |
| 387 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 388 | return gsups; |
| 389 | |
| 390 | failed: |
| 391 | osmo_gsup_server_destroy(gsups); |
| 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | void osmo_gsup_server_destroy(struct osmo_gsup_server *gsups) |
| 396 | { |
| 397 | if (gsups->link) { |
| 398 | ipa_server_link_close(gsups->link); |
| 399 | ipa_server_link_destroy(gsups->link); |
| 400 | gsups->link = NULL; |
| 401 | } |
| 402 | talloc_free(gsups); |
| 403 | } |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 404 | |
Neels Hofmeyr | 5aeb438 | 2018-05-04 16:02:44 +0200 | [diff] [blame] | 405 | /* Set GSUP message's pdp_infos[0] to a wildcard APN. |
| 406 | * Use the provided apn_buf to store the produced APN data. This must remain valid until |
Neels Hofmeyr | 1b8a1dc | 2018-05-04 16:46:47 +0200 | [diff] [blame] | 407 | * osmo_gsup_encode() is done. Return 0 if an entry was added, -ENOMEM if the provided buffer is too |
| 408 | * small. */ |
| 409 | int osmo_gsup_configure_wildcard_apn(struct osmo_gsup_message *gsup, |
| 410 | uint8_t *apn_buf, size_t apn_buf_size) |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 411 | { |
| 412 | int l; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 413 | |
Neels Hofmeyr | 5aeb438 | 2018-05-04 16:02:44 +0200 | [diff] [blame] | 414 | l = osmo_apn_from_str(apn_buf, apn_buf_size, "*"); |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 415 | if (l <= 0) |
Neels Hofmeyr | 1b8a1dc | 2018-05-04 16:46:47 +0200 | [diff] [blame] | 416 | return -ENOMEM; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 417 | |
Neels Hofmeyr | 5aeb438 | 2018-05-04 16:02:44 +0200 | [diff] [blame] | 418 | gsup->pdp_infos[0].apn_enc = apn_buf; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 419 | gsup->pdp_infos[0].apn_enc_len = l; |
| 420 | gsup->pdp_infos[0].have_info = 1; |
| 421 | gsup->num_pdp_infos = 1; |
| 422 | /* FIXME: use real value: */ |
| 423 | gsup->pdp_infos[0].context_id = 1; |
Neels Hofmeyr | 1b8a1dc | 2018-05-04 16:46:47 +0200 | [diff] [blame] | 424 | |
| 425 | return 0; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 426 | } |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 427 | |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 428 | |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 429 | /** |
| 430 | * Populate a gsup message structure with an Insert Subscriber Data Message. |
| 431 | * All required memory buffers for data pointed to by pointers in struct omso_gsup_message |
| 432 | * must be allocated by the caller and should have the same lifetime as the gsup parameter. |
| 433 | * |
| 434 | * \param[out] gsup The gsup message to populate. |
| 435 | * \param[in] imsi The subscriber's IMSI. |
| 436 | * \param[in] msisdn The subscriber's MSISDN. |
| 437 | * \param[out] msisdn_enc A buffer large enough to store the MSISDN in encoded form. |
| 438 | * \param[in] msisdn_enc_size Size of the buffer (must be >= OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN). |
| 439 | * \param[out] apn_buf A buffer large enough to store an APN (required if cn_domain is OSMO_GSUP_CN_DOMAIN_PS). |
| 440 | * \param[in] apn_buf_size Size of APN buffer (must be >= APN_MAXLEN). |
| 441 | * \param[in] cn_domain The CN Domain of the subscriber connection. |
| 442 | * \returns 0 on success, and negative on error. |
| 443 | */ |
| 444 | int osmo_gsup_create_insert_subscriber_data_msg(struct osmo_gsup_message *gsup, const char *imsi, const char *msisdn, |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 445 | uint8_t *msisdn_enc, size_t msisdn_enc_size, |
| 446 | uint8_t *apn_buf, size_t apn_buf_size, |
| 447 | enum osmo_gsup_cn_domain cn_domain) |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 448 | { |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 449 | int len; |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 450 | |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 451 | OSMO_ASSERT(gsup); |
| 452 | *gsup = (struct osmo_gsup_message){ |
| 453 | .message_type = OSMO_GSUP_MSGT_INSERT_DATA_REQUEST, |
| 454 | }; |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 455 | |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 456 | osmo_strlcpy(gsup->imsi, imsi, sizeof(gsup->imsi)); |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 457 | |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 458 | if (msisdn_enc_size < OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN) |
| 459 | return -ENOSPC; |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 460 | |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 461 | OSMO_ASSERT(msisdn_enc); |
| 462 | len = gsm48_encode_bcd_number(msisdn_enc, msisdn_enc_size, 0, msisdn); |
| 463 | if (len < 1) { |
| 464 | LOGP(DLGSUP, LOGL_ERROR, "%s: Error: cannot encode MSISDN '%s'\n", imsi, msisdn); |
| 465 | return -ENOSPC; |
| 466 | } |
| 467 | gsup->msisdn_enc = msisdn_enc; |
| 468 | gsup->msisdn_enc_len = len; |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 469 | |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 470 | #pragma message "FIXME: deal with encoding the following data: gsup.hlr_enc" |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 471 | |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 472 | gsup->cn_domain = cn_domain; |
| 473 | if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) { |
| 474 | OSMO_ASSERT(apn_buf_size >= APN_MAXLEN); |
| 475 | OSMO_ASSERT(apn_buf); |
| 476 | /* FIXME: PDP infos - use more fine-grained access control |
| 477 | instead of wildcard APN */ |
| 478 | osmo_gsup_configure_wildcard_apn(gsup, apn_buf, apn_buf_size); |
| 479 | } |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 480 | |
Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 481 | return 0; |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 482 | } |