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 | |
Pau Espin Pedrol | 23ac586 | 2020-10-12 16:39:38 +0200 | [diff] [blame] | 320 | static void update_fd_settings(int fd) |
| 321 | { |
| 322 | int ret; |
| 323 | int val; |
| 324 | |
| 325 | /*TODO: Set keepalive settings here. See OS#4312 */ |
| 326 | |
| 327 | val = 1; |
| 328 | ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)); |
| 329 | if (ret < 0) |
| 330 | LOGP(DLGSUP, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno)); |
| 331 | } |
| 332 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 333 | /* a client has connected to the server socket and we have accept()ed it */ |
| 334 | static int osmo_gsup_server_accept_cb(struct ipa_server_link *link, int fd) |
| 335 | { |
| 336 | struct osmo_gsup_conn *conn; |
| 337 | struct osmo_gsup_server *gsups = |
| 338 | (struct osmo_gsup_server *) link->data; |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 339 | int rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 340 | |
Harald Welte | a7617e9 | 2016-04-28 12:57:10 +0200 | [diff] [blame] | 341 | conn = talloc_zero(gsups, struct osmo_gsup_conn); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 342 | OSMO_ASSERT(conn); |
| 343 | |
Harald Welte | a7617e9 | 2016-04-28 12:57:10 +0200 | [diff] [blame] | 344 | conn->conn = ipa_server_conn_create(gsups, link, fd, |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 345 | osmo_gsup_server_read_cb, |
| 346 | osmo_gsup_server_closed_cb, conn); |
| 347 | OSMO_ASSERT(conn->conn); |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 348 | conn->conn->ccm_cb = osmo_gsup_server_ccm_cb; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 349 | |
| 350 | /* link data structure with server structure */ |
| 351 | conn->server = gsups; |
Neels Hofmeyr | 3f9d197 | 2019-12-12 04:04:53 +0100 | [diff] [blame] | 352 | llist_add_tail(&conn->list, &gsups->clients); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 353 | |
Neels Hofmeyr | 3f9d197 | 2019-12-12 04:04:53 +0100 | [diff] [blame] | 354 | 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] | 355 | |
Pau Espin Pedrol | 23ac586 | 2020-10-12 16:39:38 +0200 | [diff] [blame] | 356 | update_fd_settings(fd); |
| 357 | |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 358 | /* request the identity of the client */ |
| 359 | rc = ipa_ccm_send_id_req(fd); |
| 360 | if (rc < 0) |
| 361 | goto failed; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 362 | #if 0 |
| 363 | rc = oap_init(&gsups->oap_config, &conn->oap_state); |
| 364 | if (rc != 0) |
| 365 | goto failed; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 366 | #endif |
| 367 | return 0; |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 368 | failed: |
| 369 | ipa_server_conn_destroy(conn->conn); |
| 370 | return -1; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | struct osmo_gsup_server * |
Max | 9cacb6f | 2017-02-20 17:22:56 +0100 | [diff] [blame] | 374 | 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] | 375 | osmo_gsup_read_cb_t read_cb, void *priv) |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 376 | { |
| 377 | struct osmo_gsup_server *gsups; |
| 378 | int rc; |
| 379 | |
| 380 | gsups = talloc_zero(ctx, struct osmo_gsup_server); |
| 381 | OSMO_ASSERT(gsups); |
| 382 | |
| 383 | INIT_LLIST_HEAD(&gsups->clients); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 384 | INIT_LLIST_HEAD(&gsups->routes); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 385 | |
| 386 | gsups->link = ipa_server_link_create(gsups, |
| 387 | /* no e1inp */ NULL, |
| 388 | ip_addr, tcp_port, |
| 389 | osmo_gsup_server_accept_cb, |
| 390 | gsups); |
| 391 | if (!gsups->link) |
| 392 | goto failed; |
| 393 | |
| 394 | gsups->read_cb = read_cb; |
Harald Welte | 32acace | 2018-06-16 17:07:28 +0200 | [diff] [blame] | 395 | gsups->priv = priv; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 396 | |
| 397 | rc = ipa_server_link_open(gsups->link); |
| 398 | if (rc < 0) |
| 399 | goto failed; |
| 400 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 401 | return gsups; |
| 402 | |
| 403 | failed: |
| 404 | osmo_gsup_server_destroy(gsups); |
| 405 | return NULL; |
| 406 | } |
| 407 | |
| 408 | void osmo_gsup_server_destroy(struct osmo_gsup_server *gsups) |
| 409 | { |
| 410 | if (gsups->link) { |
| 411 | ipa_server_link_close(gsups->link); |
| 412 | ipa_server_link_destroy(gsups->link); |
| 413 | gsups->link = NULL; |
| 414 | } |
| 415 | talloc_free(gsups); |
| 416 | } |
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 | /* Set GSUP message's pdp_infos[0] to a wildcard APN. |
| 419 | * 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] | 420 | * osmo_gsup_encode() is done. Return 0 if an entry was added, -ENOMEM if the provided buffer is too |
| 421 | * small. */ |
| 422 | int osmo_gsup_configure_wildcard_apn(struct osmo_gsup_message *gsup, |
| 423 | uint8_t *apn_buf, size_t apn_buf_size) |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 424 | { |
| 425 | int l; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 426 | |
Neels Hofmeyr | 5aeb438 | 2018-05-04 16:02:44 +0200 | [diff] [blame] | 427 | l = osmo_apn_from_str(apn_buf, apn_buf_size, "*"); |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 428 | if (l <= 0) |
Neels Hofmeyr | 1b8a1dc | 2018-05-04 16:46:47 +0200 | [diff] [blame] | 429 | return -ENOMEM; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 430 | |
Neels Hofmeyr | 5aeb438 | 2018-05-04 16:02:44 +0200 | [diff] [blame] | 431 | gsup->pdp_infos[0].apn_enc = apn_buf; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 432 | gsup->pdp_infos[0].apn_enc_len = l; |
| 433 | gsup->pdp_infos[0].have_info = 1; |
| 434 | gsup->num_pdp_infos = 1; |
| 435 | /* FIXME: use real value: */ |
| 436 | gsup->pdp_infos[0].context_id = 1; |
Neels Hofmeyr | 1b8a1dc | 2018-05-04 16:46:47 +0200 | [diff] [blame] | 437 | |
| 438 | return 0; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 439 | } |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 440 | |
| 441 | /** |
| 442 | * Populate a gsup message structure with an Insert Subscriber Data Message. |
Vadim Yanitskiy | fa20702 | 2020-05-31 03:52:52 +0700 | [diff] [blame] | 443 | * 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] | 444 | * must be allocated by the caller and should have the same lifetime as the gsup parameter. |
| 445 | * |
| 446 | * \param[out] gsup The gsup message to populate. |
| 447 | * \param[in] imsi The subscriber's IMSI. |
| 448 | * \param[in] msisdn The subscriber's MSISDN. |
| 449 | * \param[out] msisdn_enc A buffer large enough to store the MSISDN in encoded form. |
| 450 | * \param[in] msisdn_enc_size Size of the buffer (must be >= OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN). |
| 451 | * \param[out] apn_buf A buffer large enough to store an APN (required if cn_domain is OSMO_GSUP_CN_DOMAIN_PS). |
| 452 | * \param[in] apn_buf_size Size of APN buffer (must be >= APN_MAXLEN). |
| 453 | * \param[in] cn_domain The CN Domain of the subscriber connection. |
| 454 | * \returns 0 on success, and negative on error. |
| 455 | */ |
| 456 | int osmo_gsup_create_insert_subscriber_data_msg(struct osmo_gsup_message *gsup, const char *imsi, const char *msisdn, |
| 457 | uint8_t *msisdn_enc, size_t msisdn_enc_size, |
| 458 | uint8_t *apn_buf, size_t apn_buf_size, |
| 459 | enum osmo_gsup_cn_domain cn_domain) |
| 460 | { |
| 461 | int len; |
| 462 | |
| 463 | OSMO_ASSERT(gsup); |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 464 | *gsup = (struct osmo_gsup_message){ |
| 465 | .message_type = OSMO_GSUP_MSGT_INSERT_DATA_REQUEST, |
| 466 | }; |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 467 | |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 468 | osmo_strlcpy(gsup->imsi, imsi, sizeof(gsup->imsi)); |
| 469 | |
| 470 | if (msisdn_enc_size < OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN) |
| 471 | return -ENOSPC; |
| 472 | |
| 473 | OSMO_ASSERT(msisdn_enc); |
| 474 | len = gsm48_encode_bcd_number(msisdn_enc, msisdn_enc_size, 0, msisdn); |
| 475 | if (len < 1) { |
| 476 | LOGP(DLGSUP, LOGL_ERROR, "%s: Error: cannot encode MSISDN '%s'\n", imsi, msisdn); |
| 477 | return -ENOSPC; |
| 478 | } |
| 479 | gsup->msisdn_enc = msisdn_enc; |
| 480 | gsup->msisdn_enc_len = len; |
| 481 | |
| 482 | #pragma message "FIXME: deal with encoding the following data: gsup.hlr_enc" |
| 483 | |
| 484 | gsup->cn_domain = cn_domain; |
| 485 | if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) { |
| 486 | OSMO_ASSERT(apn_buf_size >= APN_MAXLEN); |
| 487 | OSMO_ASSERT(apn_buf); |
| 488 | /* FIXME: PDP infos - use more fine-grained access control |
| 489 | instead of wildcard APN */ |
| 490 | osmo_gsup_configure_wildcard_apn(gsup, apn_buf, apn_buf_size); |
| 491 | } |
| 492 | |
| 493 | return 0; |
| 494 | } |
Neels Hofmeyr | 76328bd | 2019-11-20 03:35:37 +0100 | [diff] [blame] | 495 | |
| 496 | int osmo_gsup_forward_to_local_peer(struct osmo_gsup_server *server, const struct osmo_cni_peer_id *to_peer, |
| 497 | struct osmo_gsup_req *req, struct osmo_gsup_message *modified_gsup) |
| 498 | { |
| 499 | int rc; |
| 500 | /* To forward to a remote entity (HLR, SMSC,...), we need to indicate the original source name in the Source |
| 501 | * Name IE to make sure the reply can be routed back. Store the sender in gsup->source_name -- the remote entity |
| 502 | * is required to return this as gsup->destination_name so that the reply gets routed to the original sender. */ |
| 503 | struct osmo_gsup_message forward = *(modified_gsup? : &req->gsup); |
| 504 | |
| 505 | if (req->source_name.type != OSMO_CNI_PEER_ID_IPA_NAME) { |
| 506 | osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "Unsupported GSUP peer id type: %s", |
| 507 | osmo_cni_peer_id_type_name(req->source_name.type)); |
| 508 | rc = -ENOTSUP; |
| 509 | goto routing_error; |
| 510 | } |
| 511 | forward.source_name = req->source_name.ipa_name.val; |
| 512 | forward.source_name_len = req->source_name.ipa_name.len; |
| 513 | |
| 514 | if (to_peer->type != OSMO_CNI_PEER_ID_IPA_NAME) { |
| 515 | osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "Unsupported GSUP peer id type: %s", |
| 516 | osmo_cni_peer_id_type_name(to_peer->type)); |
| 517 | rc = -ENOTSUP; |
| 518 | goto routing_error; |
| 519 | } |
| 520 | LOG_GSUP_REQ(req, LOGL_INFO, "Forwarding to %s\n", osmo_cni_peer_id_to_str(to_peer)); |
| 521 | rc = osmo_gsup_enc_send_to_ipa_name(server, &to_peer->ipa_name, &forward); |
| 522 | if (rc) |
| 523 | goto routing_error; |
| 524 | osmo_gsup_req_free(req); |
| 525 | return 0; |
| 526 | |
| 527 | routing_error: |
| 528 | osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true); |
| 529 | return rc; |
| 530 | } |