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