blob: 0a20ae6d9d86c9bf13cc7c11219a7113c3cfb17c [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
43#define nacc_fsm_state_chg(fi, NEXT_STATE) \
44 osmo_fsm_inst_state_chg(fi, NEXT_STATE, 0, 0)
45
46const 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 */
55static 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
135free_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 */
142static 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
180free_ret:
181 talloc_free(mac_control_block);
182 msgb_free(msg);
183 return NULL;
184}
185
186static 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
247static 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
276static 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;
282 struct ctrl_cmd *cmd;
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
298 cmd = ctrl_cmd_create(ctx, CTRL_TYPE_GET);
299 if (!cmd) {
300 LOGPFSML(fi, LOGL_ERROR, "CTRL msg creation failed\n");
301 goto err_term;
302 }
303
304 cmd->id = talloc_asprintf(cmd, "1");
305 cmd->variable = talloc_asprintf(cmd, "neighbor_resolve_cgi_ps_from_lac_ci.%d.%d.%d.%d",
306 ctx->neigh_key.local_lac, ctx->neigh_key.local_ci,
307 ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic);
308 rc = ctrl_cmd_send(&ctx->neigh_ctrl_conn->write_queue, cmd);
309 if (rc) {
310 LOGPFSML(fi, LOGL_ERROR, "CTRL msg sent failed: %d\n", rc);
311 goto err_term;
312 }
313
314 talloc_free(cmd);
315 return;
316
317err_term:
318 talloc_free(cmd);
319 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
320}
321
322
323static void st_wait_resolve_rac_ci(struct osmo_fsm_inst *fi, uint32_t event, void *data)
324{
325 switch (event) {
326 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
327 break;
328 case NACC_EV_RX_RAC_CI:
329 /* Assumption: ctx->cgi_ps has been filled by caller of the event */
330 nacc_fsm_state_chg(fi, NACC_ST_WAIT_REQUEST_SI);
331 break;
332 default:
333 OSMO_ASSERT(0);
334 }
335}
336
337/* At this point, we expect correct tgt cell info to be already in ctx->cgi_ps */
338static void st_wait_request_si_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
339{
340 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
341 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
342 struct gprs_pcu *pcu = bts->pcu;
343 struct bssgp_ran_information_pdu pdu;
344 const struct si_cache_value *si;
345 int rc;
346
347 /* First check if we have SI info for the target cell in cache */
348 si = si_cache_lookup_value(pcu->si_cache, &ctx->cgi_ps);
349 if (si) {
350 /* Copy info since cache can be deleted at any point */
351 memcpy(&ctx->si_info, si, sizeof(ctx->si_info));
352 /* Tell the PCU scheduler we are ready to go, from here one we
353 * are polled/driven by the scheduler */
354 nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA);
355 return;
356 }
357
358 /* SI info not in cache, resolve it using RIM procedure against SGSN */
359 if (fill_rim_ran_info_req(ctx, &pdu) < 0) {
360 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
361 return;
362 }
363
364 rc = bssgp_tx_rim(&pdu, gprs_ns2_nse_nsei(ctx->ms->bts->nse));
365 if (rc < 0) {
366 LOGPFSML(fi, LOGL_ERROR, "Failed transmitting RIM PDU: %d\n", rc);
367 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
368 return;
369 }
370}
371
372
373static void st_wait_request_si(struct osmo_fsm_inst *fi, uint32_t event, void *data)
374{
375 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
376 struct si_cache_entry *entry;
377
378 switch (event) {
379 case NACC_EV_RX_SI:
380 entry = (struct si_cache_entry *)data;
381 /* Copy info since cache can be deleted at any point */
382 memcpy(&ctx->si_info, &entry->value, sizeof(ctx->si_info));
383 /* Tell the PCU scheduler we are ready to go, from here one we
384 * are polled/driven by the scheduler */
385 nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA);
386 break;
387 default:
388 OSMO_ASSERT(0);
389 }
390}
391
392/* st_tx_neighbour_data_on_enter:
393 * At this point, we already received all required SI information to send stored
394 * in struct nacc_fsm_ctx. We now wait for scheduler to ask us to construct
395 * RLCMAC DL CTRL messages to move FSM states forward
396 */
397
398static void st_tx_neighbour_data(struct osmo_fsm_inst *fi, uint32_t event, void *data)
399{
400 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
401 struct nacc_ev_create_rlcmac_msg_ctx *data_ctx;
402 bool all_si_info_sent;
403
404 switch (event) {
405 case NACC_EV_CREATE_RLCMAC_MSG:
406 data_ctx = (struct nacc_ev_create_rlcmac_msg_ctx *)data;
407 data_ctx->msg = create_packet_neighbour_cell_data(ctx, data_ctx->tbf, &all_si_info_sent);
408 if (!data_ctx->msg) {
409 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
410 return;
411 }
412 if (all_si_info_sent) /* DONE */
413 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
414 break;
415 default:
416 OSMO_ASSERT(0);
417 }
418}
419
420/* st_cell_cgh_continue_on_enter:
421 * At this point, we already sent all Pkt Cell Neighbour Change rlcmac
422 * blocks, and we only need to wait to be scheduled again to send PKT
423 * CELL CHANGE NOTIFICATION and then we are done
424 */
425
426static void st_cell_cgh_continue(struct osmo_fsm_inst *fi, uint32_t event, void *data)
427{
428 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
429 struct nacc_ev_create_rlcmac_msg_ctx *data_ctx;
430
431 switch (event) {
432 case NACC_EV_CREATE_RLCMAC_MSG:
433 data_ctx = (struct nacc_ev_create_rlcmac_msg_ctx *)data;
434 data_ctx->msg = create_packet_cell_chg_continue(ctx, data_ctx->tbf);
435 nacc_fsm_state_chg(fi, NACC_ST_DONE);
436 break;
437 default:
438 OSMO_ASSERT(0);
439 }
440}
441
442
443static void st_done_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
444{
445 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
446}
447
448static void nacc_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
449{
450 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
451 /* after cleanup() finishes, FSM termination calls osmo_fsm_inst_free,
452 so we need to avoid double-freeing it during ctx talloc free
453 destructor */
454 talloc_reparent(ctx, ctx->ms, ctx->fi);
455 ctx->fi = NULL;
456
457 /* remove references from owning MS and free entire ctx */
458 ctx->ms->nacc = NULL;
459 talloc_free(ctx);
460}
461
462static struct osmo_fsm_state nacc_fsm_states[] = {
463 [NACC_ST_INITIAL] = {
464 .in_event_mask =
465 X(NACC_EV_RX_CELL_CHG_NOTIFICATION),
466 .out_state_mask =
467 X(NACC_ST_WAIT_RESOLVE_RAC_CI),
468 .name = "INITIAL",
469 .action = st_initial,
470 },
471 [NACC_ST_WAIT_RESOLVE_RAC_CI] = {
472 .in_event_mask =
473 X(NACC_EV_RX_RAC_CI),
474 .out_state_mask =
475 X(NACC_ST_WAIT_REQUEST_SI),
476 .name = "WAIT_RESOLVE_RAC_CI",
477 .onenter = st_wait_resolve_rac_ci_on_enter,
478 .action = st_wait_resolve_rac_ci,
479 },
480 [NACC_ST_WAIT_REQUEST_SI] = {
481 .in_event_mask =
482 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
483 X(NACC_EV_RX_SI),
484 .out_state_mask =
485 X(NACC_ST_TX_NEIGHBOUR_DATA),
486 .name = "WAIT_REQUEST_SI",
487 .onenter = st_wait_request_si_on_enter,
488 .action = st_wait_request_si,
489 },
490 [NACC_ST_TX_NEIGHBOUR_DATA] = {
491 .in_event_mask =
492 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
493 X(NACC_EV_RX_SI) |
494 X(NACC_EV_CREATE_RLCMAC_MSG),
495 .out_state_mask =
496 X(NACC_ST_TX_CELL_CHG_CONTINUE),
497 .name = "TX_NEIGHBOUR_DATA",
498 .action = st_tx_neighbour_data,
499 },
500 [NACC_ST_TX_CELL_CHG_CONTINUE] = {
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_DONE),
507 .name = "TX_CELL_CHG_CONTINUE",
508 .action = st_cell_cgh_continue,
509 },
510 [NACC_ST_DONE] = {
511 .in_event_mask = 0,
512 .out_state_mask = 0,
513 .name = "DONE",
514 .onenter = st_done_on_enter,
515 },
516};
517
518static struct osmo_fsm nacc_fsm = {
519 .name = "NACC",
520 .states = nacc_fsm_states,
521 .num_states = ARRAY_SIZE(nacc_fsm_states),
522 .cleanup = nacc_fsm_cleanup,
523 .log_subsys = DNACC,
524 .event_names = nacc_fsm_event_names,
525};
526
527static __attribute__((constructor)) void nacc_fsm_init(void)
528{
529 OSMO_ASSERT(osmo_fsm_register(&nacc_fsm) == 0);
530}
531
532void nacc_fsm_ctrl_reply_cb(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd, void *data)
533{
534 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)data;
535 char *tmp = NULL, *tok, *saveptr;
536
537 LOGPFSML(ctx->fi, LOGL_NOTICE, "Received CTRL message: type=%d %s: %s\n",
538 cmd->type, cmd->variable, osmo_escape_str(cmd->reply, -1));
539
540 if (cmd->type != CTRL_TYPE_GET_REPLY || !cmd->reply) {
541 osmo_fsm_inst_term(ctx->fi, OSMO_FSM_TERM_ERROR, NULL);
542 return;
543 }
544
545 /* TODO: Potentially validate cmd->variable contains same params as we
546 sent, and that cmd->id matches the original set. We may want to keep
547 the original cmd around by setting cmd->defer=1 when sending it. */
548
549 tmp = talloc_strdup(cmd, cmd->reply);
550 if (!tmp)
551 goto free_ret;
552
553 if (!(tok = strtok_r(tmp, "-", &saveptr)))
554 goto free_ret;
555 ctx->cgi_ps.rai.lac.plmn.mcc = atoi(tok);
556
557 if (!(tok = strtok_r(NULL, "-", &saveptr)))
558 goto free_ret;
559 ctx->cgi_ps.rai.lac.plmn.mnc = atoi(tok);
560
561 if (!(tok = strtok_r(NULL, "-", &saveptr)))
562 goto free_ret;
563 ctx->cgi_ps.rai.lac.lac = atoi(tok);
564
565 if (!(tok = strtok_r(NULL, "-", &saveptr)))
566 goto free_ret;
567 ctx->cgi_ps.rai.rac = atoi(tok);
568
569 if (!(tok = strtok_r(NULL, "\0", &saveptr)))
570 goto free_ret;
571 ctx->cgi_ps.cell_identity = atoi(tok);
572
573 /* Cache the cgi_ps so we can avoid requesting again same resolution for a while */
574 neigh_cache_add(ctx->ms->bts->pcu->neigh_cache, &ctx->neigh_key, &ctx->cgi_ps);
575
576 osmo_fsm_inst_dispatch(ctx->fi, NACC_EV_RX_RAC_CI, NULL);
577 return;
578
579free_ret:
580 talloc_free(tmp);
581 osmo_fsm_inst_term(ctx->fi, OSMO_FSM_TERM_ERROR, NULL);
582 return;
583}
584
585static int nacc_fsm_ctx_talloc_destructor(struct nacc_fsm_ctx *ctx)
586{
587 if (ctx->fi) {
588 osmo_fsm_inst_free(ctx->fi);
589 ctx->fi = NULL;
590 }
591
592 if (ctx->neigh_ctrl_conn) {
593 if (ctx->neigh_ctrl_conn->write_queue.bfd.fd != -1) {
594 osmo_wqueue_clear(&ctx->neigh_ctrl_conn->write_queue);
595 osmo_fd_unregister(&ctx->neigh_ctrl_conn->write_queue.bfd);
596 close(ctx->neigh_ctrl_conn->write_queue.bfd.fd);
597 ctx->neigh_ctrl_conn->write_queue.bfd.fd = -1;
598 }
599 }
600
601 return 0;
602}
603
604struct nacc_fsm_ctx *nacc_fsm_alloc(struct GprsMs* ms)
605{
606 struct gprs_rlcmac_bts *bts = ms->bts;
607 struct gprs_pcu *pcu = bts->pcu;
608 struct nacc_fsm_ctx *ctx = talloc_zero(ms, struct nacc_fsm_ctx);
609 char buf[64];
610 int rc;
611
612 talloc_set_destructor(ctx, nacc_fsm_ctx_talloc_destructor);
613
614 ctx->ms = ms;
615
616 snprintf(buf, sizeof(buf), "TLLI-0x%08x", ms_tlli(ms));
617 ctx->fi = osmo_fsm_inst_alloc(&nacc_fsm, ctx, ctx, LOGL_INFO, buf);
618 if (!ctx->fi)
619 goto free_ret;
620
621 ctx->neigh_ctrl = ctrl_handle_alloc(ctx, ctx, NULL);
622 ctx->neigh_ctrl->reply_cb = nacc_fsm_ctrl_reply_cb;
623 ctx->neigh_ctrl_conn = osmo_ctrl_conn_alloc(ctx, ctx->neigh_ctrl);
624 if (!ctx->neigh_ctrl_conn)
625 goto free_ret;
626 llist_add(&ctx->neigh_ctrl_conn->list_entry, &ctx->neigh_ctrl->ccon_list);
627
628 rc = osmo_sock_init2_ofd(&ctx->neigh_ctrl_conn->write_queue.bfd,
629 AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP,
630 NULL, 0, pcu->vty.neigh_ctrl_addr, pcu->vty.neigh_ctrl_port,
631 OSMO_SOCK_F_CONNECT);
632 if (rc < 0) {
633 LOGP(DNACC, LOGL_ERROR, "Can't connect to CTRL @ %s:%u\n",
634 pcu->vty.neigh_ctrl_addr, pcu->vty.neigh_ctrl_port);
635 goto free_ret;
636 }
637
638 return ctx;
639free_ret:
640 talloc_free(ctx);
641 return NULL;
642}