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); |
Harald Welte | 5341b5d | 2016-04-28 12:48:39 +0200 | [diff] [blame] | 182 | msgb_free(msg); |
Vadim Yanitskiy | f8b73f0 | 2023-07-05 01:06:20 +0700 | [diff] [blame] | 183 | if (rc < 0) /* conn is already invalid here! */ |
| 184 | return -1; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 185 | return 0; |
| 186 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 187 | |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 188 | if (hh->proto != IPAC_PROTO_OSMO) { |
| 189 | LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA stream ID 0x%02x\n", |
| 190 | hh->proto); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 191 | goto invalid; |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 192 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 193 | |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 194 | if (!he || msgb_l2len(msg) < sizeof(*he)) { |
| 195 | LOGP(DLGSUP, LOGL_NOTICE, "short IPA message\n"); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 196 | goto invalid; |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 197 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 198 | |
| 199 | msg->l2h = &he->data[0]; |
| 200 | |
| 201 | if (he->proto == IPAC_PROTO_EXT_GSUP) { |
| 202 | OSMO_ASSERT(clnt->server->read_cb != NULL); |
| 203 | clnt->server->read_cb(clnt, msg); |
| 204 | /* expecting read_cb() to free msg */ |
| 205 | } else if (he->proto == IPAC_PROTO_EXT_OAP) { |
| 206 | return osmo_gsup_conn_oap_handle(clnt, msg); |
| 207 | /* osmo_gsup_client_oap_handle frees msg */ |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 208 | } else { |
| 209 | LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA Osmo Proto 0x%02x\n", |
| 210 | hh->proto); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 211 | goto invalid; |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 212 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 213 | |
| 214 | return 0; |
| 215 | |
| 216 | invalid: |
| 217 | LOGP(DLGSUP, LOGL_NOTICE, |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 218 | "GSUP received an invalid IPA message from %s:%d: %s\n", |
| 219 | conn->addr, conn->port, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg))); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 220 | msgb_free(msg); |
| 221 | return -1; |
| 222 | |
| 223 | } |
| 224 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 225 | static void osmo_tlvp_dump(const struct tlv_parsed *tlvp, |
| 226 | int subsys, int level) |
| 227 | { |
| 228 | unsigned int i; |
| 229 | |
| 230 | for (i = 0; i < ARRAY_SIZE(tlvp->lv); i++) { |
| 231 | if (!TLVP_PRESENT(tlvp, i)) |
| 232 | continue; |
| 233 | |
| 234 | LOGP(subsys, level, "%u: %s\n", i, |
| 235 | TLVP_VAL(tlvp, i)); |
| 236 | LOGP(subsys, level, "%u: %s\n", i, |
| 237 | osmo_hexdump(TLVP_VAL(tlvp, i), |
| 238 | TLVP_LEN(tlvp, i))); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* FIXME: should this be parrt of ipas_server handling, not GSUP? */ |
| 243 | static void tlvp_copy(void *ctx, struct tlv_parsed *out, const struct tlv_parsed *in) |
| 244 | { |
| 245 | unsigned int i; |
| 246 | |
| 247 | for (i = 0; i < ARRAY_SIZE(out->lv); i++) { |
| 248 | if (!TLVP_PRESENT(in, i)) { |
| 249 | if (TLVP_PRESENT(out, i)) { |
| 250 | talloc_free((void *) out->lv[i].val); |
| 251 | out->lv[i].val = NULL; |
| 252 | out->lv[i].len = 0; |
| 253 | } |
| 254 | continue; |
| 255 | } |
| 256 | out->lv[i].val = talloc_memdup(ctx, in->lv[i].val, in->lv[i].len); |
| 257 | out->lv[i].len = in->lv[i].len; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | int osmo_gsup_conn_ccm_get(const struct osmo_gsup_conn *clnt, uint8_t **addr, |
| 262 | uint8_t tag) |
| 263 | { |
| 264 | if (!TLVP_PRESENT(&clnt->ccm, tag)) |
| 265 | return -ENODEV; |
| 266 | *addr = (uint8_t *) TLVP_VAL(&clnt->ccm, tag); |
| 267 | |
| 268 | return TLVP_LEN(&clnt->ccm, tag); |
| 269 | } |
| 270 | |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 271 | static int osmo_gsup_server_ccm_cb(struct ipa_server_conn *conn, |
| 272 | struct msgb *msg, struct tlv_parsed *tlvp, |
| 273 | struct ipaccess_unit *unit) |
| 274 | { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 275 | struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data; |
Neels Hofmeyr | ec1b959 | 2016-12-11 01:22:23 +0100 | [diff] [blame] | 276 | uint8_t *addr = NULL; |
Neels Hofmeyr | 1bd3ec4 | 2020-05-04 17:53:50 +0200 | [diff] [blame] | 277 | int addr_len; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 278 | |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 279 | LOGP(DLGSUP, LOGL_INFO, "CCM Callback\n"); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 280 | |
| 281 | /* FIXME: should this be parrt of ipas_server handling, not |
| 282 | * GSUP? */ |
| 283 | tlvp_copy(clnt, &clnt->ccm, tlvp); |
| 284 | osmo_tlvp_dump(tlvp, DLGSUP, LOGL_INFO); |
| 285 | |
| 286 | addr_len = osmo_gsup_conn_ccm_get(clnt, &addr, IPAC_IDTAG_SERNR); |
Neels Hofmeyr | 5ecdc56 | 2017-02-24 06:38:04 +0100 | [diff] [blame] | 287 | if (addr_len <= 0) { |
| 288 | LOGP(DLGSUP, LOGL_ERROR, "GSUP client %s:%u has no %s IE and" |
| 289 | " cannot be routed\n", |
| 290 | conn->addr, conn->port, |
| 291 | ipa_ccm_idtag_name(IPAC_IDTAG_SERNR)); |
| 292 | return -EINVAL; |
| 293 | } |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 294 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 295 | osmo_ipa_name_set(&clnt->peer_name, addr, addr_len); |
| 296 | gsup_route_add_ipa_name(clnt, &clnt->peer_name); |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 300 | static void osmo_gsup_conn_free(struct osmo_gsup_conn *conn) |
| 301 | { |
| 302 | gsup_route_del_conn(conn); |
| 303 | llist_del(&conn->list); |
| 304 | talloc_free(conn); |
| 305 | } |
| 306 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 307 | static int osmo_gsup_server_closed_cb(struct ipa_server_conn *conn) |
| 308 | { |
| 309 | struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data; |
| 310 | |
| 311 | LOGP(DLGSUP, LOGL_INFO, "Lost GSUP client %s:%d\n", |
| 312 | conn->addr, conn->port); |
| 313 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 314 | osmo_gsup_conn_free(clnt); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | |
Pau Espin Pedrol | 23ac586 | 2020-10-12 16:39:38 +0200 | [diff] [blame] | 318 | static void update_fd_settings(int fd) |
| 319 | { |
| 320 | int ret; |
| 321 | int val; |
| 322 | |
| 323 | /*TODO: Set keepalive settings here. See OS#4312 */ |
| 324 | |
| 325 | val = 1; |
| 326 | ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)); |
| 327 | if (ret < 0) |
| 328 | LOGP(DLGSUP, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno)); |
| 329 | } |
| 330 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 331 | /* a client has connected to the server socket and we have accept()ed it */ |
| 332 | static int osmo_gsup_server_accept_cb(struct ipa_server_link *link, int fd) |
| 333 | { |
| 334 | struct osmo_gsup_conn *conn; |
| 335 | struct osmo_gsup_server *gsups = |
| 336 | (struct osmo_gsup_server *) link->data; |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 337 | int rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 338 | |
Harald Welte | a7617e9 | 2016-04-28 12:57:10 +0200 | [diff] [blame] | 339 | conn = talloc_zero(gsups, struct osmo_gsup_conn); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 340 | OSMO_ASSERT(conn); |
| 341 | |
Harald Welte | a7617e9 | 2016-04-28 12:57:10 +0200 | [diff] [blame] | 342 | conn->conn = ipa_server_conn_create(gsups, link, fd, |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 343 | osmo_gsup_server_read_cb, |
| 344 | osmo_gsup_server_closed_cb, conn); |
| 345 | OSMO_ASSERT(conn->conn); |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 346 | conn->conn->ccm_cb = osmo_gsup_server_ccm_cb; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 347 | |
| 348 | /* link data structure with server structure */ |
| 349 | conn->server = gsups; |
Neels Hofmeyr | 3f9d197 | 2019-12-12 04:04:53 +0100 | [diff] [blame] | 350 | llist_add_tail(&conn->list, &gsups->clients); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 351 | |
Neels Hofmeyr | 3f9d197 | 2019-12-12 04:04:53 +0100 | [diff] [blame] | 352 | LOGP(DLGSUP, LOGL_INFO, "New GSUP client %s:%d\n", conn->conn->addr, conn->conn->port); |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 353 | |
Pau Espin Pedrol | 23ac586 | 2020-10-12 16:39:38 +0200 | [diff] [blame] | 354 | update_fd_settings(fd); |
| 355 | |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 356 | /* request the identity of the client */ |
| 357 | rc = ipa_ccm_send_id_req(fd); |
| 358 | if (rc < 0) |
| 359 | goto failed; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 360 | #if 0 |
| 361 | rc = oap_init(&gsups->oap_config, &conn->oap_state); |
| 362 | if (rc != 0) |
| 363 | goto failed; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 364 | #endif |
| 365 | return 0; |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 366 | failed: |
| 367 | ipa_server_conn_destroy(conn->conn); |
| 368 | return -1; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | struct osmo_gsup_server * |
Max | 9cacb6f | 2017-02-20 17:22:56 +0100 | [diff] [blame] | 372 | 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] | 373 | osmo_gsup_read_cb_t read_cb, void *priv) |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 374 | { |
| 375 | struct osmo_gsup_server *gsups; |
| 376 | int rc; |
| 377 | |
| 378 | gsups = talloc_zero(ctx, struct osmo_gsup_server); |
| 379 | OSMO_ASSERT(gsups); |
| 380 | |
| 381 | INIT_LLIST_HEAD(&gsups->clients); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 382 | INIT_LLIST_HEAD(&gsups->routes); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 383 | |
| 384 | gsups->link = ipa_server_link_create(gsups, |
| 385 | /* no e1inp */ NULL, |
| 386 | ip_addr, tcp_port, |
| 387 | osmo_gsup_server_accept_cb, |
| 388 | gsups); |
| 389 | if (!gsups->link) |
| 390 | goto failed; |
| 391 | |
| 392 | gsups->read_cb = read_cb; |
Harald Welte | 32acace | 2018-06-16 17:07:28 +0200 | [diff] [blame] | 393 | gsups->priv = priv; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 394 | |
| 395 | rc = ipa_server_link_open(gsups->link); |
| 396 | if (rc < 0) |
| 397 | goto failed; |
| 398 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 399 | return gsups; |
| 400 | |
| 401 | failed: |
| 402 | osmo_gsup_server_destroy(gsups); |
| 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | void osmo_gsup_server_destroy(struct osmo_gsup_server *gsups) |
| 407 | { |
| 408 | if (gsups->link) { |
| 409 | ipa_server_link_close(gsups->link); |
| 410 | ipa_server_link_destroy(gsups->link); |
| 411 | gsups->link = NULL; |
| 412 | } |
| 413 | talloc_free(gsups); |
| 414 | } |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 415 | |
Neels Hofmeyr | 5aeb438 | 2018-05-04 16:02:44 +0200 | [diff] [blame] | 416 | /* Set GSUP message's pdp_infos[0] to a wildcard APN. |
| 417 | * 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] | 418 | * osmo_gsup_encode() is done. Return 0 if an entry was added, -ENOMEM if the provided buffer is too |
| 419 | * small. */ |
| 420 | int osmo_gsup_configure_wildcard_apn(struct osmo_gsup_message *gsup, |
| 421 | uint8_t *apn_buf, size_t apn_buf_size) |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 422 | { |
| 423 | int l; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 424 | |
Neels Hofmeyr | 5aeb438 | 2018-05-04 16:02:44 +0200 | [diff] [blame] | 425 | l = osmo_apn_from_str(apn_buf, apn_buf_size, "*"); |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 426 | if (l <= 0) |
Neels Hofmeyr | 1b8a1dc | 2018-05-04 16:46:47 +0200 | [diff] [blame] | 427 | return -ENOMEM; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 428 | |
Neels Hofmeyr | 5aeb438 | 2018-05-04 16:02:44 +0200 | [diff] [blame] | 429 | gsup->pdp_infos[0].apn_enc = apn_buf; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 430 | gsup->pdp_infos[0].apn_enc_len = l; |
| 431 | gsup->pdp_infos[0].have_info = 1; |
| 432 | gsup->num_pdp_infos = 1; |
| 433 | /* FIXME: use real value: */ |
| 434 | gsup->pdp_infos[0].context_id = 1; |
Neels Hofmeyr | 1b8a1dc | 2018-05-04 16:46:47 +0200 | [diff] [blame] | 435 | |
| 436 | return 0; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 437 | } |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 438 | |
| 439 | /** |
| 440 | * Populate a gsup message structure with an Insert Subscriber Data Message. |
Vadim Yanitskiy | fa20702 | 2020-05-31 03:52:52 +0700 | [diff] [blame] | 441 | * 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] | 442 | * must be allocated by the caller and should have the same lifetime as the gsup parameter. |
| 443 | * |
| 444 | * \param[out] gsup The gsup message to populate. |
| 445 | * \param[in] imsi The subscriber's IMSI. |
| 446 | * \param[in] msisdn The subscriber's MSISDN. |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 447 | * \param[in] cn_domain The CN Domain of the subscriber connection. |
Alexander Couzens | 2989873 | 2023-04-11 17:53:33 +0200 | [diff] [blame] | 448 | * \param[in] talloc_ctx To allocation memory for dynamic fields (msisdn, apn) in the gsup field |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 449 | * \returns 0 on success, and negative on error. |
| 450 | */ |
| 451 | int osmo_gsup_create_insert_subscriber_data_msg(struct osmo_gsup_message *gsup, const char *imsi, const char *msisdn, |
Alexander Couzens | 2989873 | 2023-04-11 17:53:33 +0200 | [diff] [blame] | 452 | enum osmo_gsup_cn_domain cn_domain, |
| 453 | void *talloc_ctx) |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 454 | { |
| 455 | int len; |
Alexander Couzens | 2989873 | 2023-04-11 17:53:33 +0200 | [diff] [blame] | 456 | uint8_t *msisdn_buf = talloc_size(talloc_ctx, OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN); |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 457 | |
| 458 | OSMO_ASSERT(gsup); |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 459 | *gsup = (struct osmo_gsup_message){ |
| 460 | .message_type = OSMO_GSUP_MSGT_INSERT_DATA_REQUEST, |
| 461 | }; |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 462 | |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 463 | osmo_strlcpy(gsup->imsi, imsi, sizeof(gsup->imsi)); |
| 464 | |
Alexander Couzens | 2989873 | 2023-04-11 17:53:33 +0200 | [diff] [blame] | 465 | len = gsm48_encode_bcd_number(msisdn_buf, OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN, 0, msisdn); |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 466 | if (len < 1) { |
| 467 | LOGP(DLGSUP, LOGL_ERROR, "%s: Error: cannot encode MSISDN '%s'\n", imsi, msisdn); |
| 468 | return -ENOSPC; |
| 469 | } |
Alexander Couzens | 2989873 | 2023-04-11 17:53:33 +0200 | [diff] [blame] | 470 | gsup->msisdn_enc = msisdn_buf; |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 471 | gsup->msisdn_enc_len = len; |
| 472 | |
| 473 | #pragma message "FIXME: deal with encoding the following data: gsup.hlr_enc" |
| 474 | |
| 475 | gsup->cn_domain = cn_domain; |
| 476 | if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) { |
Alexander Couzens | 2989873 | 2023-04-11 17:53:33 +0200 | [diff] [blame] | 477 | uint8_t *apn_buf = talloc_size(talloc_ctx, APN_MAXLEN); |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 478 | /* FIXME: PDP infos - use more fine-grained access control |
| 479 | instead of wildcard APN */ |
Alexander Couzens | 2989873 | 2023-04-11 17:53:33 +0200 | [diff] [blame] | 480 | osmo_gsup_configure_wildcard_apn(gsup, apn_buf, APN_MAXLEN); |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | return 0; |
| 484 | } |
Neels Hofmeyr | 76328bd | 2019-11-20 03:35:37 +0100 | [diff] [blame] | 485 | |
| 486 | int osmo_gsup_forward_to_local_peer(struct osmo_gsup_server *server, const struct osmo_cni_peer_id *to_peer, |
| 487 | struct osmo_gsup_req *req, struct osmo_gsup_message *modified_gsup) |
| 488 | { |
| 489 | int rc; |
| 490 | /* To forward to a remote entity (HLR, SMSC,...), we need to indicate the original source name in the Source |
| 491 | * Name IE to make sure the reply can be routed back. Store the sender in gsup->source_name -- the remote entity |
| 492 | * is required to return this as gsup->destination_name so that the reply gets routed to the original sender. */ |
| 493 | struct osmo_gsup_message forward = *(modified_gsup? : &req->gsup); |
| 494 | |
| 495 | if (req->source_name.type != OSMO_CNI_PEER_ID_IPA_NAME) { |
| 496 | osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "Unsupported GSUP peer id type: %s", |
| 497 | osmo_cni_peer_id_type_name(req->source_name.type)); |
| 498 | rc = -ENOTSUP; |
| 499 | goto routing_error; |
| 500 | } |
| 501 | forward.source_name = req->source_name.ipa_name.val; |
| 502 | forward.source_name_len = req->source_name.ipa_name.len; |
| 503 | |
| 504 | if (to_peer->type != OSMO_CNI_PEER_ID_IPA_NAME) { |
| 505 | osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "Unsupported GSUP peer id type: %s", |
| 506 | osmo_cni_peer_id_type_name(to_peer->type)); |
| 507 | rc = -ENOTSUP; |
| 508 | goto routing_error; |
| 509 | } |
| 510 | LOG_GSUP_REQ(req, LOGL_INFO, "Forwarding to %s\n", osmo_cni_peer_id_to_str(to_peer)); |
| 511 | rc = osmo_gsup_enc_send_to_ipa_name(server, &to_peer->ipa_name, &forward); |
| 512 | if (rc) |
| 513 | goto routing_error; |
| 514 | osmo_gsup_req_free(req); |
| 515 | return 0; |
| 516 | |
| 517 | routing_error: |
| 518 | osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true); |
| 519 | return rc; |
| 520 | } |