blob: 0719d2ca1d4d4ea717be3ef6bac944681e9f589e [file] [log] [blame]
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +01001/* 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.
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010015 */
16
17#include <unistd.h>
18
19#include <talloc.h>
20
21#include <osmocom/core/rate_ctr.h>
22#include <osmocom/ctrl/control_cmd.h>
23#include <osmocom/ctrl/control_if.h>
24
25#include <osmocom/gsm/gsm48.h>
26#include <osmocom/gprs/gprs_bssgp.h>
27#include <osmocom/gprs/gprs_bssgp_rim.h>
28
29#include <nacc_fsm.h>
30#include <gprs_rlcmac.h>
31#include <gprs_debug.h>
32#include <gprs_ms.h>
33#include <encoding.h>
34#include <bts.h>
35#include <neigh_cache.h>
36
37#define X(s) (1 << (s))
38
Pau Espin Pedrol069a6372021-02-10 17:33:13 +010039/* Infer CTRL id (seqnum) for a given tgt arfcn+bsic (bsic range: 0-63) */
40#define arfcn_bsic_2_ctrl_id(arfcn, bsic) ((arfcn) * 100 + (bsic))
41
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +010042static const struct osmo_tdef_state_timeout nacc_fsm_timeouts[32] = {
43 [NACC_ST_INITIAL] = {},
44 [NACC_ST_WAIT_RESOLVE_RAC_CI] = { .T = PCU_TDEF_NEIGH_RESOLVE_TO },
45 [NACC_ST_WAIT_REQUEST_SI] = { .T = PCU_TDEF_SI_RESOLVE_TO },
46 [NACC_ST_TX_NEIGHBOUR_DATA] = {},
47 [NACC_ST_TX_CELL_CHG_CONTINUE] = {},
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +010048 [NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK] = {}, /* Timeout through event controlled by tbf::poll_timeout() */
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +010049 [NACC_ST_DONE] = {},
50};
51
Pau Espin Pedrol628d8812021-07-29 13:46:34 +020052/* Transition to a state, using the T timer defined in nacc_fsm_timeouts.
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +010053 * The actual timeout value is in turn obtained from conn->T_defs.
54 * Assumes local variable fi exists. */
55
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010056#define nacc_fsm_state_chg(fi, NEXT_STATE) \
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +010057 osmo_tdef_fsm_inst_state_chg(fi, NEXT_STATE, \
58 nacc_fsm_timeouts, \
59 ((struct nacc_fsm_ctx*)(fi->priv))->ms->bts->pcu->T_defs, \
60 -1)
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010061
62const struct value_string nacc_fsm_event_names[] = {
63 { NACC_EV_RX_CELL_CHG_NOTIFICATION, "RX_CELL_CHG_NOTIFICATION" },
64 { NACC_EV_RX_RAC_CI, "RX_RAC_CI" },
65 { NACC_EV_RX_SI, "RX_SI" },
66 { NACC_EV_CREATE_RLCMAC_MSG, "CREATE_RLCMAC_MSG" },
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +010067 { NACC_EV_RX_CELL_CHG_CONTINUE_ACK, "RX_CELL_CHG_CONTINUE_ACK"},
68 { NACC_EV_TIMEOUT_CELL_CHG_CONTINUE, "TIMEOUT_CELL_CHG_CONTINUE" },
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010069 { 0, NULL }
70};
71
72/* TS 44 060 11.2.9e Packet Neighbour Cell Data */
73static struct msgb *create_packet_neighbour_cell_data(struct nacc_fsm_ctx *ctx,
74 const struct gprs_rlcmac_tbf *tbf,
75 bool *all_si_info_sent)
76{
77 struct msgb *msg;
78 int rc;
79 RlcMacDownlink_t *mac_control_block;
80 struct GprsMs *ms = tbf_ms(tbf);
81 OSMO_ASSERT(tbf_is_tfi_assigned(tbf));
82 uint8_t tfi_is_dl = tbf_direction(tbf) == GPRS_RLCMAC_DL_TBF;
83 uint8_t tfi = tbf_tfi(tbf);
84 uint8_t container_id = 0;
85 PNCDContainer_t container;
86 size_t max_len, len_to_write;
87 uint8_t *cont_buf;
88 uint8_t si_type = ctx->si_info.type_psi ? 0x01 : 0x0;
89
90 memset(&container, 0, sizeof(container));
91 if (ctx->container_idx == 0) {
92 container.UnionType = 1; /* with ID */
93 container.u.PNCD_Container_With_ID.ARFCN = ctx->neigh_key.tgt_arfcn;
94 container.u.PNCD_Container_With_ID.BSIC = ctx->neigh_key.tgt_bsic;
95 cont_buf = &container.u.PNCD_Container_With_ID.CONTAINER[0];
96 max_len = sizeof(container.u.PNCD_Container_With_ID.CONTAINER) - 1;
97 } else {
98 container.UnionType = 0; /* without ID */
99 cont_buf = &container.u.PNCD_Container_Without_ID.CONTAINER[0];
100 max_len = sizeof(container.u.PNCD_Container_Without_ID.CONTAINER) - 1;
101 }
102
103 len_to_write = ctx->si_info.si_len - ctx->si_info_bytes_sent;
104
105 if (len_to_write == 0) {
106 /* We sent all info on last message filing it exactly, we now send a zeroed one to finish */
107 *all_si_info_sent = true;
108 *cont_buf = (si_type << 5) | 0x00;
109 } else if (len_to_write >= max_len) {
110 /* We fill the rlcmac block, we'll need more messages */
111 *all_si_info_sent = false;
112 *cont_buf = (si_type << 5) | 0x1F;
113 memcpy(cont_buf + 1, &ctx->si_info.si_buf[ctx->si_info_bytes_sent], max_len);
114 ctx->si_info_bytes_sent += max_len;
115 } else {
116 /* Last block, we don't fill it exactly */
117 *all_si_info_sent = true;
118 *cont_buf = (si_type << 5) | (len_to_write & 0x1F);
119 memcpy(cont_buf + 1, &ctx->si_info.si_buf[ctx->si_info_bytes_sent], len_to_write);
120 ctx->si_info_bytes_sent += len_to_write;
121 }
122
123 msg = msgb_alloc(GSM_MACBLOCK_LEN, "neighbour_cell_data");
124 if (!msg)
125 return NULL;
126
127 /* Initialize a bit vector that uses allocated msgb as the data buffer. */
128 struct bitvec bv = {
129 .data = msgb_put(msg, GSM_MACBLOCK_LEN),
130 .data_len = GSM_MACBLOCK_LEN,
131 };
132 bitvec_unhex(&bv, DUMMY_VEC);
133
134 mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->ms, RlcMacDownlink_t);
135
136 write_packet_neighbour_cell_data(mac_control_block,
137 tfi_is_dl, tfi, container_id,
138 ctx->container_idx, &container);
139 LOGP(DNACC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Neighbour Cell Data +++++++++++++++++++++++++\n");
140 rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block);
141 if (rc < 0) {
142 LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Neighbour Cell Data failed (%d)\n", rc);
143 goto free_ret;
144 }
145 LOGP(DNACC, LOGL_DEBUG, "------------------------- TX : Packet Neighbour Cell Data -------------------------\n");
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200146 rate_ctr_inc(rate_ctr_group_get_ctr(bts_rate_counters(ms->bts), CTR_PKT_NEIGH_CELL_DATA));
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100147 talloc_free(mac_control_block);
148
149 ctx->container_idx++;
150
151 return msg;
152
153free_ret:
154 talloc_free(mac_control_block);
155 msgb_free(msg);
156 return NULL;
157}
158
159/* TS 44 060 11.2.2a Packet Cell Change Continue */
160static struct msgb *create_packet_cell_chg_continue(const struct nacc_fsm_ctx *ctx,
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100161 const struct nacc_ev_create_rlcmac_msg_ctx *data,
162 uint32_t *new_poll_fn)
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100163{
164 struct msgb *msg;
165 int rc;
166 RlcMacDownlink_t *mac_control_block;
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100167 struct gprs_rlcmac_tbf *tbf = data->tbf;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100168 struct GprsMs *ms = tbf_ms(tbf);
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100169 unsigned int rrbp;
170
Pau Espin Pedrolfd74e792022-12-15 18:23:12 +0100171 rc = tbf_check_polling(tbf, data->pdch, data->fn, new_poll_fn, &rrbp);
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100172 if (rc < 0) {
173 LOGP(DTBF, LOGL_ERROR, "Failed registering poll for Pkt Cell Chg Continue (%d)\n", rc);
174 return NULL;
175 }
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100176
177 msg = msgb_alloc(GSM_MACBLOCK_LEN, "pkt_cell_chg_continue");
178 if (!msg)
179 return NULL;
180
181 /* Initialize a bit vector that uses allocated msgb as the data buffer. */
182 struct bitvec bv = {
183 .data = msgb_put(msg, GSM_MACBLOCK_LEN),
184 .data_len = GSM_MACBLOCK_LEN,
185 };
186 bitvec_unhex(&bv, DUMMY_VEC);
187
188 mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->ms, RlcMacDownlink_t);
189
190 OSMO_ASSERT(tbf_is_tfi_assigned(tbf));
191 uint8_t tfi_is_dl = tbf_direction(tbf) == GPRS_RLCMAC_DL_TBF;
192 uint8_t tfi = tbf_tfi(tbf);
193 uint8_t container_id = 0;
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100194 write_packet_cell_change_continue(mac_control_block, 1, rrbp, tfi_is_dl, tfi, true,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100195 ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic, container_id);
196 LOGP(DNACC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Cell Change Continue +++++++++++++++++++++++++\n");
197 rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block);
198 if (rc < 0) {
199 LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Cell Change Continue failed (%d)\n", rc);
200 goto free_ret;
201 }
202 LOGP(DNACC, LOGL_DEBUG, "------------------------- TX : Packet Cell Change Continue -------------------------\n");
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200203 rate_ctr_inc(rate_ctr_group_get_ctr(bts_rate_counters(ms->bts), CTR_PKT_CELL_CHG_CONTINUE));
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100204 talloc_free(mac_control_block);
Pau Espin Pedrol5b7eeec2022-12-15 19:08:13 +0100205 tbf_set_polling(tbf, data->pdch, *new_poll_fn, PDCH_ULC_POLL_CELL_CHG_CONTINUE);
Pau Espin Pedrolbd1f01f2022-10-27 15:19:39 +0200206 LOGPTBF(tbf, LOGL_DEBUG,
207 "Scheduled 'Packet Cell Change Continue' polling on PACCH (FN=%d, TS=%d)\n",
Pau Espin Pedrol5ba3ef92022-12-12 18:02:25 +0100208 *new_poll_fn, data->pdch->ts_no);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100209 return msg;
210
211free_ret:
212 talloc_free(mac_control_block);
213 msgb_free(msg);
214 return NULL;
215}
216
217static int fill_rim_ran_info_req(const struct nacc_fsm_ctx *ctx, struct bssgp_ran_information_pdu *pdu)
218{
219 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
220
221 *pdu = (struct bssgp_ran_information_pdu){
222 .routing_info_dest = {
223 .discr = BSSGP_RIM_ROUTING_INFO_GERAN,
224 .geran = {
225 .raid = {
226 .mcc = ctx->cgi_ps.rai.lac.plmn.mcc,
227 .mnc = ctx->cgi_ps.rai.lac.plmn.mnc,
228 .mnc_3_digits = ctx->cgi_ps.rai.lac.plmn.mnc_3_digits,
229 .lac = ctx->cgi_ps.rai.lac.lac,
230 .rac = ctx->cgi_ps.rai.rac,
231 },
232 .cid = ctx->cgi_ps.cell_identity,
233 },
234 },
235 .routing_info_src = {
236 .discr = BSSGP_RIM_ROUTING_INFO_GERAN,
237 .geran = {
238 .raid = {
239 .mcc = bts->cgi_ps.rai.lac.plmn.mcc,
240 .mnc = bts->cgi_ps.rai.lac.plmn.mnc,
241 .mnc_3_digits = bts->cgi_ps.rai.lac.plmn.mnc_3_digits,
242 .lac = bts->cgi_ps.rai.lac.lac,
243 .rac = bts->cgi_ps.rai.rac,
244 },
245 .cid = bts->cgi_ps.cell_identity,
246 },
247 },
248 .rim_cont_iei = BSSGP_IE_RI_REQ_RIM_CONTAINER,
249 .decoded_present = true,
250 .decoded = {
251 .req_rim_cont = {
252 .app_id = BSSGP_RAN_INF_APP_ID_NACC,
253 .seq_num = 1,
254 .pdu_ind = {
255 .ack_requested = 0,
256 .pdu_type_ext = RIM_PDU_TYPE_SING_REP,
257 },
258 .prot_ver = 1,
259 .son_trans_app_id = NULL,
260 .son_trans_app_id_len = 0,
261 .u = {
262 .app_cont_nacc = {
263 .reprt_cell = ctx->cgi_ps,
264 },
265 },
266 },
267 },
268 };
269
270 return 0;
271}
272
Pau Espin Pedrol0c10b3c2021-02-10 17:12:02 +0100273static int fill_neigh_key_from_bts_pkt_cell_chg_not(struct neigh_cache_entry_key *neigh_key,
274 const struct gprs_rlcmac_bts *bts,
275 const Packet_Cell_Change_Notification_t *notif)
276{
277 switch (notif->Target_Cell.UnionType) {
278 case 0: /* GSM */
279 neigh_key->local_lac = bts->cgi_ps.rai.lac.lac;
280 neigh_key->local_ci = bts->cgi_ps.cell_identity;
281 neigh_key->tgt_arfcn = notif->Target_Cell.u.Target_Cell_GSM_Notif.ARFCN;
282 neigh_key->tgt_bsic = notif->Target_Cell.u.Target_Cell_GSM_Notif.BSIC;
283 return 0;
284 default:
285 return -ENOTSUP;
286 }
287}
288
Pau Espin Pedrol44768f22021-02-02 13:11:30 +0100289#define SI_HDR_LEN 2
290static void bts_fill_si_cache_value(const struct gprs_rlcmac_bts *bts, struct si_cache_value *val)
291{
292 val->type_psi = false;
293 val->si_len = 0;
294 if (bts->si1_is_set) {
295 osmo_static_assert(sizeof(bts->si1) - SI_HDR_LEN == BSSGP_RIM_SI_LEN, _si1_header_size);
296 memcpy(&val->si_buf[val->si_len], bts->si1 + SI_HDR_LEN, BSSGP_RIM_SI_LEN);
297 val->si_len += BSSGP_RIM_SI_LEN;
298 }
299 if (bts->si3_is_set) {
300 osmo_static_assert(sizeof(bts->si3) - SI_HDR_LEN == BSSGP_RIM_SI_LEN, _si3_header_size);
301 memcpy(&val->si_buf[val->si_len], bts->si3 + SI_HDR_LEN, BSSGP_RIM_SI_LEN);
302 val->si_len += BSSGP_RIM_SI_LEN;
303 }
304 if (bts->si13_is_set) {
305 osmo_static_assert(sizeof(bts->si13) - SI_HDR_LEN == BSSGP_RIM_SI_LEN, _si13_header_size);
306 memcpy(&val->si_buf[val->si_len], bts->si13 + SI_HDR_LEN, BSSGP_RIM_SI_LEN);
307 val->si_len += BSSGP_RIM_SI_LEN;
308 }
309}
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100310
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100311/* Called on event NACC_EV_RX_CELL_CHG_NOTIFICATION on states after
312 * WAIT_RESOLVE_RAC_CI. Ignore duplicate messages, transition back if target
313 * cell changed.
314 */
315static void handle_retrans_pkt_cell_chg_notif(struct nacc_fsm_ctx *ctx, const Packet_Cell_Change_Notification_t *notif)
316{
317 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
318 struct neigh_cache_entry_key neigh_key;
319
320 if (fill_neigh_key_from_bts_pkt_cell_chg_not(&neigh_key, bts, notif) < 0) {
321 LOGPFSML(ctx->fi, LOGL_NOTICE, "TargetCell type=0x%x not supported\n",
322 notif->Target_Cell.UnionType);
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100323 if (ctx->fi->state != NACC_ST_TX_CELL_CHG_CONTINUE)
324 nacc_fsm_state_chg(ctx->fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100325 return;
326 }
327 /* If tgt cell changed, restart resolving it */
328 if (!neigh_cache_entry_key_eq(&ctx->neigh_key, &neigh_key)) {
329 ctx->neigh_key = neigh_key;
330 nacc_fsm_state_chg(ctx->fi, NACC_ST_WAIT_RESOLVE_RAC_CI);
331 }
332 /* else: ignore it, it's a dup, carry on what we were doing */
333}
334
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100335////////////////
336// FSM states //
337////////////////
338
339static void st_initial(struct osmo_fsm_inst *fi, uint32_t event, void *data)
340{
341 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
342 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
343 Packet_Cell_Change_Notification_t *notif;
344
345 switch (event) {
346 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
347 notif = (Packet_Cell_Change_Notification_t *)data;
Pau Espin Pedrol0c10b3c2021-02-10 17:12:02 +0100348 if (fill_neigh_key_from_bts_pkt_cell_chg_not(&ctx->neigh_key, bts, notif) < 0) {
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100349 LOGPFSML(fi, LOGL_NOTICE, "TargetCell type=0x%x not supported\n",
350 notif->Target_Cell.UnionType);
351 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
Pau Espin Pedrol0c10b3c2021-02-10 17:12:02 +0100352 } else {
353 nacc_fsm_state_chg(fi, NACC_ST_WAIT_RESOLVE_RAC_CI);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100354 }
355 break;
356 default:
357 OSMO_ASSERT(0);
358 }
359}
360
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200361static int send_neigh_addr_req_ctrl_iface(struct nacc_fsm_ctx *ctx)
362{
363 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
364 struct gprs_pcu *pcu = bts->pcu;
365 struct ctrl_cmd *cmd = NULL;
366 int rc;
367
368 /* We may have changed to this state previously (eg: we are handling
369 * another Pkt cell Change Notify with different target). Avoid
370 * re-creating the socket in that case. */
371 if (ctx->neigh_ctrl_conn->write_queue.bfd.fd == -1) {
372 rc = osmo_sock_init2_ofd(&ctx->neigh_ctrl_conn->write_queue.bfd,
373 AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP,
374 NULL, 0, pcu->vty.neigh_ctrl_addr, pcu->vty.neigh_ctrl_port,
375 OSMO_SOCK_F_CONNECT);
376 if (rc < 0) {
377 LOGPFSML(ctx->fi, LOGL_ERROR,
378 "Failed to establish CTRL (neighbor resolution) connection to BSC r=%s:%u\n\n",
379 pcu->vty.neigh_ctrl_addr, pcu->vty.neigh_ctrl_port);
380 goto err_term;
381 }
382 }
383
384 cmd = ctrl_cmd_create(ctx, CTRL_TYPE_GET);
385 if (!cmd) {
386 LOGPFSML(ctx->fi, LOGL_ERROR, "CTRL msg creation failed\n");
387 goto err_term;
388 }
389
390 cmd->id = talloc_asprintf(cmd, "%u", arfcn_bsic_2_ctrl_id(ctx->neigh_key.tgt_arfcn,
391 ctx->neigh_key.tgt_bsic));
392 cmd->variable = talloc_asprintf(cmd, "neighbor_resolve_cgi_ps_from_lac_ci.%d.%d.%d.%d",
393 ctx->neigh_key.local_lac, ctx->neigh_key.local_ci,
394 ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic);
395 rc = ctrl_cmd_send(&ctx->neigh_ctrl_conn->write_queue, cmd);
396 if (rc) {
397 LOGPFSML(ctx->fi, LOGL_ERROR, "CTRL msg sent failed: %d\n", rc);
398 goto err_term;
399 }
400
401 talloc_free(cmd);
402 return 0;
403
404err_term:
405 talloc_free(cmd);
406 return -1;
407}
408
409static int send_neigh_addr_req(struct nacc_fsm_ctx *ctx)
410{
411 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
412
413 /* If PCU was configured to use the old CTRL interface, use it: */
414 if (ctx->neigh_ctrl_conn)
415 return send_neigh_addr_req_ctrl_iface(ctx);
416
417 /* Otherwise, by default the new PCUIF over IPA Abis multiplex proto should be used: */
418 return pcu_tx_neigh_addr_res_req(bts, &ctx->neigh_key);
419}
420
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100421static void st_wait_resolve_rac_ci_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
422{
423 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
424 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
425 struct gprs_pcu *pcu = bts->pcu;
426 const struct osmo_cell_global_id_ps *cgi_ps;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100427
428 /* First try to find the value in the cache */
429 cgi_ps = neigh_cache_lookup_value(pcu->neigh_cache, &ctx->neigh_key);
430 if (cgi_ps) {
431 ctx->cgi_ps = *cgi_ps;
432 nacc_fsm_state_chg(fi, NACC_ST_WAIT_REQUEST_SI);
433 return;
434 }
435
436 /* CGI-PS not in cache, resolve it using BSC Neighbor Resolution CTRL interface */
437
438 LOGPFSML(fi, LOGL_DEBUG, "No CGI-PS found in cache, resolving " NEIGH_CACHE_ENTRY_KEY_FMT "...\n",
439 NEIGH_CACHE_ENTRY_KEY_ARGS(&ctx->neigh_key));
440
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200441 if (send_neigh_addr_req(ctx) < 0)
442 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100443}
444
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100445static void st_wait_resolve_rac_ci(struct osmo_fsm_inst *fi, uint32_t event, void *data)
446{
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100447 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100448 const Packet_Cell_Change_Notification_t *notif;
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100449
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100450 switch (event) {
451 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100452 notif = (const Packet_Cell_Change_Notification_t *)data;
453 handle_retrans_pkt_cell_chg_notif(ctx, notif);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100454 break;
455 case NACC_EV_RX_RAC_CI:
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200456 /* data is NULL upon failure */
457 if (data) {
458 ctx->cgi_ps = *(struct osmo_cell_global_id_ps *)data;
459 nacc_fsm_state_chg(fi, NACC_ST_WAIT_REQUEST_SI);
460 } else {
461 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
462 }
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100463 break;
464 default:
465 OSMO_ASSERT(0);
466 }
467}
468
469/* At this point, we expect correct tgt cell info to be already in ctx->cgi_ps */
470static void st_wait_request_si_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
471{
472 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
473 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
474 struct gprs_pcu *pcu = bts->pcu;
475 struct bssgp_ran_information_pdu pdu;
476 const struct si_cache_value *si;
Pau Espin Pedrol44768f22021-02-02 13:11:30 +0100477 struct gprs_rlcmac_bts *bts_i;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100478 int rc;
479
Pau Espin Pedrol44768f22021-02-02 13:11:30 +0100480 /* First check if the CGI-PS addresses a cell managed by this PCU. If
481 * that's the case, we already have the info and there's no need to go
482 * the RIM way since we'd end up to this same PCU on the other end anyway.
483 */
484 llist_for_each_entry(bts_i, &the_pcu->bts_list, list) {
485 if (bts_i == bts) /* Makes no sense targeting the same cell */
486 continue;
487 if (osmo_cgi_ps_cmp(&ctx->cgi_ps, &bts_i->cgi_ps) != 0)
488 continue;
489
490 LOGPFSML(fi, LOGL_DEBUG, "neighbor CGI-PS %s addresses local BTS %d\n",
491 osmo_cgi_ps_name(&ctx->cgi_ps), bts_i->nr);
492 bts_fill_si_cache_value(bts, &ctx->si_info);
493 /* Tell the PCU scheduler we are ready to go, from here one we
494 * are polled/driven by the scheduler */
495 nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA);
496 return;
497 }
498
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100499 /* First check if we have SI info for the target cell in cache */
500 si = si_cache_lookup_value(pcu->si_cache, &ctx->cgi_ps);
501 if (si) {
502 /* Copy info since cache can be deleted at any point */
503 memcpy(&ctx->si_info, si, sizeof(ctx->si_info));
504 /* Tell the PCU scheduler we are ready to go, from here one we
505 * are polled/driven by the scheduler */
506 nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA);
507 return;
508 }
509
510 /* SI info not in cache, resolve it using RIM procedure against SGSN */
511 if (fill_rim_ran_info_req(ctx, &pdu) < 0) {
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100512 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100513 return;
514 }
515
Pau Espin Pedrol9313da52021-02-15 12:12:44 +0100516 LOGPFSML(fi, LOGL_INFO, "Tx RIM RAN-INFO to request SI of %s\n",
517 osmo_cgi_ps_name(&ctx->cgi_ps));
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100518 rc = bssgp_tx_rim(&pdu, gprs_ns2_nse_nsei(ctx->ms->bts->nse));
519 if (rc < 0) {
Pau Espin Pedrol9313da52021-02-15 12:12:44 +0100520 LOGPFSML(fi, LOGL_ERROR, "Failed transmitting RIM RAN-INFO %s PDU: %d\n",
521 osmo_cgi_ps_name(&ctx->cgi_ps), rc);
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100522 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100523 return;
524 }
525}
526
527
528static void st_wait_request_si(struct osmo_fsm_inst *fi, uint32_t event, void *data)
529{
530 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100531 const Packet_Cell_Change_Notification_t *notif;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100532 struct si_cache_entry *entry;
533
534 switch (event) {
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100535 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
536 notif = (const Packet_Cell_Change_Notification_t *)data;
537 handle_retrans_pkt_cell_chg_notif(ctx, notif);
538 break;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100539 case NACC_EV_RX_SI:
540 entry = (struct si_cache_entry *)data;
541 /* Copy info since cache can be deleted at any point */
542 memcpy(&ctx->si_info, &entry->value, sizeof(ctx->si_info));
543 /* Tell the PCU scheduler we are ready to go, from here one we
544 * are polled/driven by the scheduler */
545 nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA);
546 break;
547 default:
548 OSMO_ASSERT(0);
549 }
550}
551
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100552/* At this point, we already received all required SI information to send stored
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100553 * in struct nacc_fsm_ctx. We now wait for scheduler to ask us to construct
554 * RLCMAC DL CTRL messages to move FSM states forward
555 */
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100556static void st_tx_neighbour_data_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
557{
558 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
559 ctx->si_info_bytes_sent = 0;
560 ctx->container_idx = 0;
561}
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100562
563static void st_tx_neighbour_data(struct osmo_fsm_inst *fi, uint32_t event, void *data)
564{
565 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100566 const Packet_Cell_Change_Notification_t *notif;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100567 struct nacc_ev_create_rlcmac_msg_ctx *data_ctx;
568 bool all_si_info_sent;
569
570 switch (event) {
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100571 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
572 notif = (const Packet_Cell_Change_Notification_t *)data;
573 handle_retrans_pkt_cell_chg_notif(ctx, notif);
574 break;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100575 case NACC_EV_CREATE_RLCMAC_MSG:
576 data_ctx = (struct nacc_ev_create_rlcmac_msg_ctx *)data;
577 data_ctx->msg = create_packet_neighbour_cell_data(ctx, data_ctx->tbf, &all_si_info_sent);
578 if (!data_ctx->msg) {
579 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
580 return;
581 }
582 if (all_si_info_sent) /* DONE */
583 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
584 break;
585 default:
586 OSMO_ASSERT(0);
587 }
588}
589
Pau Espin Pedrol1aef1132021-02-01 19:33:59 +0100590/* st_cell_chg_continue_on_enter:
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100591 * At this point, we already sent all Pkt Cell Neighbour Change rlcmac
592 * blocks, and we only need to wait to be scheduled again to send PKT
593 * CELL CHANGE NOTIFICATION and then we are done
594 */
595
Pau Espin Pedrol1aef1132021-02-01 19:33:59 +0100596static void st_cell_chg_continue(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100597{
598 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100599 const Packet_Cell_Change_Notification_t *notif;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100600 struct nacc_ev_create_rlcmac_msg_ctx *data_ctx;
601
602 switch (event) {
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100603 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
604 notif = (const Packet_Cell_Change_Notification_t *)data;
605 handle_retrans_pkt_cell_chg_notif(ctx, notif);
606 break;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100607 case NACC_EV_CREATE_RLCMAC_MSG:
608 data_ctx = (struct nacc_ev_create_rlcmac_msg_ctx *)data;
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100609 data_ctx->msg = create_packet_cell_chg_continue(ctx, data_ctx, &ctx->continue_poll_fn);
610 if (data_ctx->msg) {
Pau Espin Pedrol5ba3ef92022-12-12 18:02:25 +0100611 ctx->continue_poll_ts = data_ctx->pdch->ts_no;
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100612 nacc_fsm_state_chg(fi, NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK);
613 }
614 break;
615 default:
616 OSMO_ASSERT(0);
617 }
618}
619
620static void st_wait_cell_chg_continue_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
621{
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100622 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
623 const Packet_Cell_Change_Notification_t *notif;
624
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100625 switch (event) {
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100626 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
627 notif = (const Packet_Cell_Change_Notification_t *)data;
628 handle_retrans_pkt_cell_chg_notif(ctx, notif);
629 break;
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100630 case NACC_EV_TIMEOUT_CELL_CHG_CONTINUE:
631 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
632 break;
633 case NACC_EV_RX_CELL_CHG_CONTINUE_ACK:
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100634 nacc_fsm_state_chg(fi, NACC_ST_DONE);
635 break;
636 default:
637 OSMO_ASSERT(0);
638 }
639}
640
641
642static void st_done_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
643{
644 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
645}
646
647static void nacc_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
648{
649 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
650 /* after cleanup() finishes, FSM termination calls osmo_fsm_inst_free,
651 so we need to avoid double-freeing it during ctx talloc free
652 destructor */
653 talloc_reparent(ctx, ctx->ms, ctx->fi);
654 ctx->fi = NULL;
655
656 /* remove references from owning MS and free entire ctx */
657 ctx->ms->nacc = NULL;
658 talloc_free(ctx);
659}
660
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100661static int nacc_fsm_timer_cb(struct osmo_fsm_inst *fi)
662{
663 switch (fi->T) {
664 case PCU_TDEF_NEIGH_RESOLVE_TO:
665 case PCU_TDEF_SI_RESOLVE_TO:
666 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
667 break;
668 }
669 return 0;
670}
671
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100672static struct osmo_fsm_state nacc_fsm_states[] = {
673 [NACC_ST_INITIAL] = {
674 .in_event_mask =
675 X(NACC_EV_RX_CELL_CHG_NOTIFICATION),
676 .out_state_mask =
677 X(NACC_ST_WAIT_RESOLVE_RAC_CI),
678 .name = "INITIAL",
679 .action = st_initial,
680 },
681 [NACC_ST_WAIT_RESOLVE_RAC_CI] = {
682 .in_event_mask =
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100683 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100684 X(NACC_EV_RX_RAC_CI),
685 .out_state_mask =
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100686 X(NACC_ST_WAIT_RESOLVE_RAC_CI) |
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100687 X(NACC_ST_WAIT_REQUEST_SI) |
688 X(NACC_ST_TX_CELL_CHG_CONTINUE),
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100689 .name = "WAIT_RESOLVE_RAC_CI",
690 .onenter = st_wait_resolve_rac_ci_on_enter,
691 .action = st_wait_resolve_rac_ci,
692 },
693 [NACC_ST_WAIT_REQUEST_SI] = {
694 .in_event_mask =
695 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
696 X(NACC_EV_RX_SI),
697 .out_state_mask =
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100698 X(NACC_ST_WAIT_RESOLVE_RAC_CI) |
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100699 X(NACC_ST_TX_NEIGHBOUR_DATA) |
700 X(NACC_ST_TX_CELL_CHG_CONTINUE),
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100701 .name = "WAIT_REQUEST_SI",
702 .onenter = st_wait_request_si_on_enter,
703 .action = st_wait_request_si,
704 },
705 [NACC_ST_TX_NEIGHBOUR_DATA] = {
706 .in_event_mask =
707 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100708 X(NACC_EV_CREATE_RLCMAC_MSG),
709 .out_state_mask =
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100710 X(NACC_ST_WAIT_RESOLVE_RAC_CI) |
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100711 X(NACC_ST_TX_CELL_CHG_CONTINUE),
712 .name = "TX_NEIGHBOUR_DATA",
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100713 .onenter = st_tx_neighbour_data_on_enter,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100714 .action = st_tx_neighbour_data,
715 },
716 [NACC_ST_TX_CELL_CHG_CONTINUE] = {
717 .in_event_mask =
718 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100719 X(NACC_EV_CREATE_RLCMAC_MSG),
720 .out_state_mask =
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100721 X(NACC_ST_WAIT_RESOLVE_RAC_CI) |
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100722 X(NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK),
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100723 .name = "TX_CELL_CHG_CONTINUE",
Pau Espin Pedrol1aef1132021-02-01 19:33:59 +0100724 .action = st_cell_chg_continue,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100725 },
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100726 [NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK] = {
727 .in_event_mask =
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100728 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100729 X(NACC_EV_RX_CELL_CHG_CONTINUE_ACK) |
730 X(NACC_EV_TIMEOUT_CELL_CHG_CONTINUE),
731 .out_state_mask =
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100732 X(NACC_ST_WAIT_RESOLVE_RAC_CI) |
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100733 X(NACC_ST_TX_CELL_CHG_CONTINUE) |
734 X(NACC_ST_DONE),
735 .name = "WAIT_CELL_CHG_CONTINUE_ACK",
736 .action = st_wait_cell_chg_continue_ack,
737 },
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100738 [NACC_ST_DONE] = {
739 .in_event_mask = 0,
740 .out_state_mask = 0,
741 .name = "DONE",
742 .onenter = st_done_on_enter,
743 },
744};
745
746static struct osmo_fsm nacc_fsm = {
747 .name = "NACC",
748 .states = nacc_fsm_states,
749 .num_states = ARRAY_SIZE(nacc_fsm_states),
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100750 .timer_cb = nacc_fsm_timer_cb,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100751 .cleanup = nacc_fsm_cleanup,
752 .log_subsys = DNACC,
753 .event_names = nacc_fsm_event_names,
754};
755
756static __attribute__((constructor)) void nacc_fsm_init(void)
757{
758 OSMO_ASSERT(osmo_fsm_register(&nacc_fsm) == 0);
759}
760
761void nacc_fsm_ctrl_reply_cb(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd, void *data)
762{
763 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)data;
764 char *tmp = NULL, *tok, *saveptr;
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100765 unsigned int exp_id;
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200766 struct osmo_cell_global_id_ps cgi_ps;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100767
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100768 LOGPFSML(ctx->fi, LOGL_NOTICE, "Received CTRL message: type=%d %s %s: %s\n",
769 cmd->type, cmd->variable, cmd->id, osmo_escape_str(cmd->reply, -1));
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100770
771 if (cmd->type != CTRL_TYPE_GET_REPLY || !cmd->reply) {
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200772 osmo_fsm_inst_dispatch(ctx->fi, NACC_EV_RX_RAC_CI, NULL);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100773 return;
774 }
775
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100776 /* Validate it's the seqnum from our last GET cmd, and not from older
777 * one we may have requested in case MS decided to resend Pkt Cell
778 * Change Notify with a different tgt cell:
779 */
780 exp_id = arfcn_bsic_2_ctrl_id(ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic);
781 if ((unsigned int)atoi(cmd->id) != exp_id) {
782 LOGPFSML(ctx->fi, LOGL_INFO,
783 "Received CTRL message with id=%s doesn't match our expected last id=%d, ignoring\n",
784 cmd->id, exp_id);
785 return;
786 }
787
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100788 /* TODO: Potentially validate cmd->variable contains same params as we
789 sent, and that cmd->id matches the original set. We may want to keep
790 the original cmd around by setting cmd->defer=1 when sending it. */
791
792 tmp = talloc_strdup(cmd, cmd->reply);
793 if (!tmp)
794 goto free_ret;
795
796 if (!(tok = strtok_r(tmp, "-", &saveptr)))
797 goto free_ret;
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200798 cgi_ps.rai.lac.plmn.mcc = atoi(tok);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100799
800 if (!(tok = strtok_r(NULL, "-", &saveptr)))
801 goto free_ret;
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200802 cgi_ps.rai.lac.plmn.mnc = atoi(tok);
Pau Espin Pedrol263c75c2022-06-29 18:56:42 +0200803 cgi_ps.rai.lac.plmn.mnc_3_digits = strlen(tok) > 2;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100804
805 if (!(tok = strtok_r(NULL, "-", &saveptr)))
806 goto free_ret;
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200807 cgi_ps.rai.lac.lac = atoi(tok);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100808
809 if (!(tok = strtok_r(NULL, "-", &saveptr)))
810 goto free_ret;
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200811 cgi_ps.rai.rac = atoi(tok);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100812
813 if (!(tok = strtok_r(NULL, "\0", &saveptr)))
814 goto free_ret;
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200815 cgi_ps.cell_identity = atoi(tok);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100816
817 /* Cache the cgi_ps so we can avoid requesting again same resolution for a while */
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200818 neigh_cache_add(ctx->ms->bts->pcu->neigh_cache, &ctx->neigh_key, &cgi_ps);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100819
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200820 osmo_fsm_inst_dispatch(ctx->fi, NACC_EV_RX_RAC_CI, &cgi_ps);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100821 return;
822
823free_ret:
824 talloc_free(tmp);
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200825 osmo_fsm_inst_dispatch(ctx->fi, NACC_EV_RX_RAC_CI, NULL);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100826 return;
827}
828
829static int nacc_fsm_ctx_talloc_destructor(struct nacc_fsm_ctx *ctx)
830{
831 if (ctx->fi) {
832 osmo_fsm_inst_free(ctx->fi);
833 ctx->fi = NULL;
834 }
835
836 if (ctx->neigh_ctrl_conn) {
837 if (ctx->neigh_ctrl_conn->write_queue.bfd.fd != -1) {
838 osmo_wqueue_clear(&ctx->neigh_ctrl_conn->write_queue);
839 osmo_fd_unregister(&ctx->neigh_ctrl_conn->write_queue.bfd);
840 close(ctx->neigh_ctrl_conn->write_queue.bfd.fd);
841 ctx->neigh_ctrl_conn->write_queue.bfd.fd = -1;
842 }
843 }
844
845 return 0;
846}
847
848struct nacc_fsm_ctx *nacc_fsm_alloc(struct GprsMs* ms)
849{
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100850 struct nacc_fsm_ctx *ctx = talloc_zero(ms, struct nacc_fsm_ctx);
851 char buf[64];
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100852
853 talloc_set_destructor(ctx, nacc_fsm_ctx_talloc_destructor);
854
855 ctx->ms = ms;
856
857 snprintf(buf, sizeof(buf), "TLLI-0x%08x", ms_tlli(ms));
858 ctx->fi = osmo_fsm_inst_alloc(&nacc_fsm, ctx, ctx, LOGL_INFO, buf);
859 if (!ctx->fi)
860 goto free_ret;
861
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200862 /* If CTRL ip present, use the old CTRL interface for neighbor resolution */
863 if (ms->bts->pcu->vty.neigh_ctrl_addr) {
864 ctx->neigh_ctrl = ctrl_handle_alloc(ctx, ctx, NULL);
865 ctx->neigh_ctrl->reply_cb = nacc_fsm_ctrl_reply_cb;
866 ctx->neigh_ctrl_conn = osmo_ctrl_conn_alloc(ctx, ctx->neigh_ctrl);
867 if (!ctx->neigh_ctrl_conn)
868 goto free_ret;
869 /* Older versions of osmo_ctrl_conn_alloc didn't properly initialize fd to -1,
870 * so make sure to do it here otherwise fd may be valid fd 0 and cause trouble */
871 ctx->neigh_ctrl_conn->write_queue.bfd.fd = -1;
872 llist_add(&ctx->neigh_ctrl_conn->list_entry, &ctx->neigh_ctrl->ccon_list);
873 }
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100874
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100875 return ctx;
876free_ret:
877 talloc_free(ctx);
878 return NULL;
879}
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200880
881bool nacc_fsm_is_waiting_addr_resolution(const struct nacc_fsm_ctx *ctx,
882 const struct neigh_cache_entry_key *neigh_key)
883{
884 if (ctx->fi->state != NACC_ST_WAIT_RESOLVE_RAC_CI)
885 return false;
886 return neigh_cache_entry_key_eq(&ctx->neigh_key, neigh_key);
887}
Pau Espin Pedrol6c81add2021-09-07 14:43:46 +0200888
889bool nacc_fsm_is_waiting_si_resolution(const struct nacc_fsm_ctx *ctx,
890 const struct osmo_cell_global_id_ps *cgi_ps)
891{
892 if (ctx->fi->state != NACC_ST_WAIT_REQUEST_SI)
893 return false;
894 return !osmo_cgi_ps_cmp(&ctx->cgi_ps, cgi_ps);
895}
Pau Espin Pedrolc276e492021-09-28 12:53:35 +0200896
897bool nacc_fsm_exp_ctrl_ack(const struct nacc_fsm_ctx *ctx, uint32_t fn, uint8_t ts)
898{
899 return ctx->fi->state == NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK &&
900 ctx->continue_poll_fn == fn &&
901 ctx->continue_poll_ts == ts;
902}
Pau Espin Pedrol106f2a02022-12-12 17:29:10 +0100903
904bool tbf_nacc_rts(const struct gprs_rlcmac_tbf *tbf, const struct gprs_rlcmac_pdch *pdch)
905{
906 if (!tbf_is_control_ts(tbf, pdch))
907 return false;
908
909 return tbf_is_tfi_assigned(tbf) && ms_nacc_rts(tbf_ms(tbf));
910}