Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 1 | /* nacc_fsm.c |
| 2 | * |
| 3 | * Copyright (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 4 | * Author: Pau Espin Pedrol <pespin@sysmocom.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <talloc.h> |
| 24 | |
| 25 | #include <osmocom/core/rate_ctr.h> |
| 26 | #include <osmocom/ctrl/control_cmd.h> |
| 27 | #include <osmocom/ctrl/control_if.h> |
| 28 | |
| 29 | #include <osmocom/gsm/gsm48.h> |
| 30 | #include <osmocom/gprs/gprs_bssgp.h> |
| 31 | #include <osmocom/gprs/gprs_bssgp_rim.h> |
| 32 | |
| 33 | #include <nacc_fsm.h> |
| 34 | #include <gprs_rlcmac.h> |
| 35 | #include <gprs_debug.h> |
| 36 | #include <gprs_ms.h> |
| 37 | #include <encoding.h> |
| 38 | #include <bts.h> |
| 39 | #include <neigh_cache.h> |
| 40 | |
| 41 | #define X(s) (1 << (s)) |
| 42 | |
| 43 | #define nacc_fsm_state_chg(fi, NEXT_STATE) \ |
| 44 | osmo_fsm_inst_state_chg(fi, NEXT_STATE, 0, 0) |
| 45 | |
| 46 | const struct value_string nacc_fsm_event_names[] = { |
| 47 | { NACC_EV_RX_CELL_CHG_NOTIFICATION, "RX_CELL_CHG_NOTIFICATION" }, |
| 48 | { NACC_EV_RX_RAC_CI, "RX_RAC_CI" }, |
| 49 | { NACC_EV_RX_SI, "RX_SI" }, |
| 50 | { NACC_EV_CREATE_RLCMAC_MSG, "CREATE_RLCMAC_MSG" }, |
| 51 | { 0, NULL } |
| 52 | }; |
| 53 | |
| 54 | /* TS 44 060 11.2.9e Packet Neighbour Cell Data */ |
| 55 | static struct msgb *create_packet_neighbour_cell_data(struct nacc_fsm_ctx *ctx, |
| 56 | const struct gprs_rlcmac_tbf *tbf, |
| 57 | bool *all_si_info_sent) |
| 58 | { |
| 59 | struct msgb *msg; |
| 60 | int rc; |
| 61 | RlcMacDownlink_t *mac_control_block; |
| 62 | struct GprsMs *ms = tbf_ms(tbf); |
| 63 | OSMO_ASSERT(tbf_is_tfi_assigned(tbf)); |
| 64 | uint8_t tfi_is_dl = tbf_direction(tbf) == GPRS_RLCMAC_DL_TBF; |
| 65 | uint8_t tfi = tbf_tfi(tbf); |
| 66 | uint8_t container_id = 0; |
| 67 | PNCDContainer_t container; |
| 68 | size_t max_len, len_to_write; |
| 69 | uint8_t *cont_buf; |
| 70 | uint8_t si_type = ctx->si_info.type_psi ? 0x01 : 0x0; |
| 71 | |
| 72 | memset(&container, 0, sizeof(container)); |
| 73 | if (ctx->container_idx == 0) { |
| 74 | container.UnionType = 1; /* with ID */ |
| 75 | container.u.PNCD_Container_With_ID.ARFCN = ctx->neigh_key.tgt_arfcn; |
| 76 | container.u.PNCD_Container_With_ID.BSIC = ctx->neigh_key.tgt_bsic; |
| 77 | cont_buf = &container.u.PNCD_Container_With_ID.CONTAINER[0]; |
| 78 | max_len = sizeof(container.u.PNCD_Container_With_ID.CONTAINER) - 1; |
| 79 | } else { |
| 80 | container.UnionType = 0; /* without ID */ |
| 81 | cont_buf = &container.u.PNCD_Container_Without_ID.CONTAINER[0]; |
| 82 | max_len = sizeof(container.u.PNCD_Container_Without_ID.CONTAINER) - 1; |
| 83 | } |
| 84 | |
| 85 | len_to_write = ctx->si_info.si_len - ctx->si_info_bytes_sent; |
| 86 | |
| 87 | if (len_to_write == 0) { |
| 88 | /* We sent all info on last message filing it exactly, we now send a zeroed one to finish */ |
| 89 | *all_si_info_sent = true; |
| 90 | *cont_buf = (si_type << 5) | 0x00; |
| 91 | } else if (len_to_write >= max_len) { |
| 92 | /* We fill the rlcmac block, we'll need more messages */ |
| 93 | *all_si_info_sent = false; |
| 94 | *cont_buf = (si_type << 5) | 0x1F; |
| 95 | memcpy(cont_buf + 1, &ctx->si_info.si_buf[ctx->si_info_bytes_sent], max_len); |
| 96 | ctx->si_info_bytes_sent += max_len; |
| 97 | } else { |
| 98 | /* Last block, we don't fill it exactly */ |
| 99 | *all_si_info_sent = true; |
| 100 | *cont_buf = (si_type << 5) | (len_to_write & 0x1F); |
| 101 | memcpy(cont_buf + 1, &ctx->si_info.si_buf[ctx->si_info_bytes_sent], len_to_write); |
| 102 | ctx->si_info_bytes_sent += len_to_write; |
| 103 | } |
| 104 | |
| 105 | msg = msgb_alloc(GSM_MACBLOCK_LEN, "neighbour_cell_data"); |
| 106 | if (!msg) |
| 107 | return NULL; |
| 108 | |
| 109 | /* Initialize a bit vector that uses allocated msgb as the data buffer. */ |
| 110 | struct bitvec bv = { |
| 111 | .data = msgb_put(msg, GSM_MACBLOCK_LEN), |
| 112 | .data_len = GSM_MACBLOCK_LEN, |
| 113 | }; |
| 114 | bitvec_unhex(&bv, DUMMY_VEC); |
| 115 | |
| 116 | mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->ms, RlcMacDownlink_t); |
| 117 | |
| 118 | write_packet_neighbour_cell_data(mac_control_block, |
| 119 | tfi_is_dl, tfi, container_id, |
| 120 | ctx->container_idx, &container); |
| 121 | LOGP(DNACC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Neighbour Cell Data +++++++++++++++++++++++++\n"); |
| 122 | rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block); |
| 123 | if (rc < 0) { |
| 124 | LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Neighbour Cell Data failed (%d)\n", rc); |
| 125 | goto free_ret; |
| 126 | } |
| 127 | LOGP(DNACC, LOGL_DEBUG, "------------------------- TX : Packet Neighbour Cell Data -------------------------\n"); |
| 128 | rate_ctr_inc(&bts_rate_counters(ms->bts)->ctr[CTR_PKT_NEIGH_CELL_DATA]); |
| 129 | talloc_free(mac_control_block); |
| 130 | |
| 131 | ctx->container_idx++; |
| 132 | |
| 133 | return msg; |
| 134 | |
| 135 | free_ret: |
| 136 | talloc_free(mac_control_block); |
| 137 | msgb_free(msg); |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | /* TS 44 060 11.2.2a Packet Cell Change Continue */ |
| 142 | static struct msgb *create_packet_cell_chg_continue(const struct nacc_fsm_ctx *ctx, |
| 143 | const struct gprs_rlcmac_tbf *tbf) |
| 144 | { |
| 145 | struct msgb *msg; |
| 146 | int rc; |
| 147 | RlcMacDownlink_t *mac_control_block; |
| 148 | struct GprsMs *ms = tbf_ms(tbf); |
| 149 | |
| 150 | msg = msgb_alloc(GSM_MACBLOCK_LEN, "pkt_cell_chg_continue"); |
| 151 | if (!msg) |
| 152 | return NULL; |
| 153 | |
| 154 | /* Initialize a bit vector that uses allocated msgb as the data buffer. */ |
| 155 | struct bitvec bv = { |
| 156 | .data = msgb_put(msg, GSM_MACBLOCK_LEN), |
| 157 | .data_len = GSM_MACBLOCK_LEN, |
| 158 | }; |
| 159 | bitvec_unhex(&bv, DUMMY_VEC); |
| 160 | |
| 161 | mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->ms, RlcMacDownlink_t); |
| 162 | |
| 163 | OSMO_ASSERT(tbf_is_tfi_assigned(tbf)); |
| 164 | uint8_t tfi_is_dl = tbf_direction(tbf) == GPRS_RLCMAC_DL_TBF; |
| 165 | uint8_t tfi = tbf_tfi(tbf); |
| 166 | uint8_t container_id = 0; |
| 167 | write_packet_cell_change_continue(mac_control_block, tfi_is_dl, tfi, true, |
| 168 | ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic, container_id); |
| 169 | LOGP(DNACC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Cell Change Continue +++++++++++++++++++++++++\n"); |
| 170 | rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block); |
| 171 | if (rc < 0) { |
| 172 | LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Cell Change Continue failed (%d)\n", rc); |
| 173 | goto free_ret; |
| 174 | } |
| 175 | LOGP(DNACC, LOGL_DEBUG, "------------------------- TX : Packet Cell Change Continue -------------------------\n"); |
| 176 | rate_ctr_inc(&bts_rate_counters(ms->bts)->ctr[CTR_PKT_CELL_CHG_CONTINUE]); |
| 177 | talloc_free(mac_control_block); |
| 178 | return msg; |
| 179 | |
| 180 | free_ret: |
| 181 | talloc_free(mac_control_block); |
| 182 | msgb_free(msg); |
| 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | static int fill_rim_ran_info_req(const struct nacc_fsm_ctx *ctx, struct bssgp_ran_information_pdu *pdu) |
| 187 | { |
| 188 | struct gprs_rlcmac_bts *bts = ctx->ms->bts; |
| 189 | |
| 190 | *pdu = (struct bssgp_ran_information_pdu){ |
| 191 | .routing_info_dest = { |
| 192 | .discr = BSSGP_RIM_ROUTING_INFO_GERAN, |
| 193 | .geran = { |
| 194 | .raid = { |
| 195 | .mcc = ctx->cgi_ps.rai.lac.plmn.mcc, |
| 196 | .mnc = ctx->cgi_ps.rai.lac.plmn.mnc, |
| 197 | .mnc_3_digits = ctx->cgi_ps.rai.lac.plmn.mnc_3_digits, |
| 198 | .lac = ctx->cgi_ps.rai.lac.lac, |
| 199 | .rac = ctx->cgi_ps.rai.rac, |
| 200 | }, |
| 201 | .cid = ctx->cgi_ps.cell_identity, |
| 202 | }, |
| 203 | }, |
| 204 | .routing_info_src = { |
| 205 | .discr = BSSGP_RIM_ROUTING_INFO_GERAN, |
| 206 | .geran = { |
| 207 | .raid = { |
| 208 | .mcc = bts->cgi_ps.rai.lac.plmn.mcc, |
| 209 | .mnc = bts->cgi_ps.rai.lac.plmn.mnc, |
| 210 | .mnc_3_digits = bts->cgi_ps.rai.lac.plmn.mnc_3_digits, |
| 211 | .lac = bts->cgi_ps.rai.lac.lac, |
| 212 | .rac = bts->cgi_ps.rai.rac, |
| 213 | }, |
| 214 | .cid = bts->cgi_ps.cell_identity, |
| 215 | }, |
| 216 | }, |
| 217 | .rim_cont_iei = BSSGP_IE_RI_REQ_RIM_CONTAINER, |
| 218 | .decoded_present = true, |
| 219 | .decoded = { |
| 220 | .req_rim_cont = { |
| 221 | .app_id = BSSGP_RAN_INF_APP_ID_NACC, |
| 222 | .seq_num = 1, |
| 223 | .pdu_ind = { |
| 224 | .ack_requested = 0, |
| 225 | .pdu_type_ext = RIM_PDU_TYPE_SING_REP, |
| 226 | }, |
| 227 | .prot_ver = 1, |
| 228 | .son_trans_app_id = NULL, |
| 229 | .son_trans_app_id_len = 0, |
| 230 | .u = { |
| 231 | .app_cont_nacc = { |
| 232 | .reprt_cell = ctx->cgi_ps, |
| 233 | }, |
| 234 | }, |
| 235 | }, |
| 236 | }, |
| 237 | }; |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | //////////////// |
| 244 | // FSM states // |
| 245 | //////////////// |
| 246 | |
| 247 | static void st_initial(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 248 | { |
| 249 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
| 250 | struct gprs_rlcmac_bts *bts = ctx->ms->bts; |
| 251 | Packet_Cell_Change_Notification_t *notif; |
| 252 | |
| 253 | switch (event) { |
| 254 | case NACC_EV_RX_CELL_CHG_NOTIFICATION: |
| 255 | notif = (Packet_Cell_Change_Notification_t *)data; |
| 256 | switch (notif->Target_Cell.UnionType) { |
| 257 | case 0: /* GSM */ |
| 258 | ctx->neigh_key.local_lac = bts->cgi_ps.rai.lac.lac; |
| 259 | ctx->neigh_key.local_ci = bts->cgi_ps.cell_identity; |
| 260 | ctx->neigh_key.tgt_arfcn = notif->Target_Cell.u.Target_Cell_GSM_Notif.ARFCN; |
| 261 | ctx->neigh_key.tgt_bsic = notif->Target_Cell.u.Target_Cell_GSM_Notif.BSIC; |
| 262 | nacc_fsm_state_chg(fi, NACC_ST_WAIT_RESOLVE_RAC_CI); |
| 263 | break; |
| 264 | default: |
| 265 | LOGPFSML(fi, LOGL_NOTICE, "TargetCell type=0x%x not supported\n", |
| 266 | notif->Target_Cell.UnionType); |
| 267 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 268 | return; |
| 269 | } |
| 270 | break; |
| 271 | default: |
| 272 | OSMO_ASSERT(0); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | static void st_wait_resolve_rac_ci_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 277 | { |
| 278 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
| 279 | struct gprs_rlcmac_bts *bts = ctx->ms->bts; |
| 280 | struct gprs_pcu *pcu = bts->pcu; |
| 281 | const struct osmo_cell_global_id_ps *cgi_ps; |
Pau Espin Pedrol | c0805e6 | 2021-01-27 17:16:59 +0100 | [diff] [blame^] | 282 | struct ctrl_cmd *cmd = NULL; |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 283 | int rc; |
| 284 | |
| 285 | /* First try to find the value in the cache */ |
| 286 | cgi_ps = neigh_cache_lookup_value(pcu->neigh_cache, &ctx->neigh_key); |
| 287 | if (cgi_ps) { |
| 288 | ctx->cgi_ps = *cgi_ps; |
| 289 | nacc_fsm_state_chg(fi, NACC_ST_WAIT_REQUEST_SI); |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | /* CGI-PS not in cache, resolve it using BSC Neighbor Resolution CTRL interface */ |
| 294 | |
| 295 | LOGPFSML(fi, LOGL_DEBUG, "No CGI-PS found in cache, resolving " NEIGH_CACHE_ENTRY_KEY_FMT "...\n", |
| 296 | NEIGH_CACHE_ENTRY_KEY_ARGS(&ctx->neigh_key)); |
| 297 | |
Pau Espin Pedrol | c0805e6 | 2021-01-27 17:16:59 +0100 | [diff] [blame^] | 298 | rc = osmo_sock_init2_ofd(&ctx->neigh_ctrl_conn->write_queue.bfd, |
| 299 | AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, |
| 300 | NULL, 0, pcu->vty.neigh_ctrl_addr, pcu->vty.neigh_ctrl_port, |
| 301 | OSMO_SOCK_F_CONNECT); |
| 302 | if (rc < 0) { |
| 303 | LOGPFSML(fi, LOGL_ERROR, "Can't connect to CTRL @ %s:%u\n", |
| 304 | pcu->vty.neigh_ctrl_addr, pcu->vty.neigh_ctrl_port); |
| 305 | goto err_term; |
| 306 | } |
| 307 | |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 308 | cmd = ctrl_cmd_create(ctx, CTRL_TYPE_GET); |
| 309 | if (!cmd) { |
| 310 | LOGPFSML(fi, LOGL_ERROR, "CTRL msg creation failed\n"); |
| 311 | goto err_term; |
| 312 | } |
| 313 | |
| 314 | cmd->id = talloc_asprintf(cmd, "1"); |
| 315 | cmd->variable = talloc_asprintf(cmd, "neighbor_resolve_cgi_ps_from_lac_ci.%d.%d.%d.%d", |
| 316 | ctx->neigh_key.local_lac, ctx->neigh_key.local_ci, |
| 317 | ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic); |
| 318 | rc = ctrl_cmd_send(&ctx->neigh_ctrl_conn->write_queue, cmd); |
| 319 | if (rc) { |
| 320 | LOGPFSML(fi, LOGL_ERROR, "CTRL msg sent failed: %d\n", rc); |
| 321 | goto err_term; |
| 322 | } |
| 323 | |
| 324 | talloc_free(cmd); |
| 325 | return; |
| 326 | |
| 327 | err_term: |
| 328 | talloc_free(cmd); |
| 329 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 330 | } |
| 331 | |
| 332 | |
| 333 | static void st_wait_resolve_rac_ci(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 334 | { |
| 335 | switch (event) { |
| 336 | case NACC_EV_RX_CELL_CHG_NOTIFICATION: |
| 337 | break; |
| 338 | case NACC_EV_RX_RAC_CI: |
| 339 | /* Assumption: ctx->cgi_ps has been filled by caller of the event */ |
| 340 | nacc_fsm_state_chg(fi, NACC_ST_WAIT_REQUEST_SI); |
| 341 | break; |
| 342 | default: |
| 343 | OSMO_ASSERT(0); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /* At this point, we expect correct tgt cell info to be already in ctx->cgi_ps */ |
| 348 | static void st_wait_request_si_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 349 | { |
| 350 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
| 351 | struct gprs_rlcmac_bts *bts = ctx->ms->bts; |
| 352 | struct gprs_pcu *pcu = bts->pcu; |
| 353 | struct bssgp_ran_information_pdu pdu; |
| 354 | const struct si_cache_value *si; |
| 355 | int rc; |
| 356 | |
| 357 | /* First check if we have SI info for the target cell in cache */ |
| 358 | si = si_cache_lookup_value(pcu->si_cache, &ctx->cgi_ps); |
| 359 | if (si) { |
| 360 | /* Copy info since cache can be deleted at any point */ |
| 361 | memcpy(&ctx->si_info, si, sizeof(ctx->si_info)); |
| 362 | /* Tell the PCU scheduler we are ready to go, from here one we |
| 363 | * are polled/driven by the scheduler */ |
| 364 | nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA); |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | /* SI info not in cache, resolve it using RIM procedure against SGSN */ |
| 369 | if (fill_rim_ran_info_req(ctx, &pdu) < 0) { |
| 370 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | rc = bssgp_tx_rim(&pdu, gprs_ns2_nse_nsei(ctx->ms->bts->nse)); |
| 375 | if (rc < 0) { |
| 376 | LOGPFSML(fi, LOGL_ERROR, "Failed transmitting RIM PDU: %d\n", rc); |
| 377 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 378 | return; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | |
| 383 | static void st_wait_request_si(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 384 | { |
| 385 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
| 386 | struct si_cache_entry *entry; |
| 387 | |
| 388 | switch (event) { |
| 389 | case NACC_EV_RX_SI: |
| 390 | entry = (struct si_cache_entry *)data; |
| 391 | /* Copy info since cache can be deleted at any point */ |
| 392 | memcpy(&ctx->si_info, &entry->value, sizeof(ctx->si_info)); |
| 393 | /* Tell the PCU scheduler we are ready to go, from here one we |
| 394 | * are polled/driven by the scheduler */ |
| 395 | nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA); |
| 396 | break; |
| 397 | default: |
| 398 | OSMO_ASSERT(0); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | /* st_tx_neighbour_data_on_enter: |
| 403 | * At this point, we already received all required SI information to send stored |
| 404 | * in struct nacc_fsm_ctx. We now wait for scheduler to ask us to construct |
| 405 | * RLCMAC DL CTRL messages to move FSM states forward |
| 406 | */ |
| 407 | |
| 408 | static void st_tx_neighbour_data(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 409 | { |
| 410 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
| 411 | struct nacc_ev_create_rlcmac_msg_ctx *data_ctx; |
| 412 | bool all_si_info_sent; |
| 413 | |
| 414 | switch (event) { |
| 415 | case NACC_EV_CREATE_RLCMAC_MSG: |
| 416 | data_ctx = (struct nacc_ev_create_rlcmac_msg_ctx *)data; |
| 417 | data_ctx->msg = create_packet_neighbour_cell_data(ctx, data_ctx->tbf, &all_si_info_sent); |
| 418 | if (!data_ctx->msg) { |
| 419 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 420 | return; |
| 421 | } |
| 422 | if (all_si_info_sent) /* DONE */ |
| 423 | nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE); |
| 424 | break; |
| 425 | default: |
| 426 | OSMO_ASSERT(0); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /* st_cell_cgh_continue_on_enter: |
| 431 | * At this point, we already sent all Pkt Cell Neighbour Change rlcmac |
| 432 | * blocks, and we only need to wait to be scheduled again to send PKT |
| 433 | * CELL CHANGE NOTIFICATION and then we are done |
| 434 | */ |
| 435 | |
| 436 | static void st_cell_cgh_continue(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 437 | { |
| 438 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
| 439 | struct nacc_ev_create_rlcmac_msg_ctx *data_ctx; |
| 440 | |
| 441 | switch (event) { |
| 442 | case NACC_EV_CREATE_RLCMAC_MSG: |
| 443 | data_ctx = (struct nacc_ev_create_rlcmac_msg_ctx *)data; |
| 444 | data_ctx->msg = create_packet_cell_chg_continue(ctx, data_ctx->tbf); |
| 445 | nacc_fsm_state_chg(fi, NACC_ST_DONE); |
| 446 | break; |
| 447 | default: |
| 448 | OSMO_ASSERT(0); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | |
| 453 | static void st_done_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 454 | { |
| 455 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL); |
| 456 | } |
| 457 | |
| 458 | static void nacc_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause) |
| 459 | { |
| 460 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
| 461 | /* after cleanup() finishes, FSM termination calls osmo_fsm_inst_free, |
| 462 | so we need to avoid double-freeing it during ctx talloc free |
| 463 | destructor */ |
| 464 | talloc_reparent(ctx, ctx->ms, ctx->fi); |
| 465 | ctx->fi = NULL; |
| 466 | |
| 467 | /* remove references from owning MS and free entire ctx */ |
| 468 | ctx->ms->nacc = NULL; |
| 469 | talloc_free(ctx); |
| 470 | } |
| 471 | |
| 472 | static struct osmo_fsm_state nacc_fsm_states[] = { |
| 473 | [NACC_ST_INITIAL] = { |
| 474 | .in_event_mask = |
| 475 | X(NACC_EV_RX_CELL_CHG_NOTIFICATION), |
| 476 | .out_state_mask = |
| 477 | X(NACC_ST_WAIT_RESOLVE_RAC_CI), |
| 478 | .name = "INITIAL", |
| 479 | .action = st_initial, |
| 480 | }, |
| 481 | [NACC_ST_WAIT_RESOLVE_RAC_CI] = { |
| 482 | .in_event_mask = |
| 483 | X(NACC_EV_RX_RAC_CI), |
| 484 | .out_state_mask = |
| 485 | X(NACC_ST_WAIT_REQUEST_SI), |
| 486 | .name = "WAIT_RESOLVE_RAC_CI", |
| 487 | .onenter = st_wait_resolve_rac_ci_on_enter, |
| 488 | .action = st_wait_resolve_rac_ci, |
| 489 | }, |
| 490 | [NACC_ST_WAIT_REQUEST_SI] = { |
| 491 | .in_event_mask = |
| 492 | X(NACC_EV_RX_CELL_CHG_NOTIFICATION) | |
| 493 | X(NACC_EV_RX_SI), |
| 494 | .out_state_mask = |
| 495 | X(NACC_ST_TX_NEIGHBOUR_DATA), |
| 496 | .name = "WAIT_REQUEST_SI", |
| 497 | .onenter = st_wait_request_si_on_enter, |
| 498 | .action = st_wait_request_si, |
| 499 | }, |
| 500 | [NACC_ST_TX_NEIGHBOUR_DATA] = { |
| 501 | .in_event_mask = |
| 502 | X(NACC_EV_RX_CELL_CHG_NOTIFICATION) | |
| 503 | X(NACC_EV_RX_SI) | |
| 504 | X(NACC_EV_CREATE_RLCMAC_MSG), |
| 505 | .out_state_mask = |
| 506 | X(NACC_ST_TX_CELL_CHG_CONTINUE), |
| 507 | .name = "TX_NEIGHBOUR_DATA", |
| 508 | .action = st_tx_neighbour_data, |
| 509 | }, |
| 510 | [NACC_ST_TX_CELL_CHG_CONTINUE] = { |
| 511 | .in_event_mask = |
| 512 | X(NACC_EV_RX_CELL_CHG_NOTIFICATION) | |
| 513 | X(NACC_EV_RX_SI) | |
| 514 | X(NACC_EV_CREATE_RLCMAC_MSG), |
| 515 | .out_state_mask = |
| 516 | X(NACC_ST_DONE), |
| 517 | .name = "TX_CELL_CHG_CONTINUE", |
| 518 | .action = st_cell_cgh_continue, |
| 519 | }, |
| 520 | [NACC_ST_DONE] = { |
| 521 | .in_event_mask = 0, |
| 522 | .out_state_mask = 0, |
| 523 | .name = "DONE", |
| 524 | .onenter = st_done_on_enter, |
| 525 | }, |
| 526 | }; |
| 527 | |
| 528 | static struct osmo_fsm nacc_fsm = { |
| 529 | .name = "NACC", |
| 530 | .states = nacc_fsm_states, |
| 531 | .num_states = ARRAY_SIZE(nacc_fsm_states), |
| 532 | .cleanup = nacc_fsm_cleanup, |
| 533 | .log_subsys = DNACC, |
| 534 | .event_names = nacc_fsm_event_names, |
| 535 | }; |
| 536 | |
| 537 | static __attribute__((constructor)) void nacc_fsm_init(void) |
| 538 | { |
| 539 | OSMO_ASSERT(osmo_fsm_register(&nacc_fsm) == 0); |
| 540 | } |
| 541 | |
| 542 | void nacc_fsm_ctrl_reply_cb(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd, void *data) |
| 543 | { |
| 544 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)data; |
| 545 | char *tmp = NULL, *tok, *saveptr; |
| 546 | |
| 547 | LOGPFSML(ctx->fi, LOGL_NOTICE, "Received CTRL message: type=%d %s: %s\n", |
| 548 | cmd->type, cmd->variable, osmo_escape_str(cmd->reply, -1)); |
| 549 | |
| 550 | if (cmd->type != CTRL_TYPE_GET_REPLY || !cmd->reply) { |
| 551 | osmo_fsm_inst_term(ctx->fi, OSMO_FSM_TERM_ERROR, NULL); |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | /* TODO: Potentially validate cmd->variable contains same params as we |
| 556 | sent, and that cmd->id matches the original set. We may want to keep |
| 557 | the original cmd around by setting cmd->defer=1 when sending it. */ |
| 558 | |
| 559 | tmp = talloc_strdup(cmd, cmd->reply); |
| 560 | if (!tmp) |
| 561 | goto free_ret; |
| 562 | |
| 563 | if (!(tok = strtok_r(tmp, "-", &saveptr))) |
| 564 | goto free_ret; |
| 565 | ctx->cgi_ps.rai.lac.plmn.mcc = atoi(tok); |
| 566 | |
| 567 | if (!(tok = strtok_r(NULL, "-", &saveptr))) |
| 568 | goto free_ret; |
| 569 | ctx->cgi_ps.rai.lac.plmn.mnc = atoi(tok); |
| 570 | |
| 571 | if (!(tok = strtok_r(NULL, "-", &saveptr))) |
| 572 | goto free_ret; |
| 573 | ctx->cgi_ps.rai.lac.lac = atoi(tok); |
| 574 | |
| 575 | if (!(tok = strtok_r(NULL, "-", &saveptr))) |
| 576 | goto free_ret; |
| 577 | ctx->cgi_ps.rai.rac = atoi(tok); |
| 578 | |
| 579 | if (!(tok = strtok_r(NULL, "\0", &saveptr))) |
| 580 | goto free_ret; |
| 581 | ctx->cgi_ps.cell_identity = atoi(tok); |
| 582 | |
| 583 | /* Cache the cgi_ps so we can avoid requesting again same resolution for a while */ |
| 584 | neigh_cache_add(ctx->ms->bts->pcu->neigh_cache, &ctx->neigh_key, &ctx->cgi_ps); |
| 585 | |
| 586 | osmo_fsm_inst_dispatch(ctx->fi, NACC_EV_RX_RAC_CI, NULL); |
| 587 | return; |
| 588 | |
| 589 | free_ret: |
| 590 | talloc_free(tmp); |
| 591 | osmo_fsm_inst_term(ctx->fi, OSMO_FSM_TERM_ERROR, NULL); |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | static int nacc_fsm_ctx_talloc_destructor(struct nacc_fsm_ctx *ctx) |
| 596 | { |
| 597 | if (ctx->fi) { |
| 598 | osmo_fsm_inst_free(ctx->fi); |
| 599 | ctx->fi = NULL; |
| 600 | } |
| 601 | |
| 602 | if (ctx->neigh_ctrl_conn) { |
| 603 | if (ctx->neigh_ctrl_conn->write_queue.bfd.fd != -1) { |
| 604 | osmo_wqueue_clear(&ctx->neigh_ctrl_conn->write_queue); |
| 605 | osmo_fd_unregister(&ctx->neigh_ctrl_conn->write_queue.bfd); |
| 606 | close(ctx->neigh_ctrl_conn->write_queue.bfd.fd); |
| 607 | ctx->neigh_ctrl_conn->write_queue.bfd.fd = -1; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | struct nacc_fsm_ctx *nacc_fsm_alloc(struct GprsMs* ms) |
| 615 | { |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 616 | struct nacc_fsm_ctx *ctx = talloc_zero(ms, struct nacc_fsm_ctx); |
| 617 | char buf[64]; |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 618 | |
| 619 | talloc_set_destructor(ctx, nacc_fsm_ctx_talloc_destructor); |
| 620 | |
| 621 | ctx->ms = ms; |
| 622 | |
| 623 | snprintf(buf, sizeof(buf), "TLLI-0x%08x", ms_tlli(ms)); |
| 624 | ctx->fi = osmo_fsm_inst_alloc(&nacc_fsm, ctx, ctx, LOGL_INFO, buf); |
| 625 | if (!ctx->fi) |
| 626 | goto free_ret; |
| 627 | |
| 628 | ctx->neigh_ctrl = ctrl_handle_alloc(ctx, ctx, NULL); |
| 629 | ctx->neigh_ctrl->reply_cb = nacc_fsm_ctrl_reply_cb; |
| 630 | ctx->neigh_ctrl_conn = osmo_ctrl_conn_alloc(ctx, ctx->neigh_ctrl); |
| 631 | if (!ctx->neigh_ctrl_conn) |
| 632 | goto free_ret; |
Pau Espin Pedrol | 202a478 | 2021-01-27 17:05:12 +0100 | [diff] [blame] | 633 | /* Older versions of osmo_ctrl_conn_alloc didn't properly initialize fd to -1, |
| 634 | * so make sure to do it here otherwise fd may be valid fd 0 and cause trouble */ |
| 635 | ctx->neigh_ctrl_conn->write_queue.bfd.fd = -1; |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 636 | llist_add(&ctx->neigh_ctrl_conn->list_entry, &ctx->neigh_ctrl->ccon_list); |
| 637 | |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 638 | return ctx; |
| 639 | free_ret: |
| 640 | talloc_free(ctx); |
| 641 | return NULL; |
| 642 | } |