blob: 738b2c58a32da76df621e9a709bdc8635cadd649 [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.
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
Pau Espin Pedrol069a6372021-02-10 17:33:13 +010043/* Infer CTRL id (seqnum) for a given tgt arfcn+bsic (bsic range: 0-63) */
44#define arfcn_bsic_2_ctrl_id(arfcn, bsic) ((arfcn) * 100 + (bsic))
45
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +010046static const struct osmo_tdef_state_timeout nacc_fsm_timeouts[32] = {
47 [NACC_ST_INITIAL] = {},
48 [NACC_ST_WAIT_RESOLVE_RAC_CI] = { .T = PCU_TDEF_NEIGH_RESOLVE_TO },
49 [NACC_ST_WAIT_REQUEST_SI] = { .T = PCU_TDEF_SI_RESOLVE_TO },
50 [NACC_ST_TX_NEIGHBOUR_DATA] = {},
51 [NACC_ST_TX_CELL_CHG_CONTINUE] = {},
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +010052 [NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK] = {}, /* Timeout through event controlled by tbf::poll_timeout() */
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +010053 [NACC_ST_DONE] = {},
54};
55
Pau Espin Pedrol628d8812021-07-29 13:46:34 +020056/* Transition to a state, using the T timer defined in nacc_fsm_timeouts.
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +010057 * The actual timeout value is in turn obtained from conn->T_defs.
58 * Assumes local variable fi exists. */
59
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010060#define nacc_fsm_state_chg(fi, NEXT_STATE) \
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +010061 osmo_tdef_fsm_inst_state_chg(fi, NEXT_STATE, \
62 nacc_fsm_timeouts, \
63 ((struct nacc_fsm_ctx*)(fi->priv))->ms->bts->pcu->T_defs, \
64 -1)
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010065
66const struct value_string nacc_fsm_event_names[] = {
67 { NACC_EV_RX_CELL_CHG_NOTIFICATION, "RX_CELL_CHG_NOTIFICATION" },
68 { NACC_EV_RX_RAC_CI, "RX_RAC_CI" },
69 { NACC_EV_RX_SI, "RX_SI" },
70 { NACC_EV_CREATE_RLCMAC_MSG, "CREATE_RLCMAC_MSG" },
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +010071 { NACC_EV_RX_CELL_CHG_CONTINUE_ACK, "RX_CELL_CHG_CONTINUE_ACK"},
72 { NACC_EV_TIMEOUT_CELL_CHG_CONTINUE, "TIMEOUT_CELL_CHG_CONTINUE" },
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010073 { 0, NULL }
74};
75
76/* TS 44 060 11.2.9e Packet Neighbour Cell Data */
77static struct msgb *create_packet_neighbour_cell_data(struct nacc_fsm_ctx *ctx,
78 const struct gprs_rlcmac_tbf *tbf,
79 bool *all_si_info_sent)
80{
81 struct msgb *msg;
82 int rc;
83 RlcMacDownlink_t *mac_control_block;
84 struct GprsMs *ms = tbf_ms(tbf);
85 OSMO_ASSERT(tbf_is_tfi_assigned(tbf));
86 uint8_t tfi_is_dl = tbf_direction(tbf) == GPRS_RLCMAC_DL_TBF;
87 uint8_t tfi = tbf_tfi(tbf);
88 uint8_t container_id = 0;
89 PNCDContainer_t container;
90 size_t max_len, len_to_write;
91 uint8_t *cont_buf;
92 uint8_t si_type = ctx->si_info.type_psi ? 0x01 : 0x0;
93
94 memset(&container, 0, sizeof(container));
95 if (ctx->container_idx == 0) {
96 container.UnionType = 1; /* with ID */
97 container.u.PNCD_Container_With_ID.ARFCN = ctx->neigh_key.tgt_arfcn;
98 container.u.PNCD_Container_With_ID.BSIC = ctx->neigh_key.tgt_bsic;
99 cont_buf = &container.u.PNCD_Container_With_ID.CONTAINER[0];
100 max_len = sizeof(container.u.PNCD_Container_With_ID.CONTAINER) - 1;
101 } else {
102 container.UnionType = 0; /* without ID */
103 cont_buf = &container.u.PNCD_Container_Without_ID.CONTAINER[0];
104 max_len = sizeof(container.u.PNCD_Container_Without_ID.CONTAINER) - 1;
105 }
106
107 len_to_write = ctx->si_info.si_len - ctx->si_info_bytes_sent;
108
109 if (len_to_write == 0) {
110 /* We sent all info on last message filing it exactly, we now send a zeroed one to finish */
111 *all_si_info_sent = true;
112 *cont_buf = (si_type << 5) | 0x00;
113 } else if (len_to_write >= max_len) {
114 /* We fill the rlcmac block, we'll need more messages */
115 *all_si_info_sent = false;
116 *cont_buf = (si_type << 5) | 0x1F;
117 memcpy(cont_buf + 1, &ctx->si_info.si_buf[ctx->si_info_bytes_sent], max_len);
118 ctx->si_info_bytes_sent += max_len;
119 } else {
120 /* Last block, we don't fill it exactly */
121 *all_si_info_sent = true;
122 *cont_buf = (si_type << 5) | (len_to_write & 0x1F);
123 memcpy(cont_buf + 1, &ctx->si_info.si_buf[ctx->si_info_bytes_sent], len_to_write);
124 ctx->si_info_bytes_sent += len_to_write;
125 }
126
127 msg = msgb_alloc(GSM_MACBLOCK_LEN, "neighbour_cell_data");
128 if (!msg)
129 return NULL;
130
131 /* Initialize a bit vector that uses allocated msgb as the data buffer. */
132 struct bitvec bv = {
133 .data = msgb_put(msg, GSM_MACBLOCK_LEN),
134 .data_len = GSM_MACBLOCK_LEN,
135 };
136 bitvec_unhex(&bv, DUMMY_VEC);
137
138 mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->ms, RlcMacDownlink_t);
139
140 write_packet_neighbour_cell_data(mac_control_block,
141 tfi_is_dl, tfi, container_id,
142 ctx->container_idx, &container);
143 LOGP(DNACC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Neighbour Cell Data +++++++++++++++++++++++++\n");
144 rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block);
145 if (rc < 0) {
146 LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Neighbour Cell Data failed (%d)\n", rc);
147 goto free_ret;
148 }
149 LOGP(DNACC, LOGL_DEBUG, "------------------------- TX : Packet Neighbour Cell Data -------------------------\n");
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200150 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 +0100151 talloc_free(mac_control_block);
152
153 ctx->container_idx++;
154
155 return msg;
156
157free_ret:
158 talloc_free(mac_control_block);
159 msgb_free(msg);
160 return NULL;
161}
162
163/* TS 44 060 11.2.2a Packet Cell Change Continue */
164static struct msgb *create_packet_cell_chg_continue(const struct nacc_fsm_ctx *ctx,
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100165 const struct nacc_ev_create_rlcmac_msg_ctx *data,
166 uint32_t *new_poll_fn)
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100167{
168 struct msgb *msg;
169 int rc;
170 RlcMacDownlink_t *mac_control_block;
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100171 struct gprs_rlcmac_tbf *tbf = data->tbf;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100172 struct GprsMs *ms = tbf_ms(tbf);
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100173 unsigned int rrbp;
174
175 rc = tbf_check_polling(tbf, data->fn, data->ts, new_poll_fn, &rrbp);
176 if (rc < 0) {
177 LOGP(DTBF, LOGL_ERROR, "Failed registering poll for Pkt Cell Chg Continue (%d)\n", rc);
178 return NULL;
179 }
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100180
181 msg = msgb_alloc(GSM_MACBLOCK_LEN, "pkt_cell_chg_continue");
182 if (!msg)
183 return NULL;
184
185 /* Initialize a bit vector that uses allocated msgb as the data buffer. */
186 struct bitvec bv = {
187 .data = msgb_put(msg, GSM_MACBLOCK_LEN),
188 .data_len = GSM_MACBLOCK_LEN,
189 };
190 bitvec_unhex(&bv, DUMMY_VEC);
191
192 mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->ms, RlcMacDownlink_t);
193
194 OSMO_ASSERT(tbf_is_tfi_assigned(tbf));
195 uint8_t tfi_is_dl = tbf_direction(tbf) == GPRS_RLCMAC_DL_TBF;
196 uint8_t tfi = tbf_tfi(tbf);
197 uint8_t container_id = 0;
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100198 write_packet_cell_change_continue(mac_control_block, 1, rrbp, tfi_is_dl, tfi, true,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100199 ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic, container_id);
200 LOGP(DNACC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Cell Change Continue +++++++++++++++++++++++++\n");
201 rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block);
202 if (rc < 0) {
203 LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Cell Change Continue failed (%d)\n", rc);
204 goto free_ret;
205 }
206 LOGP(DNACC, LOGL_DEBUG, "------------------------- TX : Packet Cell Change Continue -------------------------\n");
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200207 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 +0100208 talloc_free(mac_control_block);
Pau Espin Pedrol86580e12021-03-29 18:15:30 +0200209 tbf_set_polling(tbf, *new_poll_fn, data->ts, PDCH_ULC_POLL_CELL_CHG_CONTINUE);
Pau Espin Pedrola161bf42021-07-30 13:42:06 +0200210 LOGPTBFDL(tbf, LOGL_DEBUG, "Scheduled 'Packet Cell Change Continue' polling on PACCH (FN=%d, TS=%d)\n",
211 *new_poll_fn, data->ts);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100212 return msg;
213
214free_ret:
215 talloc_free(mac_control_block);
216 msgb_free(msg);
217 return NULL;
218}
219
220static int fill_rim_ran_info_req(const struct nacc_fsm_ctx *ctx, struct bssgp_ran_information_pdu *pdu)
221{
222 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
223
224 *pdu = (struct bssgp_ran_information_pdu){
225 .routing_info_dest = {
226 .discr = BSSGP_RIM_ROUTING_INFO_GERAN,
227 .geran = {
228 .raid = {
229 .mcc = ctx->cgi_ps.rai.lac.plmn.mcc,
230 .mnc = ctx->cgi_ps.rai.lac.plmn.mnc,
231 .mnc_3_digits = ctx->cgi_ps.rai.lac.plmn.mnc_3_digits,
232 .lac = ctx->cgi_ps.rai.lac.lac,
233 .rac = ctx->cgi_ps.rai.rac,
234 },
235 .cid = ctx->cgi_ps.cell_identity,
236 },
237 },
238 .routing_info_src = {
239 .discr = BSSGP_RIM_ROUTING_INFO_GERAN,
240 .geran = {
241 .raid = {
242 .mcc = bts->cgi_ps.rai.lac.plmn.mcc,
243 .mnc = bts->cgi_ps.rai.lac.plmn.mnc,
244 .mnc_3_digits = bts->cgi_ps.rai.lac.plmn.mnc_3_digits,
245 .lac = bts->cgi_ps.rai.lac.lac,
246 .rac = bts->cgi_ps.rai.rac,
247 },
248 .cid = bts->cgi_ps.cell_identity,
249 },
250 },
251 .rim_cont_iei = BSSGP_IE_RI_REQ_RIM_CONTAINER,
252 .decoded_present = true,
253 .decoded = {
254 .req_rim_cont = {
255 .app_id = BSSGP_RAN_INF_APP_ID_NACC,
256 .seq_num = 1,
257 .pdu_ind = {
258 .ack_requested = 0,
259 .pdu_type_ext = RIM_PDU_TYPE_SING_REP,
260 },
261 .prot_ver = 1,
262 .son_trans_app_id = NULL,
263 .son_trans_app_id_len = 0,
264 .u = {
265 .app_cont_nacc = {
266 .reprt_cell = ctx->cgi_ps,
267 },
268 },
269 },
270 },
271 };
272
273 return 0;
274}
275
Pau Espin Pedrol0c10b3c2021-02-10 17:12:02 +0100276static int fill_neigh_key_from_bts_pkt_cell_chg_not(struct neigh_cache_entry_key *neigh_key,
277 const struct gprs_rlcmac_bts *bts,
278 const Packet_Cell_Change_Notification_t *notif)
279{
280 switch (notif->Target_Cell.UnionType) {
281 case 0: /* GSM */
282 neigh_key->local_lac = bts->cgi_ps.rai.lac.lac;
283 neigh_key->local_ci = bts->cgi_ps.cell_identity;
284 neigh_key->tgt_arfcn = notif->Target_Cell.u.Target_Cell_GSM_Notif.ARFCN;
285 neigh_key->tgt_bsic = notif->Target_Cell.u.Target_Cell_GSM_Notif.BSIC;
286 return 0;
287 default:
288 return -ENOTSUP;
289 }
290}
291
Pau Espin Pedrol44768f22021-02-02 13:11:30 +0100292#define SI_HDR_LEN 2
293static void bts_fill_si_cache_value(const struct gprs_rlcmac_bts *bts, struct si_cache_value *val)
294{
295 val->type_psi = false;
296 val->si_len = 0;
297 if (bts->si1_is_set) {
298 osmo_static_assert(sizeof(bts->si1) - SI_HDR_LEN == BSSGP_RIM_SI_LEN, _si1_header_size);
299 memcpy(&val->si_buf[val->si_len], bts->si1 + SI_HDR_LEN, BSSGP_RIM_SI_LEN);
300 val->si_len += BSSGP_RIM_SI_LEN;
301 }
302 if (bts->si3_is_set) {
303 osmo_static_assert(sizeof(bts->si3) - SI_HDR_LEN == BSSGP_RIM_SI_LEN, _si3_header_size);
304 memcpy(&val->si_buf[val->si_len], bts->si3 + SI_HDR_LEN, BSSGP_RIM_SI_LEN);
305 val->si_len += BSSGP_RIM_SI_LEN;
306 }
307 if (bts->si13_is_set) {
308 osmo_static_assert(sizeof(bts->si13) - SI_HDR_LEN == BSSGP_RIM_SI_LEN, _si13_header_size);
309 memcpy(&val->si_buf[val->si_len], bts->si13 + SI_HDR_LEN, BSSGP_RIM_SI_LEN);
310 val->si_len += BSSGP_RIM_SI_LEN;
311 }
312}
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100313
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100314/* Called on event NACC_EV_RX_CELL_CHG_NOTIFICATION on states after
315 * WAIT_RESOLVE_RAC_CI. Ignore duplicate messages, transition back if target
316 * cell changed.
317 */
318static void handle_retrans_pkt_cell_chg_notif(struct nacc_fsm_ctx *ctx, const Packet_Cell_Change_Notification_t *notif)
319{
320 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
321 struct neigh_cache_entry_key neigh_key;
322
323 if (fill_neigh_key_from_bts_pkt_cell_chg_not(&neigh_key, bts, notif) < 0) {
324 LOGPFSML(ctx->fi, LOGL_NOTICE, "TargetCell type=0x%x not supported\n",
325 notif->Target_Cell.UnionType);
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100326 if (ctx->fi->state != NACC_ST_TX_CELL_CHG_CONTINUE)
327 nacc_fsm_state_chg(ctx->fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100328 return;
329 }
330 /* If tgt cell changed, restart resolving it */
331 if (!neigh_cache_entry_key_eq(&ctx->neigh_key, &neigh_key)) {
332 ctx->neigh_key = neigh_key;
333 nacc_fsm_state_chg(ctx->fi, NACC_ST_WAIT_RESOLVE_RAC_CI);
334 }
335 /* else: ignore it, it's a dup, carry on what we were doing */
336}
337
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100338////////////////
339// FSM states //
340////////////////
341
342static void st_initial(struct osmo_fsm_inst *fi, uint32_t event, void *data)
343{
344 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
345 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
346 Packet_Cell_Change_Notification_t *notif;
347
348 switch (event) {
349 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
350 notif = (Packet_Cell_Change_Notification_t *)data;
Pau Espin Pedrol0c10b3c2021-02-10 17:12:02 +0100351 if (fill_neigh_key_from_bts_pkt_cell_chg_not(&ctx->neigh_key, bts, notif) < 0) {
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100352 LOGPFSML(fi, LOGL_NOTICE, "TargetCell type=0x%x not supported\n",
353 notif->Target_Cell.UnionType);
354 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
Pau Espin Pedrol0c10b3c2021-02-10 17:12:02 +0100355 } else {
356 nacc_fsm_state_chg(fi, NACC_ST_WAIT_RESOLVE_RAC_CI);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100357 }
358 break;
359 default:
360 OSMO_ASSERT(0);
361 }
362}
363
364static void st_wait_resolve_rac_ci_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
365{
366 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
367 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
368 struct gprs_pcu *pcu = bts->pcu;
369 const struct osmo_cell_global_id_ps *cgi_ps;
Pau Espin Pedrolc0805e62021-01-27 17:16:59 +0100370 struct ctrl_cmd *cmd = NULL;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100371 int rc;
372
373 /* First try to find the value in the cache */
374 cgi_ps = neigh_cache_lookup_value(pcu->neigh_cache, &ctx->neigh_key);
375 if (cgi_ps) {
376 ctx->cgi_ps = *cgi_ps;
377 nacc_fsm_state_chg(fi, NACC_ST_WAIT_REQUEST_SI);
378 return;
379 }
380
381 /* CGI-PS not in cache, resolve it using BSC Neighbor Resolution CTRL interface */
382
383 LOGPFSML(fi, LOGL_DEBUG, "No CGI-PS found in cache, resolving " NEIGH_CACHE_ENTRY_KEY_FMT "...\n",
384 NEIGH_CACHE_ENTRY_KEY_ARGS(&ctx->neigh_key));
385
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100386 /* We may have changed to this state previously (eg: we are handling
387 * another Pkt cell Change Notify with different target). Avoid
388 * re-creating the socket in that case. */
389 if (ctx->neigh_ctrl_conn->write_queue.bfd.fd == -1) {
390 rc = osmo_sock_init2_ofd(&ctx->neigh_ctrl_conn->write_queue.bfd,
391 AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP,
392 NULL, 0, pcu->vty.neigh_ctrl_addr, pcu->vty.neigh_ctrl_port,
393 OSMO_SOCK_F_CONNECT);
394 if (rc < 0) {
395 LOGPFSML(fi, LOGL_ERROR,
396 "Failed to establish CTRL (neighbor resolution) connection to BSC r=%s:%u\n\n",
397 pcu->vty.neigh_ctrl_addr, pcu->vty.neigh_ctrl_port);
398 goto err_term;
399 }
Pau Espin Pedrolc0805e62021-01-27 17:16:59 +0100400 }
401
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100402 cmd = ctrl_cmd_create(ctx, CTRL_TYPE_GET);
403 if (!cmd) {
404 LOGPFSML(fi, LOGL_ERROR, "CTRL msg creation failed\n");
405 goto err_term;
406 }
407
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100408 cmd->id = talloc_asprintf(cmd, "%u", arfcn_bsic_2_ctrl_id(ctx->neigh_key.tgt_arfcn,
409 ctx->neigh_key.tgt_bsic));
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100410 cmd->variable = talloc_asprintf(cmd, "neighbor_resolve_cgi_ps_from_lac_ci.%d.%d.%d.%d",
411 ctx->neigh_key.local_lac, ctx->neigh_key.local_ci,
412 ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic);
413 rc = ctrl_cmd_send(&ctx->neigh_ctrl_conn->write_queue, cmd);
414 if (rc) {
415 LOGPFSML(fi, LOGL_ERROR, "CTRL msg sent failed: %d\n", rc);
416 goto err_term;
417 }
418
419 talloc_free(cmd);
420 return;
421
422err_term:
423 talloc_free(cmd);
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100424 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100425}
426
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100427static void st_wait_resolve_rac_ci(struct osmo_fsm_inst *fi, uint32_t event, void *data)
428{
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100429 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100430 const Packet_Cell_Change_Notification_t *notif;
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100431
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100432 switch (event) {
433 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100434 notif = (const Packet_Cell_Change_Notification_t *)data;
435 handle_retrans_pkt_cell_chg_notif(ctx, notif);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100436 break;
437 case NACC_EV_RX_RAC_CI:
438 /* Assumption: ctx->cgi_ps has been filled by caller of the event */
439 nacc_fsm_state_chg(fi, NACC_ST_WAIT_REQUEST_SI);
440 break;
441 default:
442 OSMO_ASSERT(0);
443 }
444}
445
446/* At this point, we expect correct tgt cell info to be already in ctx->cgi_ps */
447static void st_wait_request_si_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
448{
449 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
450 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
451 struct gprs_pcu *pcu = bts->pcu;
452 struct bssgp_ran_information_pdu pdu;
453 const struct si_cache_value *si;
Pau Espin Pedrol44768f22021-02-02 13:11:30 +0100454 struct gprs_rlcmac_bts *bts_i;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100455 int rc;
456
Pau Espin Pedrol44768f22021-02-02 13:11:30 +0100457 /* First check if the CGI-PS addresses a cell managed by this PCU. If
458 * that's the case, we already have the info and there's no need to go
459 * the RIM way since we'd end up to this same PCU on the other end anyway.
460 */
461 llist_for_each_entry(bts_i, &the_pcu->bts_list, list) {
462 if (bts_i == bts) /* Makes no sense targeting the same cell */
463 continue;
464 if (osmo_cgi_ps_cmp(&ctx->cgi_ps, &bts_i->cgi_ps) != 0)
465 continue;
466
467 LOGPFSML(fi, LOGL_DEBUG, "neighbor CGI-PS %s addresses local BTS %d\n",
468 osmo_cgi_ps_name(&ctx->cgi_ps), bts_i->nr);
469 bts_fill_si_cache_value(bts, &ctx->si_info);
470 /* Tell the PCU scheduler we are ready to go, from here one we
471 * are polled/driven by the scheduler */
472 nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA);
473 return;
474 }
475
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100476 /* First check if we have SI info for the target cell in cache */
477 si = si_cache_lookup_value(pcu->si_cache, &ctx->cgi_ps);
478 if (si) {
479 /* Copy info since cache can be deleted at any point */
480 memcpy(&ctx->si_info, si, sizeof(ctx->si_info));
481 /* Tell the PCU scheduler we are ready to go, from here one we
482 * are polled/driven by the scheduler */
483 nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA);
484 return;
485 }
486
487 /* SI info not in cache, resolve it using RIM procedure against SGSN */
488 if (fill_rim_ran_info_req(ctx, &pdu) < 0) {
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100489 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100490 return;
491 }
492
Pau Espin Pedrol9313da52021-02-15 12:12:44 +0100493 LOGPFSML(fi, LOGL_INFO, "Tx RIM RAN-INFO to request SI of %s\n",
494 osmo_cgi_ps_name(&ctx->cgi_ps));
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100495 rc = bssgp_tx_rim(&pdu, gprs_ns2_nse_nsei(ctx->ms->bts->nse));
496 if (rc < 0) {
Pau Espin Pedrol9313da52021-02-15 12:12:44 +0100497 LOGPFSML(fi, LOGL_ERROR, "Failed transmitting RIM RAN-INFO %s PDU: %d\n",
498 osmo_cgi_ps_name(&ctx->cgi_ps), rc);
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100499 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100500 return;
501 }
502}
503
504
505static void st_wait_request_si(struct osmo_fsm_inst *fi, uint32_t event, void *data)
506{
507 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100508 const Packet_Cell_Change_Notification_t *notif;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100509 struct si_cache_entry *entry;
510
511 switch (event) {
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100512 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
513 notif = (const Packet_Cell_Change_Notification_t *)data;
514 handle_retrans_pkt_cell_chg_notif(ctx, notif);
515 break;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100516 case NACC_EV_RX_SI:
517 entry = (struct si_cache_entry *)data;
518 /* Copy info since cache can be deleted at any point */
519 memcpy(&ctx->si_info, &entry->value, sizeof(ctx->si_info));
520 /* Tell the PCU scheduler we are ready to go, from here one we
521 * are polled/driven by the scheduler */
522 nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA);
523 break;
524 default:
525 OSMO_ASSERT(0);
526 }
527}
528
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100529/* At this point, we already received all required SI information to send stored
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100530 * in struct nacc_fsm_ctx. We now wait for scheduler to ask us to construct
531 * RLCMAC DL CTRL messages to move FSM states forward
532 */
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100533static void st_tx_neighbour_data_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
534{
535 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
536 ctx->si_info_bytes_sent = 0;
537 ctx->container_idx = 0;
538}
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100539
540static void st_tx_neighbour_data(struct osmo_fsm_inst *fi, uint32_t event, void *data)
541{
542 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100543 const Packet_Cell_Change_Notification_t *notif;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100544 struct nacc_ev_create_rlcmac_msg_ctx *data_ctx;
545 bool all_si_info_sent;
546
547 switch (event) {
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100548 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
549 notif = (const Packet_Cell_Change_Notification_t *)data;
550 handle_retrans_pkt_cell_chg_notif(ctx, notif);
551 break;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100552 case NACC_EV_CREATE_RLCMAC_MSG:
553 data_ctx = (struct nacc_ev_create_rlcmac_msg_ctx *)data;
554 data_ctx->msg = create_packet_neighbour_cell_data(ctx, data_ctx->tbf, &all_si_info_sent);
555 if (!data_ctx->msg) {
556 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
557 return;
558 }
559 if (all_si_info_sent) /* DONE */
560 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
561 break;
562 default:
563 OSMO_ASSERT(0);
564 }
565}
566
Pau Espin Pedrol1aef1132021-02-01 19:33:59 +0100567/* st_cell_chg_continue_on_enter:
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100568 * At this point, we already sent all Pkt Cell Neighbour Change rlcmac
569 * blocks, and we only need to wait to be scheduled again to send PKT
570 * CELL CHANGE NOTIFICATION and then we are done
571 */
572
Pau Espin Pedrol1aef1132021-02-01 19:33:59 +0100573static void st_cell_chg_continue(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100574{
575 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100576 const Packet_Cell_Change_Notification_t *notif;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100577 struct nacc_ev_create_rlcmac_msg_ctx *data_ctx;
578
579 switch (event) {
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100580 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
581 notif = (const Packet_Cell_Change_Notification_t *)data;
582 handle_retrans_pkt_cell_chg_notif(ctx, notif);
583 break;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100584 case NACC_EV_CREATE_RLCMAC_MSG:
585 data_ctx = (struct nacc_ev_create_rlcmac_msg_ctx *)data;
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100586 data_ctx->msg = create_packet_cell_chg_continue(ctx, data_ctx, &ctx->continue_poll_fn);
587 if (data_ctx->msg) {
588 ctx->continue_poll_ts = data_ctx->ts;
589 nacc_fsm_state_chg(fi, NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK);
590 }
591 break;
592 default:
593 OSMO_ASSERT(0);
594 }
595}
596
597static void st_wait_cell_chg_continue_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
598{
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100599 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
600 const Packet_Cell_Change_Notification_t *notif;
601
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100602 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 Pedrol952cb3d2021-02-01 14:52:48 +0100607 case NACC_EV_TIMEOUT_CELL_CHG_CONTINUE:
608 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
609 break;
610 case NACC_EV_RX_CELL_CHG_CONTINUE_ACK:
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100611 nacc_fsm_state_chg(fi, NACC_ST_DONE);
612 break;
613 default:
614 OSMO_ASSERT(0);
615 }
616}
617
618
619static void st_done_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
620{
621 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
622}
623
624static void nacc_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
625{
626 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
627 /* after cleanup() finishes, FSM termination calls osmo_fsm_inst_free,
628 so we need to avoid double-freeing it during ctx talloc free
629 destructor */
630 talloc_reparent(ctx, ctx->ms, ctx->fi);
631 ctx->fi = NULL;
632
633 /* remove references from owning MS and free entire ctx */
634 ctx->ms->nacc = NULL;
635 talloc_free(ctx);
636}
637
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100638static int nacc_fsm_timer_cb(struct osmo_fsm_inst *fi)
639{
640 switch (fi->T) {
641 case PCU_TDEF_NEIGH_RESOLVE_TO:
642 case PCU_TDEF_SI_RESOLVE_TO:
643 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
644 break;
645 }
646 return 0;
647}
648
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100649static struct osmo_fsm_state nacc_fsm_states[] = {
650 [NACC_ST_INITIAL] = {
651 .in_event_mask =
652 X(NACC_EV_RX_CELL_CHG_NOTIFICATION),
653 .out_state_mask =
654 X(NACC_ST_WAIT_RESOLVE_RAC_CI),
655 .name = "INITIAL",
656 .action = st_initial,
657 },
658 [NACC_ST_WAIT_RESOLVE_RAC_CI] = {
659 .in_event_mask =
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100660 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100661 X(NACC_EV_RX_RAC_CI),
662 .out_state_mask =
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100663 X(NACC_ST_WAIT_RESOLVE_RAC_CI) |
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100664 X(NACC_ST_WAIT_REQUEST_SI) |
665 X(NACC_ST_TX_CELL_CHG_CONTINUE),
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100666 .name = "WAIT_RESOLVE_RAC_CI",
667 .onenter = st_wait_resolve_rac_ci_on_enter,
668 .action = st_wait_resolve_rac_ci,
669 },
670 [NACC_ST_WAIT_REQUEST_SI] = {
671 .in_event_mask =
672 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
673 X(NACC_EV_RX_SI),
674 .out_state_mask =
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100675 X(NACC_ST_WAIT_RESOLVE_RAC_CI) |
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100676 X(NACC_ST_TX_NEIGHBOUR_DATA) |
677 X(NACC_ST_TX_CELL_CHG_CONTINUE),
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100678 .name = "WAIT_REQUEST_SI",
679 .onenter = st_wait_request_si_on_enter,
680 .action = st_wait_request_si,
681 },
682 [NACC_ST_TX_NEIGHBOUR_DATA] = {
683 .in_event_mask =
684 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100685 X(NACC_EV_CREATE_RLCMAC_MSG),
686 .out_state_mask =
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100687 X(NACC_ST_WAIT_RESOLVE_RAC_CI) |
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100688 X(NACC_ST_TX_CELL_CHG_CONTINUE),
689 .name = "TX_NEIGHBOUR_DATA",
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100690 .onenter = st_tx_neighbour_data_on_enter,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100691 .action = st_tx_neighbour_data,
692 },
693 [NACC_ST_TX_CELL_CHG_CONTINUE] = {
694 .in_event_mask =
695 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100696 X(NACC_EV_CREATE_RLCMAC_MSG),
697 .out_state_mask =
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100698 X(NACC_ST_WAIT_RESOLVE_RAC_CI) |
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100699 X(NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK),
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100700 .name = "TX_CELL_CHG_CONTINUE",
Pau Espin Pedrol1aef1132021-02-01 19:33:59 +0100701 .action = st_cell_chg_continue,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100702 },
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100703 [NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK] = {
704 .in_event_mask =
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100705 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100706 X(NACC_EV_RX_CELL_CHG_CONTINUE_ACK) |
707 X(NACC_EV_TIMEOUT_CELL_CHG_CONTINUE),
708 .out_state_mask =
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100709 X(NACC_ST_WAIT_RESOLVE_RAC_CI) |
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100710 X(NACC_ST_TX_CELL_CHG_CONTINUE) |
711 X(NACC_ST_DONE),
712 .name = "WAIT_CELL_CHG_CONTINUE_ACK",
713 .action = st_wait_cell_chg_continue_ack,
714 },
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100715 [NACC_ST_DONE] = {
716 .in_event_mask = 0,
717 .out_state_mask = 0,
718 .name = "DONE",
719 .onenter = st_done_on_enter,
720 },
721};
722
723static struct osmo_fsm nacc_fsm = {
724 .name = "NACC",
725 .states = nacc_fsm_states,
726 .num_states = ARRAY_SIZE(nacc_fsm_states),
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100727 .timer_cb = nacc_fsm_timer_cb,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100728 .cleanup = nacc_fsm_cleanup,
729 .log_subsys = DNACC,
730 .event_names = nacc_fsm_event_names,
731};
732
733static __attribute__((constructor)) void nacc_fsm_init(void)
734{
735 OSMO_ASSERT(osmo_fsm_register(&nacc_fsm) == 0);
736}
737
738void nacc_fsm_ctrl_reply_cb(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd, void *data)
739{
740 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)data;
741 char *tmp = NULL, *tok, *saveptr;
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100742 unsigned int exp_id;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100743
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100744 LOGPFSML(ctx->fi, LOGL_NOTICE, "Received CTRL message: type=%d %s %s: %s\n",
745 cmd->type, cmd->variable, cmd->id, osmo_escape_str(cmd->reply, -1));
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100746
747 if (cmd->type != CTRL_TYPE_GET_REPLY || !cmd->reply) {
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100748 nacc_fsm_state_chg(ctx->fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100749 return;
750 }
751
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100752 /* Validate it's the seqnum from our last GET cmd, and not from older
753 * one we may have requested in case MS decided to resend Pkt Cell
754 * Change Notify with a different tgt cell:
755 */
756 exp_id = arfcn_bsic_2_ctrl_id(ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic);
757 if ((unsigned int)atoi(cmd->id) != exp_id) {
758 LOGPFSML(ctx->fi, LOGL_INFO,
759 "Received CTRL message with id=%s doesn't match our expected last id=%d, ignoring\n",
760 cmd->id, exp_id);
761 return;
762 }
763
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100764 /* TODO: Potentially validate cmd->variable contains same params as we
765 sent, and that cmd->id matches the original set. We may want to keep
766 the original cmd around by setting cmd->defer=1 when sending it. */
767
768 tmp = talloc_strdup(cmd, cmd->reply);
769 if (!tmp)
770 goto free_ret;
771
772 if (!(tok = strtok_r(tmp, "-", &saveptr)))
773 goto free_ret;
774 ctx->cgi_ps.rai.lac.plmn.mcc = atoi(tok);
775
776 if (!(tok = strtok_r(NULL, "-", &saveptr)))
777 goto free_ret;
778 ctx->cgi_ps.rai.lac.plmn.mnc = atoi(tok);
779
780 if (!(tok = strtok_r(NULL, "-", &saveptr)))
781 goto free_ret;
782 ctx->cgi_ps.rai.lac.lac = atoi(tok);
783
784 if (!(tok = strtok_r(NULL, "-", &saveptr)))
785 goto free_ret;
786 ctx->cgi_ps.rai.rac = atoi(tok);
787
788 if (!(tok = strtok_r(NULL, "\0", &saveptr)))
789 goto free_ret;
790 ctx->cgi_ps.cell_identity = atoi(tok);
791
792 /* Cache the cgi_ps so we can avoid requesting again same resolution for a while */
793 neigh_cache_add(ctx->ms->bts->pcu->neigh_cache, &ctx->neigh_key, &ctx->cgi_ps);
794
795 osmo_fsm_inst_dispatch(ctx->fi, NACC_EV_RX_RAC_CI, NULL);
796 return;
797
798free_ret:
799 talloc_free(tmp);
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100800 nacc_fsm_state_chg(ctx->fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100801 return;
802}
803
804static int nacc_fsm_ctx_talloc_destructor(struct nacc_fsm_ctx *ctx)
805{
806 if (ctx->fi) {
807 osmo_fsm_inst_free(ctx->fi);
808 ctx->fi = NULL;
809 }
810
811 if (ctx->neigh_ctrl_conn) {
812 if (ctx->neigh_ctrl_conn->write_queue.bfd.fd != -1) {
813 osmo_wqueue_clear(&ctx->neigh_ctrl_conn->write_queue);
814 osmo_fd_unregister(&ctx->neigh_ctrl_conn->write_queue.bfd);
815 close(ctx->neigh_ctrl_conn->write_queue.bfd.fd);
816 ctx->neigh_ctrl_conn->write_queue.bfd.fd = -1;
817 }
818 }
819
820 return 0;
821}
822
823struct nacc_fsm_ctx *nacc_fsm_alloc(struct GprsMs* ms)
824{
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100825 struct nacc_fsm_ctx *ctx = talloc_zero(ms, struct nacc_fsm_ctx);
826 char buf[64];
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100827
828 talloc_set_destructor(ctx, nacc_fsm_ctx_talloc_destructor);
829
830 ctx->ms = ms;
831
832 snprintf(buf, sizeof(buf), "TLLI-0x%08x", ms_tlli(ms));
833 ctx->fi = osmo_fsm_inst_alloc(&nacc_fsm, ctx, ctx, LOGL_INFO, buf);
834 if (!ctx->fi)
835 goto free_ret;
836
837 ctx->neigh_ctrl = ctrl_handle_alloc(ctx, ctx, NULL);
838 ctx->neigh_ctrl->reply_cb = nacc_fsm_ctrl_reply_cb;
839 ctx->neigh_ctrl_conn = osmo_ctrl_conn_alloc(ctx, ctx->neigh_ctrl);
840 if (!ctx->neigh_ctrl_conn)
841 goto free_ret;
Pau Espin Pedrol202a4782021-01-27 17:05:12 +0100842 /* Older versions of osmo_ctrl_conn_alloc didn't properly initialize fd to -1,
843 * so make sure to do it here otherwise fd may be valid fd 0 and cause trouble */
844 ctx->neigh_ctrl_conn->write_queue.bfd.fd = -1;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100845 llist_add(&ctx->neigh_ctrl_conn->list_entry, &ctx->neigh_ctrl->ccon_list);
846
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100847 return ctx;
848free_ret:
849 talloc_free(ctx);
850 return NULL;
851}