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