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