2 * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <arpa/inet.h>
29 #include <osmocore/msgb.h>
30 #include <osmocore/utils.h>
31 #include <osmocore/gsm48.h>
32 #include <osmocore/talloc.h>
34 #include <osmocom/logging.h>
35 #include <osmocom/osmocom_data.h>
36 #include <osmocom/gsm48_cc.h>
37 #include <osmocom/l23_app.h>
38 #include <osmocom/networks.h>
39 #include <osmocom/l1ctl.h>
43 void mm_conn_free(struct gsm48_mm_conn *conn);
44 static int gsm48_rcv_rr(struct osmocom_ms *ms, struct msgb *msg);
45 static int gsm48_rcv_mmr(struct osmocom_ms *ms, struct msgb *msg);
46 static int gsm48_mm_ev(struct osmocom_ms *ms, int msg_type, struct msgb *msg);
47 static int gsm48_mm_tx_id_rsp(struct osmocom_ms *ms, uint8_t mi_type);
48 static int gsm48_mm_tx_loc_upd_req(struct osmocom_ms *ms);
49 static int gsm48_mm_loc_upd_failed(struct osmocom_ms *ms, struct msgb *msg);
50 static int gsm48_mm_conn_go_dedic(struct osmocom_ms *ms);
51 static int gsm48_mm_init_mm_reject(struct osmocom_ms *ms, struct msgb *msg);
52 static int gsm48_mm_data_ind(struct osmocom_ms *ms, struct msgb *msg);
53 static void new_mm_state(struct gsm48_mmlayer *mm, int state, int substate);
54 static int gsm48_mm_loc_upd_normal(struct osmocom_ms *ms, struct msgb *msg);
55 static int gsm48_mm_loc_upd_periodic(struct osmocom_ms *ms, struct msgb *msg);
56 static int gsm48_mm_loc_upd(struct osmocom_ms *ms, struct msgb *msg);
63 * Notes on IMSI detach procedure:
65 * At the end of the procedure, the state of MM, RR, cell selection: No SIM.
67 * In MM IDLE state, cell available: RR is establised, IMSI detach specific
68 * procedure is performed.
70 * In MM IDLE state, no cell: State is silently changed to No SIM.
72 * During any MM connection state, or Wait for network command: All MM
73 * connections (if any) are released locally, and IMSI detach specific
74 * procedure is performed.
76 * During IMSI detach processing: Request of IMSI detach is ignored.
78 * Any other state: The special 'delay_detach' flag is set only. If set, at any
79 * state transition we will clear the flag and restart the procedure again.
81 * The procedure is not spec conform, but always succeeds.
85 /* Notes on Service states:
87 * There are two PLMN search states:
89 * - PLMN SEARCH NORMAL
92 * They are entered, if: (4.2.1.2)
95 * - user has asked PLMN selection in certain Service states
96 * - coverage is lost in certain Service states
98 * - (optionally see 4.2.1.2)
100 * PLMN SEARCH NORMAL state is then entered, if all these conditions are met:
107 * Otherwhise PLMN SEARCH is entered.
109 * During PLMN SEARCH NORMAL state: (4.2.2.5)
110 * - on expirery of T3211 or T3213: Perform location update, when back
111 * to NORMAL SERVICE state.
112 * - on expirery of T3212: Perform periodic location update, when back
113 * to NORMAL SERVICE state.
114 * - perform IMSI detach
115 * - perform MM connections
116 * - respond to paging (if possible)
118 * During PLMN SEARCH state: (4.2.2.6)
119 * - reject MM connection except for emergency calls
122 * The NO CELL AVAILABLE state is entered, if:
123 * - no cell found during PLMN search
125 * During NO CELL AVAILABLE state:
126 * - reject any MM connection
129 * The NO IMSI state is entered if:
131 * - and cell is selected during PLMN SEARCH states
133 * During NO IMSO state: (4.2.2.4)
134 * - reject MM connection except for emergency calls
137 * The LIMITED SERVICE state is entered if:
139 * - and SIM state is U3
140 * - and cell is selected
142 * During LIMITED SERVICE state: (4.2.2.3)
143 * - reject MM connection except for emergency calls
144 * - perform location update, if new LAI is entered
147 * The LOCATION UPDATE NEEDED state is entered if:
149 * - and location update must be performed for any reason
151 * During LOCATION UPDATE NEEDED state:
152 * - reject MM connection except for emergency calls
154 * This state is left if location update is possible and directly enter
155 * state ATTEMPTING TO UPDATE and trigger location update.
156 * The function gsm48_mm_loc_upd_possible() is used to check this on state
160 * The ATTEMPTING TO UPDATE state is entered if:
162 * - and SIM state is U2
163 * - and cell is selected
165 * During ATTEMPTING TO UPDATE state: (4.2.2.2)
166 * - on expirery of T3211 or T3213: Perform location updated
167 * - on expirery of T3212: Perform location updated
168 * - on change of LAI: Perform location update
169 * - (abnormal cases unsupported)
170 * - accept MM connection for emergency calls
171 * - trigger location update on any other MM connection
172 * - respond to paging (with IMSI only, because in U2 TMSI is not valid)
175 * The NORMAL SERVICE state is entered if:
177 * - and SIM state is U1
178 * - and cell is selected
179 * - and SIM LAI == cell
181 * During NORMAL SERVICE state: (4.2.2.1)
182 * - on expirery of T3211 or T3213: Perform location updated
183 * - on expirery of T3212: Perform location updated
184 * - on change of LAI: Perform location update
185 * - perform IMSI detach
186 * - perform MM connections
187 * - respond to paging
190 * gsm48_mm_set_plmn_search() is used enter PLMN SEARCH or PLMN SEARCH NORMAL
191 * state. Depending on the conditions above, the appropiate state is selected.
194 * gsm48_mm_return_idle() is used to select the Service state when returning
195 * to MM IDLE state after cell reselection.
198 * If cell selection process indicates NO_CELL_FOUND:
200 * - NO CELL AVAILABLE state is entered, if not already.
202 * gsm48_mm_no_cell_found() is used to select the Service state.
205 * If cell selection process indicates CELL_SELECTED:
207 * - NO IMSI state is entered, if no SIM valid.
208 * - Otherwise NORMAL SERVICES state is entered, if
209 * SIM state is U1, SIM LAI == cell, IMSI is attached, T3212 not expired.
210 * - Otherwise NORMAL SERVICES state is entered, if
211 * SIM state is U1, SIM LAI == cell, attach not required, T3212 not expired.
212 * - Otherwise LIMITED SERVICE state is entered, if
213 * CS mode is automatic, cell is forbidden PLMN or forbidden LA.
214 * - Otherwise LIMITED SERVICE state is entered, if
215 * CS mode is manual, cell is not the selected one.
216 * - Otherwise LOCATION UPDATE NEEDED state is entered.
218 * gsm48_mm_cell_selected() is used to select the Service state.
226 /* decode network name */
227 static int decode_network_name(char *name, int name_len,
230 uint8_t in_len = lv[0];
237 /* must be CB encoded */
238 if ((lv[1] & 0x70) != 0x00)
241 padding = lv[1] & 0x03;
242 length = ((in_len - 1) * 8 - padding) / 7;
245 if (length >= name_len)
246 length = name_len - 1;
247 gsm_7bit_decode(name, lv + 2, length);
253 /* encode 'mobile identity' */
254 int gsm48_encode_mi(uint8_t *buf, struct msgb *msg, struct osmocom_ms *ms,
257 struct gsm_subscriber *subscr = &ms->subscr;
258 struct gsm_settings *set = &ms->settings;
262 case GSM_MI_TYPE_TMSI:
263 gsm48_generate_mid_from_tmsi(buf, subscr->tmsi);
265 case GSM_MI_TYPE_IMSI:
266 gsm48_generate_mid_from_imsi(buf, subscr->imsi);
268 case GSM_MI_TYPE_IMEI:
269 gsm48_generate_mid_from_imsi(buf, set->imei);
271 case GSM_MI_TYPE_IMEISV:
272 gsm48_generate_mid_from_imsi(buf, set->imeisv);
274 case GSM_MI_TYPE_NONE:
276 buf[0] = GSM48_IE_MOBILE_ID;
282 buf[2] = (buf[2] & ~GSM_MI_TYPE_MASK) | mi_type;
286 ie = msgb_put(msg, 1 + buf[1]);
287 memcpy(ie, buf + 1, 1 + buf[1]);
293 /* encode 'classmark 1' */
294 int gsm48_encode_classmark1(struct gsm48_classmark1 *cm, uint8_t rev_lev,
295 uint8_t es_ind, uint8_t a5_1, uint8_t pwr_lev)
297 memset(cm, 0, sizeof(*cm));
298 cm->rev_lev = rev_lev;
301 cm->pwr_lev = pwr_lev;
310 static void timeout_mm_t3210(void *arg)
312 struct gsm48_mmlayer *mm = arg;
314 LOGP(DMM, LOGL_INFO, "timer T3210 (loc. upd. timeout) has fired\n");
315 gsm48_mm_ev(mm->ms, GSM48_MM_EVENT_TIMEOUT_T3210, NULL);
318 static void timeout_mm_t3211(void *arg)
320 struct gsm48_mmlayer *mm = arg;
322 LOGP(DSUM, LOGL_INFO, "Location update retry\n");
323 LOGP(DMM, LOGL_INFO, "timer T3211 (loc. upd. retry delay) has fired\n");
324 gsm48_mm_ev(mm->ms, GSM48_MM_EVENT_TIMEOUT_T3211, NULL);
327 static void timeout_mm_t3212(void *arg)
329 struct gsm48_mmlayer *mm = arg;
331 LOGP(DSUM, LOGL_INFO, "Periodic location update\n");
332 LOGP(DMM, LOGL_INFO, "timer T3212 (periodic loc. upd. delay) has "
335 /* reset attempt counter when attempting to update (4.4.4.5) */
336 if (mm->state == GSM48_MM_ST_MM_IDLE
337 && mm->substate == GSM48_MM_SST_ATTEMPT_UPDATE)
338 mm->lupd_attempt = 0;
340 gsm48_mm_ev(mm->ms, GSM48_MM_EVENT_TIMEOUT_T3212, NULL);
343 static void timeout_mm_t3213(void *arg)
345 struct gsm48_mmlayer *mm = arg;
347 LOGP(DSUM, LOGL_INFO, "Location update retry\n");
348 LOGP(DMM, LOGL_INFO, "timer T3213 (delay after RA failure) has "
350 gsm48_mm_ev(mm->ms, GSM48_MM_EVENT_TIMEOUT_T3213, NULL);
353 static void timeout_mm_t3230(void *arg)
355 struct gsm48_mmlayer *mm = arg;
357 LOGP(DMM, LOGL_INFO, "timer T3230 (MM connection timeout) has "
359 gsm48_mm_ev(mm->ms, GSM48_MM_EVENT_TIMEOUT_T3230, NULL);
362 static void timeout_mm_t3220(void *arg)
364 struct gsm48_mmlayer *mm = arg;
366 LOGP(DMM, LOGL_INFO, "timer T3220 (IMSI detach keepalive) has "
368 gsm48_mm_ev(mm->ms, GSM48_MM_EVENT_TIMEOUT_T3220, NULL);
371 static void timeout_mm_t3240(void *arg)
373 struct gsm48_mmlayer *mm = arg;
375 LOGP(DMM, LOGL_INFO, "timer T3240 (RR release timeout) has fired\n");
376 gsm48_mm_ev(mm->ms, GSM48_MM_EVENT_TIMEOUT_T3240, NULL);
379 static void start_mm_t3210(struct gsm48_mmlayer *mm)
381 LOGP(DMM, LOGL_INFO, "starting T3210 (loc. upd. timeout) with %d.%d "
382 "seconds\n", GSM_T3210_MS);
383 mm->t3210.cb = timeout_mm_t3210;
385 bsc_schedule_timer(&mm->t3210, GSM_T3210_MS);
388 static void start_mm_t3211(struct gsm48_mmlayer *mm)
390 LOGP(DMM, LOGL_INFO, "starting T3211 (loc. upd. retry delay) with "
391 "%d.%d seconds\n", GSM_T3211_MS);
392 mm->t3211.cb = timeout_mm_t3211;
394 bsc_schedule_timer(&mm->t3211, GSM_T3211_MS);
397 static void start_mm_t3212(struct gsm48_mmlayer *mm, int sec)
399 /* don't start, if is not available */
403 LOGP(DMM, LOGL_INFO, "starting T3212 (periodic loc. upd. delay) with "
404 "%d seconds\n", sec);
405 mm->t3212.cb = timeout_mm_t3212;
407 bsc_schedule_timer(&mm->t3212, sec, 0);
410 static void start_mm_t3213(struct gsm48_mmlayer *mm)
412 LOGP(DMM, LOGL_INFO, "starting T3213 (delay after RA failure) with "
413 "%d.%d seconds\n", GSM_T3213_MS);
414 mm->t3213.cb = timeout_mm_t3213;
416 bsc_schedule_timer(&mm->t3213, GSM_T3213_MS);
419 static void start_mm_t3220(struct gsm48_mmlayer *mm)
421 LOGP(DMM, LOGL_INFO, "starting T3220 (IMSI detach keepalive) with "
422 "%d.%d seconds\n", GSM_T3220_MS);
423 mm->t3220.cb = timeout_mm_t3220;
425 bsc_schedule_timer(&mm->t3220, GSM_T3220_MS);
428 static void start_mm_t3230(struct gsm48_mmlayer *mm)
430 LOGP(DMM, LOGL_INFO, "starting T3230 (MM connection timeout) with "
431 "%d.%d seconds\n", GSM_T3230_MS);
432 mm->t3230.cb = timeout_mm_t3230;
434 bsc_schedule_timer(&mm->t3230, GSM_T3230_MS);
437 static void start_mm_t3240(struct gsm48_mmlayer *mm)
439 LOGP(DMM, LOGL_INFO, "starting T3240 (RR release timeout) with %d.%d "
440 "seconds\n", GSM_T3240_MS);
441 mm->t3240.cb = timeout_mm_t3240;
443 bsc_schedule_timer(&mm->t3240, GSM_T3240_MS);
446 static void stop_mm_t3210(struct gsm48_mmlayer *mm)
448 if (bsc_timer_pending(&mm->t3210)) {
449 LOGP(DMM, LOGL_INFO, "stopping pending (loc. upd. timeout) "
451 bsc_del_timer(&mm->t3210);
455 static void stop_mm_t3211(struct gsm48_mmlayer *mm)
457 if (bsc_timer_pending(&mm->t3211)) {
458 LOGP(DMM, LOGL_INFO, "stopping pending (loc. upd. retry "
459 "delay) timer T3211\n");
460 bsc_del_timer(&mm->t3211);
464 static void stop_mm_t3212(struct gsm48_mmlayer *mm)
466 if (bsc_timer_pending(&mm->t3212)) {
467 LOGP(DMM, LOGL_INFO, "stopping pending (periodic loc. upd. "
468 "delay) timer T3212\n");
469 bsc_del_timer(&mm->t3212);
473 static void stop_mm_t3213(struct gsm48_mmlayer *mm)
475 if (bsc_timer_pending(&mm->t3213)) {
476 LOGP(DMM, LOGL_INFO, "stopping pending (delay after RA "
477 "failure) timer T3213\n");
478 bsc_del_timer(&mm->t3213);
482 static void stop_mm_t3220(struct gsm48_mmlayer *mm)
484 if (bsc_timer_pending(&mm->t3220)) {
485 LOGP(DMM, LOGL_INFO, "stopping pending (IMSI detach keepalive) "
487 bsc_del_timer(&mm->t3220);
491 static void stop_mm_t3230(struct gsm48_mmlayer *mm)
493 if (bsc_timer_pending(&mm->t3230)) {
494 LOGP(DMM, LOGL_INFO, "stopping pending (MM connection timeout) "
496 bsc_del_timer(&mm->t3230);
500 static void stop_mm_t3240(struct gsm48_mmlayer *mm)
502 if (bsc_timer_pending(&mm->t3240)) {
503 LOGP(DMM, LOGL_INFO, "stopping pending (RR release timeout) "
505 bsc_del_timer(&mm->t3240);
509 static void stop_mm_t3241(struct gsm48_mmlayer *mm)
511 /* not implemented, not required */
518 /* names of MM events */
519 static const struct value_string gsm48_mmevent_names[] = {
520 { GSM48_MM_EVENT_CELL_SELECTED, "MM_EVENT_CELL_SELECTED" },
521 { GSM48_MM_EVENT_NO_CELL_FOUND, "MM_EVENT_NO_CELL_FOUND" },
522 { GSM48_MM_EVENT_TIMEOUT_T3210, "MM_EVENT_TIMEOUT_T3210" },
523 { GSM48_MM_EVENT_TIMEOUT_T3211, "MM_EVENT_TIMEOUT_T3211" },
524 { GSM48_MM_EVENT_TIMEOUT_T3212, "MM_EVENT_TIMEOUT_T3212" },
525 { GSM48_MM_EVENT_TIMEOUT_T3213, "MM_EVENT_TIMEOUT_T3213" },
526 { GSM48_MM_EVENT_TIMEOUT_T3220, "MM_EVENT_TIMEOUT_T3220" },
527 { GSM48_MM_EVENT_TIMEOUT_T3230, "MM_EVENT_TIMEOUT_T3230" },
528 { GSM48_MM_EVENT_TIMEOUT_T3240, "MM_EVENT_TIMEOUT_T3240" },
529 { GSM48_MM_EVENT_IMSI_DETACH, "MM_EVENT_IMSI_DETACH" },
530 { GSM48_MM_EVENT_PAGING, "MM_EVENT_PAGING" },
531 { GSM48_MM_EVENT_AUTH_RESPONSE, "MM_EVENT_AUTH_RESPONSE" },
532 { GSM48_MM_EVENT_SYSINFO, "MM_EVENT_SYSINFO" },
533 { GSM48_MM_EVENT_USER_PLMN_SEL, "MM_EVENT_USER_PLMN_SEL" },
537 const char *get_mmevent_name(int value)
539 return get_value_string(gsm48_mmevent_names, value);
542 /* names of MM-SAP */
543 static const struct value_string gsm48_mm_msg_names[] = {
544 { GSM48_MT_MM_IMSI_DETACH_IND, "MT_MM_IMSI_DETACH_IND" },
545 { GSM48_MT_MM_LOC_UPD_ACCEPT, "MT_MM_LOC_UPD_ACCEPT" },
546 { GSM48_MT_MM_LOC_UPD_REJECT, "MT_MM_LOC_UPD_REJECT" },
547 { GSM48_MT_MM_LOC_UPD_REQUEST, "MT_MM_LOC_UPD_REQUEST" },
548 { GSM48_MT_MM_AUTH_REJ, "MT_MM_AUTH_REJ" },
549 { GSM48_MT_MM_AUTH_REQ, "MT_MM_AUTH_REQ" },
550 { GSM48_MT_MM_AUTH_RESP, "MT_MM_AUTH_RESP" },
551 { GSM48_MT_MM_ID_REQ, "MT_MM_ID_REQ" },
552 { GSM48_MT_MM_ID_RESP, "MT_MM_ID_RESP" },
553 { GSM48_MT_MM_TMSI_REALL_CMD, "MT_MM_TMSI_REALL_CMD" },
554 { GSM48_MT_MM_TMSI_REALL_COMPL, "MT_MM_TMSI_REALL_COMPL" },
555 { GSM48_MT_MM_CM_SERV_ACC, "MT_MM_CM_SERV_ACC" },
556 { GSM48_MT_MM_CM_SERV_REJ, "MT_MM_CM_SERV_REJ" },
557 { GSM48_MT_MM_CM_SERV_ABORT, "MT_MM_CM_SERV_ABORT" },
558 { GSM48_MT_MM_CM_SERV_REQ, "MT_MM_CM_SERV_REQ" },
559 { GSM48_MT_MM_CM_SERV_PROMPT, "MT_MM_CM_SERV_PROMPT" },
560 { GSM48_MT_MM_CM_REEST_REQ, "MT_MM_CM_REEST_REQ" },
561 { GSM48_MT_MM_ABORT, "MT_MM_ABORT" },
562 { GSM48_MT_MM_NULL, "MT_MM_NULL" },
563 { GSM48_MT_MM_STATUS, "MT_MM_STATUS" },
564 { GSM48_MT_MM_INFO, "MT_MM_INFO" },
568 const char *get_mm_name(int value)
570 return get_value_string(gsm48_mm_msg_names, value);
573 /* names of MMxx-SAP */
574 static const struct value_string gsm48_mmxx_msg_names[] = {
575 { GSM48_MMCC_EST_REQ, "MMCC_EST_REQ" },
576 { GSM48_MMCC_EST_IND, "MMCC_EST_IND" },
577 { GSM48_MMCC_EST_CNF, "MMCC_EST_CNF" },
578 { GSM48_MMCC_REL_REQ, "MMCC_REL_REQ" },
579 { GSM48_MMCC_REL_IND, "MMCC_REL_IND" },
580 { GSM48_MMCC_DATA_REQ, "MMCC_DATA_REQ" },
581 { GSM48_MMCC_DATA_IND, "MMCC_DATA_IND" },
582 { GSM48_MMCC_UNIT_DATA_REQ, "MMCC_UNIT_DATA_REQ" },
583 { GSM48_MMCC_UNIT_DATA_IND, "MMCC_UNIT_DATA_IND" },
584 { GSM48_MMCC_SYNC_IND, "MMCC_SYNC_IND" },
585 { GSM48_MMCC_REEST_REQ, "MMCC_REEST_REQ" },
586 { GSM48_MMCC_REEST_CNF, "MMCC_REEST_CNF" },
587 { GSM48_MMCC_ERR_IND, "MMCC_ERR_IND" },
588 { GSM48_MMCC_PROMPT_IND, "MMCC_PROMPT_IND" },
589 { GSM48_MMCC_PROMPT_REJ, "MMCC_PROMPT_REJ" },
590 { GSM48_MMSS_EST_REQ, "MMSS_EST_REQ" },
591 { GSM48_MMSS_EST_IND, "MMSS_EST_IND" },
592 { GSM48_MMSS_EST_CNF, "MMSS_EST_CNF" },
593 { GSM48_MMSS_REL_REQ, "MMSS_REL_REQ" },
594 { GSM48_MMSS_REL_IND, "MMSS_REL_IND" },
595 { GSM48_MMSS_DATA_REQ, "MMSS_DATA_REQ" },
596 { GSM48_MMSS_DATA_IND, "MMSS_DATA_IND" },
597 { GSM48_MMSS_UNIT_DATA_REQ, "MMSS_UNIT_DATA_REQ" },
598 { GSM48_MMSS_UNIT_DATA_IND, "MMSS_UNIT_DATA_IND" },
599 { GSM48_MMSS_REEST_REQ, "MMSS_REEST_REQ" },
600 { GSM48_MMSS_REEST_CNF, "MMSS_REEST_CNF" },
601 { GSM48_MMSS_ERR_IND, "MMSS_ERR_IND" },
602 { GSM48_MMSS_PROMPT_IND, "MMSS_PROMPT_IND" },
603 { GSM48_MMSS_PROMPT_REJ, "MMSS_PROMPT_REJ" },
604 { GSM48_MMSMS_EST_REQ, "MMSMS_EST_REQ" },
605 { GSM48_MMSMS_EST_IND, "MMSMS_EST_IND" },
606 { GSM48_MMSMS_EST_CNF, "MMSMS_EST_CNF" },
607 { GSM48_MMSMS_REL_REQ, "MMSMS_REL_REQ" },
608 { GSM48_MMSMS_REL_IND, "MMSMS_REL_IND" },
609 { GSM48_MMSMS_DATA_REQ, "MMSMS_DATA_REQ" },
610 { GSM48_MMSMS_DATA_IND, "MMSMS_DATA_IND" },
611 { GSM48_MMSMS_UNIT_DATA_REQ, "MMSMS_UNIT_DATA_REQ" },
612 { GSM48_MMSMS_UNIT_DATA_IND, "MMSMS_UNIT_DATA_IND" },
613 { GSM48_MMSMS_REEST_REQ, "MMSMS_REEST_REQ" },
614 { GSM48_MMSMS_REEST_CNF, "MMSMS_REEST_CNF" },
615 { GSM48_MMSMS_ERR_IND, "MMSMS_ERR_IND" },
616 { GSM48_MMSMS_PROMPT_IND, "MMSMS_PROMPT_IND" },
617 { GSM48_MMSMS_PROMPT_REJ, "MMSMS_PROMPT_REJ" },
621 const char *get_mmxx_name(int value)
623 return get_value_string(gsm48_mmxx_msg_names, value);
626 /* names of MMR-SAP */
627 static const struct value_string gsm48_mmr_msg_names[] = {
628 { GSM48_MMR_REG_REQ, "MMR_REG_REQ" },
629 { GSM48_MMR_REG_CNF, "MMR_REG_CNF" },
630 { GSM48_MMR_NREG_REQ, "MMR_NREG_REQ" },
631 { GSM48_MMR_NREG_IND, "MMR_NREG_IND" },
635 const char *get_mmr_name(int value)
637 return get_value_string(gsm48_mmr_msg_names, value);
640 /* allocate GSM 04.08 message (MMxx-SAP) */
641 struct msgb *gsm48_mmxx_msgb_alloc(int msg_type, uint32_t ref,
642 uint8_t transaction_id)
645 struct gsm48_mmxx_hdr *mmh;
647 msg = msgb_alloc_headroom(MMXX_ALLOC_SIZE+MMXX_ALLOC_HEADROOM,
648 MMXX_ALLOC_HEADROOM, "GSM 04.08 MMxx");
652 mmh = (struct gsm48_mmxx_hdr *)msgb_put(msg, sizeof(*mmh));
653 mmh->msg_type = msg_type;
655 mmh->transaction_id = transaction_id;
660 /* allocate MM event message */
661 struct msgb *gsm48_mmevent_msgb_alloc(int msg_type)
664 struct gsm48_mm_event *mme;
666 msg = msgb_alloc_headroom(sizeof(*mme), 0, "GSM 04.08 MM event");
670 mme = (struct gsm48_mm_event *)msgb_put(msg, sizeof(*mme));
671 mme->msg_type = msg_type;
676 /* allocate MMR message */
677 struct msgb *gsm48_mmr_msgb_alloc(int msg_type)
680 struct gsm48_mmr *mmr;
682 msg = msgb_alloc_headroom(sizeof(*mmr), 0, "GSM 04.08 MMR");
686 mmr = (struct gsm48_mmr *)msgb_put(msg, sizeof(*mmr));
687 mmr->msg_type = msg_type;
692 /* queue message (MMxx-SAP) */
693 int gsm48_mmxx_upmsg(struct osmocom_ms *ms, struct msgb *msg)
695 struct gsm48_mmlayer *mm = &ms->mmlayer;
697 msgb_enqueue(&mm->mmxx_upqueue, msg);
702 /* queue message (MMR-SAP) */
703 int gsm48_mmr_downmsg(struct osmocom_ms *ms, struct msgb *msg)
705 struct gsm48_mmlayer *mm = &ms->mmlayer;
707 msgb_enqueue(&mm->mmr_downqueue, msg);
712 /* queue MM event message */
713 int gsm48_mmevent_msg(struct osmocom_ms *ms, struct msgb *msg)
715 struct gsm48_mmlayer *mm = &ms->mmlayer;
717 msgb_enqueue(&mm->event_queue, msg);
722 /* dequeue messages (MMxx-SAP) */
723 int gsm48_mmxx_dequeue(struct osmocom_ms *ms)
725 struct gsm48_mmlayer *mm = &ms->mmlayer;
727 struct gsm48_mmxx_hdr *mmh;
730 while ((msg = msgb_dequeue(&mm->mmxx_upqueue))) {
731 mmh = (struct gsm48_mmxx_hdr *) msg->data;
732 switch (mmh->msg_type & GSM48_MMXX_MASK) {
733 case GSM48_MMCC_CLASS:
734 gsm48_rcv_cc(ms, msg);
737 case GSM48_MMSS_CLASS:
738 gsm48_rcv_ss(ms, msg);
740 case GSM48_MMSMS_CLASS:
741 gsm48_rcv_sms(ms, msg);
746 work = 1; /* work done */
752 /* dequeue messages (MMR-SAP) */
753 int gsm48_mmr_dequeue(struct osmocom_ms *ms)
755 struct gsm48_mmlayer *mm = &ms->mmlayer;
757 struct gsm48_mmr *mmr;
760 while ((msg = msgb_dequeue(&mm->mmr_downqueue))) {
761 mmr = (struct gsm48_mmr *) msg->data;
762 gsm48_rcv_mmr(ms, msg);
764 work = 1; /* work done */
770 /* dequeue messages (RR-SAP) */
771 int gsm48_rr_dequeue(struct osmocom_ms *ms)
773 struct gsm48_mmlayer *mm = &ms->mmlayer;
777 while ((msg = msgb_dequeue(&mm->rr_upqueue))) {
778 /* msg is freed there */
779 gsm48_rcv_rr(ms, msg);
780 work = 1; /* work done */
786 /* dequeue MM event messages */
787 int gsm48_mmevent_dequeue(struct osmocom_ms *ms)
789 struct gsm48_mmlayer *mm = &ms->mmlayer;
790 struct gsm48_mm_event *mme;
794 while ((msg = msgb_dequeue(&mm->event_queue))) {
795 mme = (struct gsm48_mm_event *) msg->data;
796 gsm48_mm_ev(ms, mme->msg_type, msg);
798 work = 1; /* work done */
804 /* push RR header and send to RR */
805 static int gsm48_mm_to_rr(struct osmocom_ms *ms, struct msgb *msg,
806 int msg_type, uint8_t cause)
808 struct gsm48_rr_hdr *rrh;
811 msgb_push(msg, sizeof(struct gsm48_rr_hdr));
812 rrh = (struct gsm48_rr_hdr *) msg->data;
813 rrh->msg_type = msg_type;
816 /* send message to RR */
817 return gsm48_rr_downmsg(ms, msg);
824 const char *gsm48_mm_state_names[] = {
828 "location updating initiated",
830 "wait for outgoing MM connection",
831 "MM connection active",
832 "IMSI detach initiated",
833 "process CM service prompt",
834 "wait for network command",
835 "location updating reject",
838 "wait for RR connection (location updating)",
839 "wait for RR connection (MM connection)",
840 "wait for RR connection (IMSI detach)",
842 "wait for re-establishment",
843 "wait for RR connection active",
845 "wait for additional outgoing MM connection",
846 "MM_CONN_ACTIVE_VGCS",
848 "location updating pending",
849 "IMSI detach pending",
850 "RR connection release not allowed"
853 const char *gsm48_mm_substate_names[] = {
856 "attempting to update",
860 "location updating needed",
862 "PLMN search (normal)",
867 /* change state from LOCATION UPDATE NEEDED to ATTEMPTING TO UPDATE */
868 static int gsm48_mm_loc_upd_possible(struct gsm48_mmlayer *mm)
870 // TODO: check if really possible
871 new_mm_state(mm, GSM48_MM_ST_MM_IDLE, GSM48_MM_SST_ATTEMPT_UPDATE);
872 return gsm48_mm_loc_upd_normal(mm->ms, NULL);
875 /* Set new MM state, also new substate in case of MM IDLE state. */
876 static void new_mm_state(struct gsm48_mmlayer *mm, int state, int substate)
879 if (mm->state == GSM48_MM_ST_MM_IDLE && state == mm->state)
880 LOGP(DMM, LOGL_INFO, "new MM IDLE state %s -> %s\n",
881 gsm48_mm_substate_names[mm->substate],
882 gsm48_mm_substate_names[substate]);
883 /* IDLE -> non-IDLE */
884 else if (mm->state == GSM48_MM_ST_MM_IDLE)
885 LOGP(DMM, LOGL_INFO, "new state MM IDLE, %s -> %s\n",
886 gsm48_mm_substate_names[mm->substate],
887 gsm48_mm_state_names[state]);
888 /* non-IDLE -> IDLE */
889 else if (state == GSM48_MM_ST_MM_IDLE)
890 LOGP(DMM, LOGL_INFO, "new state %s -> MM IDLE, %s\n",
891 gsm48_mm_state_names[mm->state],
892 gsm48_mm_substate_names[substate]);
893 /* non-IDLE -> non-IDLE */
895 LOGP(DMM, LOGL_INFO, "new state %s -> %s\n",
896 gsm48_mm_state_names[mm->state],
897 gsm48_mm_state_names[state]);
899 /* remember most recent substate */
900 if (mm->state == GSM48_MM_ST_MM_IDLE)
901 mm->mr_substate = mm->substate;
904 mm->substate = substate;
906 /* resend detach event, if flag is set */
907 if (state == GSM48_MM_ST_MM_IDLE && mm->delay_detach) {
910 mm->delay_detach = 0;
912 nmsg = gsm48_mmevent_msgb_alloc(GSM48_MM_EVENT_IMSI_DETACH);
915 gsm48_mmevent_msg(mm->ms, nmsg);
918 /* 4.4.2 start T3212 in MM IDLE mode if not started or has expired */
919 if (state == GSM48_MM_ST_MM_IDLE
920 && (substate == GSM48_MM_SST_NORMAL_SERVICE
921 || substate == GSM48_MM_SST_ATTEMPT_UPDATE)) {
922 /* start periodic location update timer */
923 if (!bsc_timer_pending(&mm->t3212))
924 start_mm_t3212(mm, mm->t3212_value);
925 /* perform pending location update */
926 if (mm->lupd_retry) {
927 LOGP(DMM, LOGL_INFO, "Loc. upd. pending (type %d)\n",
930 gsm48_mm_loc_upd(mm->ms, NULL);
931 /* must exit, because this function can be called
936 if (mm->lupd_periodic) {
937 struct gsm48_sysinfo *s = &mm->ms->cellsel.sel_si;
939 LOGP(DMM, LOGL_INFO, "Periodic loc. upd. pending "
940 "(type %d)\n", mm->lupd_type);
941 mm->lupd_periodic = 0;
943 gsm48_mm_loc_upd_periodic(mm->ms, NULL);
945 LOGP(DMM, LOGL_INFO, "but not requred\n");
946 /* must exit, because this function can be called
953 /* check if location update is possible */
954 if (state == GSM48_MM_ST_MM_IDLE
955 && substate == GSM48_MM_SST_LOC_UPD_NEEDED) {
956 gsm48_mm_loc_upd_possible(mm);
957 /* must exit, because this function can be called recursively */
962 /* return PLMN SEARCH or PLMN SEARCH NORMAL state */
963 static int gsm48_mm_set_plmn_search(struct osmocom_ms *ms)
965 struct gsm_subscriber *subscr = &ms->subscr;
966 struct gsm322_cellsel *cs = &ms->cellsel;
968 /* SIM not inserted */
969 if (!subscr->sim_valid) {
970 LOGP(DMM, LOGL_INFO, "Selecting PLMN SEARCH state, because "
972 return GSM48_MM_SST_PLMN_SEARCH;
975 /* SIM not updated */
976 if (subscr->ustate != GSM_SIM_U1_UPDATED) {
977 LOGP(DMM, LOGL_INFO, "Selecting PLMN SEARCH state, because "
978 "SIM not updated.\n");
979 return GSM48_MM_SST_PLMN_SEARCH;
981 if (!subscr->lai_valid) {
982 LOGP(DMM, LOGL_INFO, "Selecting PLMN SEARCH state, because "
983 "LAI in SIM not valid.\n");
984 return GSM48_MM_SST_PLMN_SEARCH;
987 /* no cell selected */
989 LOGP(DMM, LOGL_INFO, "Selecting PLMN SEARCH state, because "
990 "no cell selected.\n");
991 return GSM48_MM_SST_PLMN_SEARCH;
994 /* selected cell's LAI not equal to LAI stored on the sim */
995 if (cs->sel_mcc != subscr->lai_mcc
996 || cs->sel_mnc != subscr->lai_mnc
997 || cs->sel_lac != subscr->lai_lac) {
998 LOGP(DMM, LOGL_INFO, "Selecting PLMN SEARCH state, because "
999 "LAI of selected cell (MCC %s MNC %s LAC 0x%04x) "
1000 "!= LAI in SIM (MCC %s MNC %s LAC 0x%04x).\n",
1001 gsm_print_mcc(cs->sel_mcc), gsm_print_mnc(cs->sel_mnc),
1002 cs->sel_lac, gsm_print_mcc(subscr->lai_mcc),
1003 gsm_print_mnc(subscr->lai_mnc), subscr->lai_lac);
1004 return GSM48_MM_SST_PLMN_SEARCH;
1007 /* SIM is updated in this LA */
1008 LOGP(DMM, LOGL_INFO, "Selecting PLMN SEARCH NORMAL state.\n");
1009 return GSM48_MM_SST_PLMN_SEARCH_NORMAL;
1012 /* 4.2.3 when returning to MM IDLE state, this function is called */
1013 static int gsm48_mm_return_idle(struct osmocom_ms *ms, struct msgb *msg)
1015 struct gsm_subscriber *subscr = &ms->subscr;
1016 struct gsm48_mmlayer *mm = &ms->mmlayer;
1017 struct gsm322_cellsel *cs = &ms->cellsel;
1019 /* 4.4.4.9 start T3211 when RR is released */
1020 if (mm->start_t3211) {
1021 LOGP(DMM, LOGL_INFO, "Starting T3211 after RR release.\n");
1022 mm->start_t3211 = 0;
1026 /* return from location update with "Roaming not allowed" */
1027 if (mm->state == GSM48_MM_ST_LOC_UPD_REJ && mm->lupd_rej_cause == 13) {
1028 LOGP(DMM, LOGL_INFO, "Roaming not allowed as returning to "
1030 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1031 gsm48_mm_set_plmn_search(ms));
1036 /* no SIM present or invalid */
1037 if (!subscr->sim_valid) {
1038 LOGP(DMM, LOGL_INFO, "SIM invalid as returning to MM IDLE\n");
1040 /* stop periodic location updating */
1041 mm->lupd_pending = 0;
1042 stop_mm_t3212(mm); /* 4.4.2 */
1044 new_mm_state(mm, GSM48_MM_ST_MM_IDLE, GSM48_MM_SST_NO_IMSI);
1049 /* selected cell equals the registered LAI */
1050 if (subscr->lai_valid
1051 && cs->sel_mcc == subscr->lai_mcc
1052 && cs->sel_mnc == subscr->lai_mnc
1053 && cs->sel_lac == subscr->lai_lac) {
1054 LOGP(DMM, LOGL_INFO, "We are in registered LAI as returning "
1056 /* if SIM not updated (abnormal case as described in 4.4.4.9) */
1057 if (subscr->ustate != GSM_SIM_U1_UPDATED)
1058 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1059 GSM48_MM_SST_ATTEMPT_UPDATE);
1061 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1062 GSM48_MM_SST_NORMAL_SERVICE);
1067 /* location update allowed */
1068 if (cs->state == GSM322_C3_CAMPED_NORMALLY) {
1069 LOGP(DMM, LOGL_INFO, "We are camping normally as returning to "
1071 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1072 GSM48_MM_SST_LOC_UPD_NEEDED);
1073 } else { /* location update not allowed */
1074 LOGP(DMM, LOGL_INFO, "We are camping on any cell as returning "
1076 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1077 GSM48_MM_SST_LIMITED_SERVICE);
1083 /* 4.2.1.1 Service state PLMN SEARCH (NORMAL) is left if no cell found */
1084 static int gsm48_mm_no_cell_found(struct osmocom_ms *ms, struct msgb *msg)
1086 struct gsm48_mmlayer *mm = &ms->mmlayer;
1088 new_mm_state(mm, GSM48_MM_ST_MM_IDLE, GSM48_MM_SST_NO_CELL_AVAIL);
1093 /* 4.2.1.1 Service state PLMN SEARCH (NORMAL) / NO CELL AVAILABLE is left
1096 static int gsm48_mm_cell_selected(struct osmocom_ms *ms, struct msgb *msg)
1098 struct gsm_subscriber *subscr = &ms->subscr;
1099 struct gsm48_mmlayer *mm = &ms->mmlayer;
1100 struct gsm322_plmn *plmn = &ms->plmn;
1101 struct gsm322_cellsel *cs = &ms->cellsel;
1102 struct gsm48_sysinfo *s = &cs->sel_si;
1103 struct gsm_settings *set = &ms->settings;
1105 /* no SIM is inserted */
1106 if (!subscr->sim_valid) {
1107 LOGP(DMM, LOGL_INFO, "SIM invalid as cell is selected.\n");
1108 new_mm_state(mm, GSM48_MM_ST_MM_IDLE, GSM48_MM_SST_NO_IMSI);
1113 /* SIM not updated in this LA */
1114 if (subscr->ustate == GSM_SIM_U1_UPDATED
1115 && subscr->lai_valid
1116 && cs->sel_mcc == subscr->lai_mcc
1117 && cs->sel_mnc == subscr->lai_mnc
1118 && cs->sel_lac == subscr->lai_lac
1119 && !mm->lupd_periodic) {
1120 if (subscr->imsi_attached) {
1123 LOGP(DMM, LOGL_INFO, "Valid in location area.\n");
1124 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1125 GSM48_MM_SST_NORMAL_SERVICE);
1127 /* send message to PLMN search process */
1128 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_SUCCESS);
1131 gsm322_plmn_sendmsg(ms, nmsg);
1135 if (!s->att_allowed) {
1138 LOGP(DMM, LOGL_INFO, "Attachment not required.\n");
1139 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1140 GSM48_MM_SST_NORMAL_SERVICE);
1142 /* send message to PLMN search process */
1143 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_SUCCESS);
1146 gsm322_plmn_sendmsg(ms, nmsg);
1150 /* else, continue */
1153 /* PLMN mode auto and selected cell is forbidden */
1154 if (set->plmn_mode == PLMN_MODE_AUTO
1155 && (gsm_subscr_is_forbidden_plmn(subscr, cs->sel_mcc, cs->sel_mnc)
1156 || gsm322_is_forbidden_la(ms, cs->sel_mcc, cs->sel_mnc,
1160 LOGP(DMM, LOGL_INFO, "Selected cell is forbidden.\n");
1161 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1162 GSM48_MM_SST_LIMITED_SERVICE);
1164 /* send message to PLMN search process */
1165 nmsg = gsm322_msgb_alloc(GSM322_EVENT_ROAMING_NA);
1168 gsm322_plmn_sendmsg(ms, nmsg);
1173 /* PLMN mode manual and selected cell not selected PLMN */
1174 if (set->plmn_mode == PLMN_MODE_MANUAL
1175 && (plmn->mcc != cs->sel_mcc
1176 || plmn->mnc != cs->sel_mnc)) {
1179 LOGP(DMM, LOGL_INFO, "Selected cell not found.\n");
1180 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1181 GSM48_MM_SST_LIMITED_SERVICE);
1183 /* send message to PLMN search process */
1184 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_FAILED);
1187 gsm322_plmn_sendmsg(ms, nmsg);
1193 new_mm_state(mm, GSM48_MM_ST_MM_IDLE, GSM48_MM_SST_LOC_UPD_NEEDED);
1198 /* 4.2.1.2 Service state PLMN SEARCH (NORMAL) is entered */
1199 static int gsm48_mm_plmn_search(struct osmocom_ms *ms, struct msgb *msg)
1201 struct gsm48_mmlayer *mm = &ms->mmlayer;
1203 new_mm_state(mm, GSM48_MM_ST_MM_IDLE, gsm48_mm_set_plmn_search(ms));
1212 /* initialize Mobility Management process */
1213 int gsm48_mm_init(struct osmocom_ms *ms)
1215 struct gsm48_mmlayer *mm = &ms->mmlayer;
1217 memset(mm, 0, sizeof(*mm));
1220 LOGP(DMM, LOGL_INFO, "init Mobility Management process\n");
1223 mm->state = GSM48_MM_ST_MM_IDLE;
1224 mm->substate = gsm48_mm_set_plmn_search(ms);
1227 INIT_LLIST_HEAD(&mm->mm_conn);
1228 INIT_LLIST_HEAD(&mm->rr_upqueue);
1229 INIT_LLIST_HEAD(&mm->mmxx_upqueue);
1230 INIT_LLIST_HEAD(&mm->mmr_downqueue);
1231 INIT_LLIST_HEAD(&mm->event_queue);
1236 /* exit MM process */
1237 int gsm48_mm_exit(struct osmocom_ms *ms)
1239 struct gsm48_mmlayer *mm = &ms->mmlayer;
1240 struct gsm48_mm_conn *conn;
1243 LOGP(DMM, LOGL_INFO, "exit Mobility Management process\n");
1246 while (!llist_empty(&mm->mm_conn)) {
1247 conn = llist_entry(mm->mm_conn.next,
1248 struct gsm48_mm_conn, list);
1251 while ((msg = msgb_dequeue(&mm->rr_upqueue)))
1253 while ((msg = msgb_dequeue(&mm->mmxx_upqueue)))
1255 while ((msg = msgb_dequeue(&mm->mmr_downqueue)))
1257 while ((msg = msgb_dequeue(&mm->event_queue)))
1273 * MM connection management
1276 static const char *gsm48_mmxx_state_names[] = {
1284 uint32_t mm_conn_new_ref = 0x80000001;
1286 /* new MM connection state */
1287 static void new_conn_state(struct gsm48_mm_conn *conn, int state)
1289 LOGP(DMM, LOGL_INFO, "(ref %x) new state %s -> %s\n", conn->ref,
1290 gsm48_mmxx_state_names[conn->state],
1291 gsm48_mmxx_state_names[state]);
1292 conn->state = state;
1295 /* find MM connection by protocol+ID */
1296 struct gsm48_mm_conn *mm_conn_by_id(struct gsm48_mmlayer *mm,
1297 uint8_t proto, uint8_t transaction_id)
1299 struct gsm48_mm_conn *conn;
1301 llist_for_each_entry(conn, &mm->mm_conn, list) {
1302 if (conn->protocol == proto &&
1303 conn->transaction_id == transaction_id)
1309 /* find MM connection by reference */
1310 struct gsm48_mm_conn *mm_conn_by_ref(struct gsm48_mmlayer *mm,
1313 struct gsm48_mm_conn *conn;
1315 llist_for_each_entry(conn, &mm->mm_conn, list) {
1316 if (conn->ref == ref)
1322 /* create MM connection instance */
1323 static struct gsm48_mm_conn* mm_conn_new(struct gsm48_mmlayer *mm,
1324 int proto, uint8_t transaction_id, uint32_t ref)
1326 struct gsm48_mm_conn *conn = talloc_zero(l23_ctx, struct gsm48_mm_conn);
1331 LOGP(DMM, LOGL_INFO, "New MM Connection (proto 0x%02x trans_id %d "
1332 "ref %d)\n", proto, transaction_id, ref);
1335 conn->state = GSM48_MMXX_ST_IDLE;
1336 conn->transaction_id = transaction_id;
1337 conn->protocol = proto;
1340 llist_add(&conn->list, &mm->mm_conn);
1345 /* destroy MM connection instance */
1346 void mm_conn_free(struct gsm48_mm_conn *conn)
1348 LOGP(DMM, LOGL_INFO, "Freeing MM Connection\n");
1350 new_conn_state(conn, GSM48_MMXX_ST_IDLE);
1352 llist_del(&conn->list);
1357 /* support function to release pending/all ongoing MM connections */
1358 static int gsm48_mm_release_mm_conn(struct osmocom_ms *ms, int abort_any,
1359 uint8_t cause, int error)
1361 struct gsm48_mmlayer *mm = &ms->mmlayer;
1362 struct gsm48_mm_conn *conn, *conn2;
1364 struct gsm48_mmxx_hdr *nmmh;
1367 LOGP(DMM, LOGL_INFO, "Release any MM Connection\n");
1369 LOGP(DMM, LOGL_INFO, "Release pending MM Connections\n");
1371 /* release MM connection(s) */
1372 llist_for_each_entry_safe(conn, conn2, &mm->mm_conn, list) {
1373 /* abort any OR the pending connection */
1374 if (abort_any || conn->state == GSM48_MMXX_ST_CONN_PEND) {
1375 /* send MMxx-REL-IND */
1377 switch(conn->protocol) {
1378 case GSM48_PDISC_CC:
1379 nmsg = gsm48_mmxx_msgb_alloc(
1380 error ? GSM48_MMCC_ERR_IND
1381 : GSM48_MMCC_REL_IND, conn->ref,
1382 conn->transaction_id);
1384 case GSM48_PDISC_NC_SS:
1385 nmsg = gsm48_mmxx_msgb_alloc(
1386 error ? GSM48_MMSS_ERR_IND
1387 : GSM48_MMSS_REL_IND, conn->ref,
1388 conn->transaction_id);
1390 case GSM48_PDISC_SMS:
1391 nmsg = gsm48_mmxx_msgb_alloc(
1392 error ? GSM48_MMSMS_ERR_IND
1393 : GSM48_MMSMS_REL_IND, conn->ref,
1394 conn->transaction_id);
1398 /* this should not happen */
1400 continue; /* skip if not of CC type */
1402 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
1403 nmmh->cause = cause;
1404 gsm48_mmxx_upmsg(ms, nmsg);
1413 * process handlers (Common procedures)
1416 /* sending MM STATUS message */
1417 static int gsm48_mm_tx_mm_status(struct osmocom_ms *ms, uint8_t cause)
1420 struct gsm48_hdr *ngh;
1421 uint8_t *reject_cause;
1423 LOGP(DMM, LOGL_INFO, "MM STATUS (cause #%d)\n", cause);
1425 nmsg = gsm48_l3_msgb_alloc();
1428 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
1429 reject_cause = msgb_put(nmsg, 1);
1431 ngh->proto_discr = GSM48_PDISC_MM;
1432 ngh->msg_type = GSM48_MT_MM_STATUS;
1433 *reject_cause = cause;
1435 /* push RR header and send down */
1436 return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_DATA_REQ, 0);
1439 /* 4.3.1.2 sending TMSI REALLOCATION COMPLETE message */
1440 static int gsm48_mm_tx_tmsi_reall_cpl(struct osmocom_ms *ms)
1443 struct gsm48_hdr *ngh;
1445 LOGP(DMM, LOGL_INFO, "TMSI REALLOCATION COMPLETE\n");
1447 nmsg = gsm48_l3_msgb_alloc();
1450 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
1452 ngh->proto_discr = GSM48_PDISC_MM;
1453 ngh->msg_type = GSM48_MT_MM_TMSI_REALL_COMPL;
1455 /* push RR header and send down */
1456 return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_DATA_REQ, 0);
1459 /* 4.3.1 TMSI REALLOCATION COMMAND is received */
1460 static int gsm48_mm_rx_tmsi_realloc_cmd(struct osmocom_ms *ms, struct msgb *msg)
1462 struct gsm_subscriber *subscr = &ms->subscr;
1463 struct gsm48_hdr *gh = msgb_l3(msg);
1464 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1465 struct gsm48_loc_area_id *lai = (struct gsm48_loc_area_id *) gh->data;
1466 uint8_t mi_type, *mi;
1469 if (payload_len < sizeof(struct gsm48_loc_area_id) + 2) {
1471 LOGP(DMM, LOGL_NOTICE, "Short read of TMSI REALLOCATION "
1472 "COMMAND message error.\n");
1476 gsm48_decode_lai(lai, &subscr->lai_mcc, &subscr->lai_mnc,
1479 mi = gh->data + sizeof(struct gsm48_loc_area_id);
1480 mi_type = mi[1] & GSM_MI_TYPE_MASK;
1482 case GSM_MI_TYPE_TMSI:
1483 if (payload_len + sizeof(struct gsm48_loc_area_id) < 6
1486 memcpy(&tmsi, mi+2, 4);
1487 subscr->tmsi = ntohl(tmsi);
1488 subscr->tmsi_valid = 1;
1489 LOGP(DMM, LOGL_INFO, "TMSI 0x%08x (%u) assigned.\n",
1490 subscr->tmsi, subscr->tmsi);
1491 gsm48_mm_tx_tmsi_reall_cpl(ms);
1493 case GSM_MI_TYPE_IMSI:
1494 subscr->tmsi_valid = 0;
1495 LOGP(DMM, LOGL_INFO, "TMSI removed.\n");
1496 gsm48_mm_tx_tmsi_reall_cpl(ms);
1499 LOGP(DMM, LOGL_NOTICE, "TMSI reallocation with unknown MI "
1500 "type %d.\n", mi_type);
1501 gsm48_mm_tx_mm_status(ms, GSM48_REJECT_INCORRECT_MESSAGE);
1503 return 0; /* don't store in SIM */
1507 store / remove from sim
1514 static int gsm48_mm_tx_auth_rsp(struct osmocom_ms *ms, struct msgb *msg);
1517 /* 4.3.2.2 AUTHENTICATION REQUEST is received */
1518 static int gsm48_mm_rx_auth_req(struct osmocom_ms *ms, struct msgb *msg)
1520 struct gsm_subscriber *subscr = &ms->subscr;
1521 struct gsm48_hdr *gh = msgb_l3(msg);
1522 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1523 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) gh->data;
1525 if (payload_len < sizeof(struct gsm48_auth_req)) {
1526 LOGP(DMM, LOGL_NOTICE, "Short read of AUTHENTICATION REQUEST "
1527 "message error.\n");
1531 /* SIM is not available */
1532 if (!subscr->sim_valid) {
1533 LOGP(DMM, LOGL_INFO, "AUTHENTICATION REQUEST without SIM\n");
1534 return gsm48_mm_tx_mm_status(ms,
1535 GSM48_REJECT_MSG_NOT_COMPATIBLE);
1538 LOGP(DMM, LOGL_INFO, "AUTHENTICATION REQUEST (seq %d)\n", ar->key_seq);
1540 /* key_seq and random */
1543 (..., ar->key_seq, ar->rand);
1547 struct gsm48_mm_event *nmme;
1548 nmsg = gsm48_l3_msgb_alloc();
1551 nmme = (struct gsm48_mm_event *)msgb_put(nmsg, sizeof(*nmme));
1552 *((uint32_t *)nmme->sres) = 0x12345678;
1553 gsm48_mm_tx_auth_rsp(ms, nmsg);
1557 /* wait for auth response event from SIM */
1561 /* 4.3.2.2 sending AUTHENTICATION RESPONSE */
1562 static int gsm48_mm_tx_auth_rsp(struct osmocom_ms *ms, struct msgb *msg)
1564 struct gsm48_mm_event *mme = (struct gsm48_mm_event *) msg->data;
1566 struct gsm48_hdr *ngh;
1569 LOGP(DMM, LOGL_INFO, "AUTHENTICATION RESPONSE\n");
1571 nmsg = gsm48_l3_msgb_alloc();
1574 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
1576 ngh->proto_discr = GSM48_PDISC_MM;
1577 ngh->msg_type = GSM48_MT_MM_AUTH_RESP;
1580 sres = msgb_put(nmsg, 4);
1581 memcpy(sres, mme->sres, 4);
1583 /* push RR header and send down */
1584 return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_DATA_REQ, 0);
1587 /* 4.3.2.5 AUTHENTICATION REJECT is received */
1588 static int gsm48_mm_rx_auth_rej(struct osmocom_ms *ms, struct msgb *msg)
1590 struct gsm_subscriber *subscr = &ms->subscr;
1591 struct gsm48_mmlayer *mm = &ms->mmlayer;
1593 LOGP(DMM, LOGL_INFO, "AUTHENTICATION REJECT\n");
1595 stop_mm_t3212(mm); /* 4.4.2 */
1598 subscr->sim_valid = 0;
1600 /* TMSI and LAI invalid */
1601 subscr->lai_valid = 0;
1602 subscr->tmsi_valid = 0;
1604 /* key is invalid */
1605 subscr->key_seq = 7;
1608 new_sim_ustate(subscr, GSM_SIM_U3_ROAMING_NA);
1611 sim: delete tmsi, lai
1612 sim: delete key seq number
1613 sim: set update status
1616 /* abort IMSI detach procedure */
1617 if (mm->state == GSM48_MM_ST_IMSI_DETACH_INIT) {
1619 struct gsm48_rr_hdr *nrrh;
1621 /* abort RR connection */
1622 nmsg = gsm48_rr_msgb_alloc(GSM48_RR_ABORT_REQ);
1625 nrrh = (struct gsm48_rr_hdr *) msgb_put(nmsg, sizeof(*nrrh));
1626 nrrh->cause = GSM48_RR_CAUSE_NORMAL;
1627 gsm48_rr_downmsg(ms, nmsg);
1629 /* CS process will trigger: return to MM IDLE / No SIM */
1636 /* 4.3.3.1 IDENTITY REQUEST is received */
1637 static int gsm48_mm_rx_id_req(struct osmocom_ms *ms, struct msgb *msg)
1639 struct gsm_subscriber *subscr = &ms->subscr;
1640 struct gsm48_hdr *gh = msgb_l3(msg);
1641 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1644 if (payload_len < 1) {
1645 LOGP(DMM, LOGL_NOTICE, "Short read of IDENTITY REQUEST message "
1651 mi_type = *gh->data;
1652 LOGP(DMM, LOGL_INFO, "IDENTITY REQUEST (mi_type %d)\n", mi_type);
1654 /* check if request can be fulfilled */
1655 if (!subscr->sim_valid && mi_type != GSM_MI_TYPE_IMEI
1656 && mi_type != GSM_MI_TYPE_IMEISV) {
1657 LOGP(DMM, LOGL_INFO, "IDENTITY REQUEST without SIM\n");
1658 return gsm48_mm_tx_mm_status(ms,
1659 GSM48_REJECT_MSG_NOT_COMPATIBLE);
1661 if (mi_type == GSM_MI_TYPE_TMSI && !subscr->tmsi_valid) {
1662 LOGP(DMM, LOGL_INFO, "IDENTITY REQUEST of TMSI, but we have no "
1664 return gsm48_mm_tx_mm_status(ms,
1665 GSM48_REJECT_MSG_NOT_COMPATIBLE);
1668 return gsm48_mm_tx_id_rsp(ms, mi_type);
1671 /* send IDENTITY RESPONSE message */
1672 static int gsm48_mm_tx_id_rsp(struct osmocom_ms *ms, uint8_t mi_type)
1675 struct gsm48_hdr *ngh;
1678 LOGP(DMM, LOGL_INFO, "IDENTITY RESPONSE\n");
1680 nmsg = gsm48_l3_msgb_alloc();
1683 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
1685 ngh->proto_discr = GSM48_PDISC_MM;
1686 ngh->msg_type = GSM48_MT_MM_ID_RESP;
1689 gsm48_encode_mi(buf, nmsg, ms, mi_type);
1691 /* push RR header and send down */
1692 return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_DATA_REQ, 0);
1695 /* 4.3.4.1 sending IMSI DETACH INDICATION message */
1696 static int gsm48_mm_tx_imsi_detach(struct osmocom_ms *ms, int rr_prim)
1698 struct gsm_subscriber *subscr = &ms->subscr;
1699 struct gsm_support *sup = &ms->support;
1700 struct gsm48_rrlayer *rr = &ms->rrlayer;
1702 struct gsm48_hdr *ngh;
1705 struct gsm48_classmark1 cm;
1708 LOGP(DMM, LOGL_INFO, "IMSI DETACH INDICATION\n");
1710 nmsg = gsm48_l3_msgb_alloc();
1713 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
1715 ngh->proto_discr = GSM48_PDISC_MM;
1716 ngh->msg_type = GSM48_MT_MM_IMSI_DETACH_IND;
1719 if (rr->cd_now.arfcn >= 512 && rr->cd_now.arfcn <= 885)
1720 pwr_lev = sup->pwr_lev_1800;
1722 pwr_lev = sup->pwr_lev_900;
1723 gsm48_encode_classmark1(&cm, sup->rev_lev, sup->es_ind, sup->a5_1,
1725 msgb_v_put(nmsg, *((uint8_t *)&cm));
1727 if (subscr->tmsi_valid) /* have TMSI ? */
1728 gsm48_encode_mi(buf, nmsg, ms, GSM_MI_TYPE_TMSI);
1730 gsm48_encode_mi(buf, nmsg, ms, GSM_MI_TYPE_IMSI);
1732 /* push RR header and send down */
1733 return gsm48_mm_to_rr(ms, nmsg, rr_prim, RR_EST_CAUSE_OTHER_SDCCH);
1736 /* detach has ended */
1737 static int gsm48_mm_imsi_detach_end(struct osmocom_ms *ms, struct msgb *msg)
1739 struct gsm48_mmlayer *mm = &ms->mmlayer;
1740 struct gsm_subscriber *subscr = &ms->subscr;
1743 LOGP(DMM, LOGL_INFO, "IMSI has been detached.\n");
1745 /* stop IMSI detach timer (if running) */
1755 subscr->sim_valid = 0;
1757 /* wait for RR idle and then power off when IMSI is detached */
1758 if (mm->power_off) {
1759 if (mm->state == GSM48_MM_ST_MM_IDLE) {
1763 mm->power_off_idle = 1;
1768 /* send SIM remove event to gsm322 */
1769 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SIM_REMOVE);
1772 gsm322_plmn_sendmsg(ms, nmsg);
1774 /* CS process will trigger return to MM IDLE / No SIM */
1778 /* start an IMSI detach in MM IDLE */
1779 static int gsm48_mm_imsi_detach_start(struct osmocom_ms *ms, struct msgb *msg)
1781 struct gsm_subscriber *subscr = &ms->subscr;
1782 struct gsm48_mmlayer *mm = &ms->mmlayer;
1783 struct gsm48_sysinfo *s = &ms->cellsel.sel_si;
1785 /* we may silently finish IMSI detach */
1786 if (!s->att_allowed || !subscr->imsi_attached) {
1787 LOGP(DMM, LOGL_INFO, "IMSI detach not required.\n");
1789 return gsm48_mm_imsi_detach_end(ms, msg);
1791 LOGP(DMM, LOGL_INFO, "IMSI detach started (MM IDLE)\n");
1793 new_mm_state(mm, GSM48_MM_ST_WAIT_RR_CONN_IMSI_D, 0);
1795 /* establish RR and send IMSI detach */
1796 return gsm48_mm_tx_imsi_detach(ms, GSM48_RR_EST_REQ);
1799 /* IMSI detach has been sent, wait for RR release */
1800 static int gsm48_mm_imsi_detach_sent(struct osmocom_ms *ms, struct msgb *msg)
1802 struct gsm48_mmlayer *mm = &ms->mmlayer;
1804 /* start T3220 (4.3.4.1) */
1807 LOGP(DMM, LOGL_INFO, "IMSI detach started (Wait for RR release)\n");
1809 new_mm_state(mm, GSM48_MM_ST_IMSI_DETACH_INIT, 0);
1814 /* release MM connection and proceed with IMSI detach */
1815 static int gsm48_mm_imsi_detach_release(struct osmocom_ms *ms, struct msgb *msg)
1817 struct gsm_subscriber *subscr = &ms->subscr;
1818 struct gsm48_mmlayer *mm = &ms->mmlayer;
1819 struct gsm48_sysinfo *s = &ms->cellsel.sel_si;
1821 /* stop MM connection timer */
1824 /* release all connections */
1825 gsm48_mm_release_mm_conn(ms, 1, 16, 0);
1827 /* wait for release of RR */
1828 if (!s->att_allowed || !subscr->imsi_attached) {
1829 LOGP(DMM, LOGL_INFO, "IMSI detach not required.\n");
1830 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
1833 if (mm->power_off) {
1841 /* send IMSI detach */
1842 gsm48_mm_tx_imsi_detach(ms, GSM48_RR_DATA_REQ);
1844 /* go to sent state */
1845 return gsm48_mm_imsi_detach_sent(ms, msg);
1848 /* ignore ongoing IMSI detach */
1849 static int gsm48_mm_imsi_detach_ignore(struct osmocom_ms *ms, struct msgb *msg)
1854 /* delay until state change (and then retry) */
1855 static int gsm48_mm_imsi_detach_delay(struct osmocom_ms *ms, struct msgb *msg)
1857 struct gsm48_mmlayer *mm = &ms->mmlayer;
1859 LOGP(DMM, LOGL_INFO, "IMSI detach delayed.\n");
1861 /* remember to detach later */
1862 mm->delay_detach = 1;
1867 /* 4.3.5.2 ABORT is received */
1868 static int gsm48_mm_rx_abort(struct osmocom_ms *ms, struct msgb *msg)
1870 struct gsm48_mmlayer *mm = &ms->mmlayer;
1871 struct gsm_subscriber *subscr = &ms->subscr;
1872 struct gsm48_hdr *gh = msgb_l3(msg);
1873 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1874 uint8_t reject_cause;
1876 if (payload_len < 1) {
1877 LOGP(DMM, LOGL_NOTICE, "Short read of ABORT message error.\n");
1881 reject_cause = *gh->data;
1883 if (llist_empty(&mm->mm_conn)) {
1884 LOGP(DMM, LOGL_NOTICE, "ABORT (cause #%d) while no MM "
1885 "connection is established.\n", reject_cause);
1886 return gsm48_mm_tx_mm_status(ms,
1887 GSM48_REJECT_MSG_NOT_COMPATIBLE);
1889 LOGP(DMM, LOGL_NOTICE, "ABORT (cause #%d) while MM connection "
1890 "is established.\n", reject_cause);
1891 /* stop MM connection timer */
1894 gsm48_mm_release_mm_conn(ms, 1, 16, 0);
1897 if (reject_cause == GSM48_REJECT_ILLEGAL_ME) {
1899 subscr->sim_valid = 0;
1901 /* TMSI and LAI invalid */
1902 subscr->lai_valid = 0;
1903 subscr->tmsi_valid = 0;
1905 /* key is invalid */
1906 subscr->key_seq = 7;
1909 new_sim_ustate(subscr, GSM_SIM_U3_ROAMING_NA);
1912 sim: delete tmsi, lai
1913 sim: delete key seq number
1914 sim: apply update state
1917 /* CS process will trigger: return to MM IDLE / No SIM */
1924 /* 4.3.6.2 MM INFORMATION is received */
1925 static int gsm48_mm_rx_info(struct osmocom_ms *ms, struct msgb *msg)
1927 struct gsm48_mmlayer *mm = &ms->mmlayer;
1928 struct gsm48_hdr *gh = msgb_l3(msg);
1929 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1930 struct tlv_parsed tp;
1932 if (payload_len < 0) {
1933 LOGP(DMM, LOGL_NOTICE, "Short read of MM INFORMATION message "
1937 tlv_parse(&tp, &gsm48_mm_att_tlvdef, gh->data, payload_len, 0, 0);
1940 if (TLVP_PRESENT(&tp, GSM48_IE_NAME_LONG)) {
1941 decode_network_name(mm->name_long, sizeof(mm->name_long),
1942 TLVP_VAL(&tp, GSM48_IE_NAME_LONG)-1);
1945 if (TLVP_PRESENT(&tp, GSM48_IE_NAME_SHORT)) {
1946 decode_network_name(mm->name_short, sizeof(mm->name_short),
1947 TLVP_VAL(&tp, GSM48_IE_NAME_SHORT)-1);
1954 * process handlers for Location Update + IMSI attach (MM specific procedures)
1957 /* 4.4.2 received sysinfo change event */
1958 static int gsm48_mm_sysinfo(struct osmocom_ms *ms, struct msgb *msg)
1960 struct gsm48_mmlayer *mm = &ms->mmlayer;
1961 struct gsm48_sysinfo *s = &ms->cellsel.sel_si;
1963 /* t3212 not changed in these states */
1964 if (mm->state == GSM48_MM_ST_MM_IDLE
1965 && (mm->substate == GSM48_MM_SST_NO_CELL_AVAIL
1966 || mm->substate == GSM48_MM_SST_LIMITED_SERVICE
1967 || mm->substate == GSM48_MM_SST_PLMN_SEARCH
1968 || mm->substate == GSM48_MM_SST_PLMN_SEARCH_NORMAL))
1971 /* new periodic location update timer timeout */
1972 if (s->t3212 && s->t3212 != mm->t3212_value) {
1973 if (bsc_timer_pending(&mm->t3212)) {
1975 struct timeval current_time;
1978 gettimeofday(¤t_time, NULL);
1979 t = mm->t3212.timeout.tv_sec - current_time.tv_sec;
1982 LOGP(DMM, LOGL_INFO, "New T3212 while timer is running "
1983 "(value %d rest %d)\n", s->t3212, t);
1985 /* rest time modulo given value */
1986 mm->t3212.timeout.tv_sec = current_time.tv_sec
1989 uint32_t rand = random();
1991 LOGP(DMM, LOGL_INFO, "New T3212 while timer is not "
1992 "running (value %d)\n", s->t3212);
1994 /* value between 0 and given value */
1995 start_mm_t3212(mm, rand % (s->t3212 + 1));
1997 mm->t3212_value = s->t3212;
2003 /* 4.4.4.1 (re)start location update
2005 * this function is called by
2006 * - normal location update
2007 * - periodic location update
2008 * - imsi attach (normal loc. upd. function)
2009 * - retry timers (T3211 and T3213)
2011 static int gsm48_mm_loc_upd(struct osmocom_ms *ms, struct msgb *msg)
2013 struct gsm48_mmlayer *mm = &ms->mmlayer;
2014 struct gsm322_cellsel *cs = &ms->cellsel;
2015 struct gsm48_sysinfo *s = &cs->sel_si;
2016 struct gsm_subscriber *subscr = &ms->subscr;
2020 /* (re)start only if we still require location update */
2021 if (!mm->lupd_pending) {
2022 LOGP(DMM, LOGL_INFO, "No loc. upd. pending.\n");
2026 /* must camp normally */
2027 if (cs->state != GSM322_C3_CAMPED_NORMALLY) {
2028 LOGP(DMM, LOGL_INFO, "Loc. upd. not camping normally.\n");
2029 msg_type = GSM322_EVENT_REG_FAILED;
2031 LOGP(DSUM, LOGL_INFO, "Location update not possible\n");
2032 mm->lupd_pending = 0;
2033 /* send message to PLMN search process */
2034 nmsg = gsm322_msgb_alloc(msg_type);
2037 gsm322_plmn_sendmsg(ms, nmsg);
2041 /* if LAI is forbidden, don't start */
2042 if (gsm_subscr_is_forbidden_plmn(subscr, cs->sel_mcc, cs->sel_mnc)) {
2043 LOGP(DMM, LOGL_INFO, "Loc. upd. not allowed PLMN.\n");
2044 msg_type = GSM322_EVENT_ROAMING_NA;
2047 if (gsm322_is_forbidden_la(ms, cs->sel_mcc,
2048 cs->sel_mnc, cs->sel_lac)) {
2049 LOGP(DMM, LOGL_INFO, "Loc. upd. not allowed LA.\n");
2050 msg_type = GSM322_EVENT_ROAMING_NA;
2054 /* 4.4.4.9 if cell is barred, don't start */
2055 if ((!subscr->acc_barr && s->cell_barr)
2056 || (!subscr->acc_barr && !((subscr->acc_class & 0xfbff) &
2057 (s->class_barr ^ 0xffff)))) {
2058 LOGP(DMM, LOGL_INFO, "Loc. upd. no access.\n");
2059 msg_type = GSM322_EVENT_ROAMING_NA;
2063 mm->lupd_mcc = cs->sel_mcc;
2064 mm->lupd_mnc = cs->sel_mnc;
2065 mm->lupd_lac = cs->sel_lac;
2067 LOGP(DSUM, LOGL_INFO, "Perform location update (MCC %s, MNC %s "
2068 "LAC 0x%04x)\n", gsm_print_mcc(mm->lupd_mcc),
2069 gsm_print_mnc(mm->lupd_mnc), mm->lupd_lac);
2071 return gsm48_mm_tx_loc_upd_req(ms);
2074 /* initiate a normal location update / imsi attach */
2075 static int gsm48_mm_loc_upd_normal(struct osmocom_ms *ms, struct msgb *msg)
2077 struct gsm48_mmlayer *mm = &ms->mmlayer;
2078 struct gsm_subscriber *subscr = &ms->subscr;
2079 struct gsm322_cellsel *cs = &ms->cellsel;
2080 struct gsm48_sysinfo *s = &cs->sel_si;
2083 /* in case we already have a location update going on */
2084 if (mm->lupd_pending) {
2085 LOGP(DMM, LOGL_INFO, "Loc. upd. already pending.\n");
2090 /* no location update, if limited service */
2091 if (cs->state != GSM322_C3_CAMPED_NORMALLY) {
2092 LOGP(DMM, LOGL_INFO, "Loc. upd. not allowed.\n");
2094 /* send message to PLMN search process */
2095 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_FAILED);
2098 gsm322_plmn_sendmsg(ms, nmsg);
2103 /* if location update is not required */
2104 if (subscr->ustate == GSM_SIM_U1_UPDATED
2106 && cs->sel_mcc == subscr->lai_mcc
2107 && cs->sel_mnc == subscr->lai_mnc
2108 && cs->sel_lac == subscr->lai_lac
2109 && (subscr->imsi_attached
2110 || !s->att_allowed)) {
2111 LOGP(DMM, LOGL_INFO, "Loc. upd. not required.\n");
2112 subscr->imsi_attached = 1;
2114 /* send message to PLMN search process */
2115 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_SUCCESS);
2118 gsm322_plmn_sendmsg(ms, nmsg);
2123 /* 4.4.3 is attachment required? */
2124 if (subscr->ustate == GSM_SIM_U1_UPDATED
2126 && cs->sel_mcc == subscr->lai_mcc
2127 && cs->sel_mnc == subscr->lai_mnc
2128 && cs->sel_lac == subscr->lai_lac
2129 && !subscr->imsi_attached
2130 && s->att_allowed) {
2131 /* do location update for IMSI attach */
2132 LOGP(DMM, LOGL_INFO, "Do Loc. upd. for IMSI attach.\n");
2135 /* do normal location update */
2136 LOGP(DMM, LOGL_INFO, "Do normal Loc. upd.\n");
2140 /* start location update */
2141 mm->lupd_attempt = 0;
2142 mm->lupd_pending = 1;
2143 mm->lupd_ra_failure = 0;
2145 return gsm48_mm_loc_upd(ms, msg);
2148 /* initiate a periodic location update */
2149 static int gsm48_mm_loc_upd_periodic(struct osmocom_ms *ms, struct msgb *msg)
2151 struct gsm48_mmlayer *mm = &ms->mmlayer;
2153 /* in case we already have a location update going on */
2154 if (mm->lupd_pending) {
2155 LOGP(DMM, LOGL_INFO, "Loc. upd. already pending.\n");
2159 /* start periodic location update */
2161 mm->lupd_pending = 1;
2162 mm->lupd_ra_failure = 0;
2164 return gsm48_mm_loc_upd(ms, msg);
2167 /* ignore location update */
2168 static int gsm48_mm_loc_upd_ignore(struct osmocom_ms *ms, struct msgb *msg)
2173 /* 9.2.15 send LOCATION UPDATING REQUEST message */
2174 static int gsm48_mm_tx_loc_upd_req(struct osmocom_ms *ms)
2176 struct gsm48_mmlayer *mm = &ms->mmlayer;
2177 struct gsm_support *sup = &ms->support;
2178 struct gsm_subscriber *subscr = &ms->subscr;
2179 struct gsm48_rrlayer *rr = &ms->rrlayer;
2181 struct gsm48_hdr *ngh;
2182 struct gsm48_loc_upd_req *nlu; /* NOTE: mi_len is part of struct */
2186 LOGP(DMM, LOGL_INFO, "LOCATION UPDATING REQUEST\n");
2188 nmsg = gsm48_l3_msgb_alloc();
2191 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
2192 nlu = (struct gsm48_loc_upd_req *)msgb_put(nmsg, sizeof(*nlu));
2194 ngh->proto_discr = GSM48_PDISC_MM;
2195 ngh->msg_type = GSM48_MT_MM_LOC_UPD_REQUEST;
2197 /* location updating type */
2198 nlu->type = mm->lupd_type;
2200 nlu->key_seq = subscr->key_seq;
2201 /* LAI (use last SIM stored LAI) */
2202 gsm48_generate_lai(&nlu->lai,
2203 subscr->lai_mcc, subscr->lai_mnc, subscr->lai_lac);
2205 if (rr->cd_now.arfcn >= 512 && rr->cd_now.arfcn <= 885)
2206 pwr_lev = sup->pwr_lev_1800;
2208 pwr_lev = sup->pwr_lev_900;
2209 gsm48_encode_classmark1(&nlu->classmark1, sup->rev_lev, sup->es_ind,
2210 sup->a5_1, pwr_lev);
2212 if (subscr->tmsi_valid) /* have TMSI ? */
2213 gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_TMSI);
2215 gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_IMSI);
2216 msgb_put(nmsg, buf[1]); /* length is part of nlu */
2217 memcpy(&nlu->mi_len, buf + 1, 1 + buf[1]);
2219 new_mm_state(mm, GSM48_MM_ST_WAIT_RR_CONN_LUPD, 0);
2221 /* push RR header and send down */
2222 return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_EST_REQ, RR_EST_CAUSE_LOC_UPD);
2225 /* 4.4.4.1 RR is esablised during location update */
2226 static int gsm48_mm_est_loc_upd(struct osmocom_ms *ms, struct msgb *msg)
2228 struct gsm48_mmlayer *mm = &ms->mmlayer;
2230 /* start location update timer */
2233 new_mm_state(mm, GSM48_MM_ST_LOC_UPD_INIT, 0);
2238 /* 4.4.4.6 LOCATION UPDATING ACCEPT is received */
2239 static int gsm48_mm_rx_loc_upd_acc(struct osmocom_ms *ms, struct msgb *msg)
2241 struct gsm48_mmlayer *mm = &ms->mmlayer;
2242 struct gsm_subscriber *subscr = &ms->subscr;
2243 struct gsm48_hdr *gh = msgb_l3(msg);
2244 struct gsm48_loc_area_id *lai = (struct gsm48_loc_area_id *) gh->data;
2245 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2246 struct tlv_parsed tp;
2249 if (payload_len < sizeof(struct gsm48_loc_area_id)) {
2251 LOGP(DMM, LOGL_NOTICE, "Short read of LOCATION UPDATING ACCEPT "
2252 "message error.\n");
2255 tlv_parse(&tp, &gsm48_mm_att_tlvdef,
2256 gh->data + sizeof(struct gsm48_loc_area_id),
2257 payload_len - sizeof(struct gsm48_loc_area_id), 0, 0);
2259 /* update has finished */
2260 mm->lupd_pending = 0;
2262 /* RA was successfull */
2263 mm->lupd_ra_failure = 0;
2265 /* stop periodic location updating timer */
2266 stop_mm_t3212(mm); /* 4.4.2 */
2269 subscr->lai_valid = 1;
2270 gsm48_decode_lai(lai, &subscr->lai_mcc, &subscr->lai_mnc,
2273 /* stop location update timer */
2276 /* reset attempt counter */
2277 mm->lupd_attempt = 0;
2279 /* mark SIM as attached */
2280 subscr->imsi_attached = 1;
2282 /* set the status in the sim to updated */
2283 new_sim_ustate(subscr, GSM_SIM_U1_UPDATED);
2285 sim: apply update state
2288 /* set last registered PLMN */
2289 subscr->plmn_valid = 1;
2290 subscr->plmn_mcc = subscr->lai_mcc;
2291 subscr->plmn_mnc = subscr->lai_mnc;
2296 LOGP(DSUM, LOGL_INFO, "Location update accepted\n");
2297 LOGP(DMM, LOGL_INFO, "LOCATION UPDATING ACCEPT (mcc %s mnc %s "
2298 "lac 0x%04x)\n", gsm_print_mcc(subscr->lai_mcc),
2299 gsm_print_mnc(subscr->lai_mnc), subscr->lai_lac);
2301 /* remove LA from forbidden list */
2302 gsm322_del_forbidden_la(ms, subscr->lai_mcc, subscr->lai_mnc,
2306 if (TLVP_PRESENT(&tp, GSM48_IE_MOBILE_ID)) {
2311 mi = TLVP_VAL(&tp, GSM48_IE_MOBILE_ID)-1;
2314 mi_type = mi[1] & GSM_MI_TYPE_MASK;
2316 case GSM_MI_TYPE_TMSI:
2317 if (payload_len + sizeof(struct gsm48_loc_area_id) < 6
2320 memcpy(&tmsi, mi+2, 4);
2321 subscr->tmsi = ntohl(tmsi);
2322 subscr->tmsi_valid = 1;
2323 LOGP(DMM, LOGL_INFO, "got TMSI 0x%08x (%u)\n",
2324 subscr->tmsi, subscr->tmsi);
2329 case GSM_MI_TYPE_IMSI:
2330 LOGP(DMM, LOGL_INFO, "TMSI removed\n");
2331 subscr->tmsi_valid = 0;
2335 /* send TMSI REALLOCATION COMPLETE */
2336 gsm48_mm_tx_tmsi_reall_cpl(ms);
2339 LOGP(DMM, LOGL_NOTICE, "TMSI reallocation with unknown "
2340 "MI type %d.\n", mi_type);
2344 /* send message to PLMN search process */
2345 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_SUCCESS);
2348 gsm322_plmn_sendmsg(ms, nmsg);
2350 /* follow on proceed */
2351 if (TLVP_PRESENT(&tp, GSM48_IE_MOBILE_ID))
2352 LOGP(DMM, LOGL_NOTICE, "follow-on proceed not supported.\n");
2354 /* start RR release timer */
2357 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
2362 /* 4.4.4.7 LOCATION UPDATING REJECT is received */
2363 static int gsm48_mm_rx_loc_upd_rej(struct osmocom_ms *ms, struct msgb *msg)
2365 struct gsm48_mmlayer *mm = &ms->mmlayer;
2366 struct gsm48_hdr *gh = msgb_l3(msg);
2367 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2369 if (payload_len < 1) {
2370 LOGP(DMM, LOGL_NOTICE, "Short read of LOCATION UPDATING REJECT "
2371 "message error.\n");
2375 /* RA was successfull */
2376 mm->lupd_ra_failure = 0;
2378 /* stop periodic location updating timer */
2379 stop_mm_t3212(mm); /* 4.4.2 */
2381 /* stop location update timer */
2384 /* store until RR is released */
2385 mm->lupd_rej_cause = *gh->data;
2387 /* start RR release timer */
2390 new_mm_state(mm, GSM48_MM_ST_LOC_UPD_REJ, 0);
2395 /* 4.4.4.7 RR is released after location update reject */
2396 static int gsm48_mm_rel_loc_upd_rej(struct osmocom_ms *ms, struct msgb *msg)
2398 struct gsm48_mmlayer *mm = &ms->mmlayer;
2399 struct gsm_subscriber *subscr = &ms->subscr;
2401 struct gsm322_msg *ngm;
2403 LOGP(DMM, LOGL_INFO, "Loc. upd. rejected (cause %d)\n",
2404 mm->lupd_rej_cause);
2406 /* stop RR release timer */
2410 switch (mm->lupd_rej_cause) {
2411 case GSM48_REJECT_IMSI_UNKNOWN_IN_HLR:
2412 case GSM48_REJECT_ILLEGAL_MS:
2413 case GSM48_REJECT_ILLEGAL_ME:
2414 /* reset attempt counter */
2415 mm->lupd_attempt = 0;
2418 subscr->sim_valid = 0;
2421 case GSM48_REJECT_PLMN_NOT_ALLOWED:
2422 case GSM48_REJECT_LOC_NOT_ALLOWED:
2423 case GSM48_REJECT_ROAMING_NOT_ALLOWED:
2424 /* TMSI and LAI invalid */
2425 subscr->lai_valid = 0;
2426 subscr->tmsi_valid = 0;
2428 /* key is invalid */
2429 subscr->key_seq = 7;
2432 new_sim_ustate(subscr, GSM_SIM_U3_ROAMING_NA);
2434 sim: delete tmsi, lai
2435 sim: delete key seq number
2436 sim: apply update state
2438 /* update has finished */
2439 mm->lupd_pending = 0;
2442 /* send event to PLMN search process */
2443 switch(mm->lupd_rej_cause) {
2444 case GSM48_REJECT_ROAMING_NOT_ALLOWED:
2445 nmsg = gsm322_msgb_alloc(GSM322_EVENT_ROAMING_NA);
2447 case GSM48_REJECT_IMSI_UNKNOWN_IN_HLR:
2448 case GSM48_REJECT_ILLEGAL_MS:
2449 case GSM48_REJECT_ILLEGAL_ME:
2450 nmsg = gsm322_msgb_alloc(GSM322_EVENT_INVALID_SIM);
2453 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_FAILED);
2457 ngm = (struct gsm322_msg *)nmsg->data;
2458 ngm->reject = mm->lupd_rej_cause;
2459 gsm322_plmn_sendmsg(ms, nmsg);
2461 /* forbidden list */
2462 switch (mm->lupd_rej_cause) {
2463 case GSM48_REJECT_IMSI_UNKNOWN_IN_HLR:
2464 LOGP(DSUM, LOGL_INFO, "Location update failed (IMSI unknown "
2467 case GSM48_REJECT_ILLEGAL_MS:
2468 LOGP(DSUM, LOGL_INFO, "Location update failed (Illegal MS)\n");
2470 case GSM48_REJECT_ILLEGAL_ME:
2471 LOGP(DSUM, LOGL_INFO, "Location update failed (Illegal ME)\n");
2473 case GSM48_REJECT_PLMN_NOT_ALLOWED:
2474 gsm_subscr_add_forbidden_plmn(subscr, mm->lupd_mcc,
2475 mm->lupd_mnc, mm->lupd_rej_cause);
2476 LOGP(DSUM, LOGL_INFO, "Location update failed (PLMN not "
2479 case GSM48_REJECT_LOC_NOT_ALLOWED:
2480 case GSM48_REJECT_ROAMING_NOT_ALLOWED:
2481 gsm322_add_forbidden_la(ms, mm->lupd_mcc, mm->lupd_mnc,
2482 mm->lupd_lac, mm->lupd_rej_cause);
2483 LOGP(DSUM, LOGL_INFO, "Location update failed (LAI not "
2487 /* 4.4.4.9 continue with failure handling */
2488 return gsm48_mm_loc_upd_failed(ms, NULL);
2491 /* CS proc triggers: return to IDLE, case 13 is also handled there */
2495 /* 4.2.2 delay a location update */
2496 static int gsm48_mm_loc_upd_delay_per(struct osmocom_ms *ms, struct msgb *msg)
2498 struct gsm48_mmlayer *mm = &ms->mmlayer;
2500 LOGP(DMM, LOGL_INFO, "Schedule a pending periodic loc. upd.\n");
2501 mm->lupd_periodic = 1;
2506 /* delay a location update retry */
2507 static int gsm48_mm_loc_upd_delay_retry(struct osmocom_ms *ms, struct msgb *msg)
2509 struct gsm48_mmlayer *mm = &ms->mmlayer;
2511 LOGP(DMM, LOGL_INFO, "Schedule a pending periodic loc. upd.\n");
2517 /* process failues as described in the lower part of 4.4.4.9 */
2518 static int gsm48_mm_loc_upd_failed(struct osmocom_ms *ms, struct msgb *msg)
2520 struct gsm48_mmlayer *mm = &ms->mmlayer;
2521 struct gsm_subscriber *subscr = &ms->subscr;
2523 LOGP(DSUM, LOGL_INFO, "Location update failed\n");
2525 /* stop location update timer, if running */
2528 if (subscr->ustate == GSM_SIM_U1_UPDATED
2529 && mm->lupd_mcc == subscr->lai_mcc
2530 && mm->lupd_mnc == subscr->lai_mnc
2531 && mm->lupd_lac == subscr->lai_lac) {
2532 if (mm->lupd_attempt < 4) {
2533 LOGP(DSUM, LOGL_INFO, "Try location update later\n");
2534 LOGP(DMM, LOGL_INFO, "Loc. upd. failed, retry #%d\n",
2537 /* start update retry timer */
2540 /* CS process will trigger: return to MM IDLE */
2543 LOGP(DMM, LOGL_INFO, "Loc. upd. failed too often.\n");
2546 /* TMSI and LAI invalid */
2547 subscr->lai_valid = 0;
2548 subscr->tmsi_valid = 0;
2550 /* key is invalid */
2551 subscr->key_seq = 7;
2554 new_sim_ustate(subscr, GSM_SIM_U2_NOT_UPDATED);
2557 sim: delete tmsi, lai
2558 sim: delete key seq number
2559 sim: set update status
2562 /* start update retry timer (RR connection is released) */
2563 if (mm->lupd_attempt < 4) {
2564 mm->start_t3211 = 1;
2565 LOGP(DSUM, LOGL_INFO, "Try location update later\n");
2568 /* CS process will trigger: return to MM IDLE */
2572 /* abort a location update due to radio failure or release */
2573 static int gsm48_mm_rel_loc_upd_abort(struct osmocom_ms *ms, struct msgb *msg)
2575 struct gsm48_mmlayer *mm = &ms->mmlayer;
2576 struct gsm48_rr_hdr *rrh = (struct gsm48_rr_hdr *)msg->data;
2578 /* stop RR release timer */
2581 if (rrh->msg_type == GSM48_RR_REL_IND) {
2582 LOGP(DMM, LOGL_INFO, "RR link released after loc. upd.\n");
2584 /* continue with failure handling */
2585 return gsm48_mm_loc_upd_failed(ms, NULL);
2588 LOGP(DMM, LOGL_INFO, "Loc. upd. aborted by radio (cause #%d)\n",
2591 /* random access failure, but not two successive failures */
2592 if (rrh->cause == RR_REL_CAUSE_RA_FAILURE && !mm->lupd_ra_failure) {
2593 mm->lupd_ra_failure = 1;
2595 /* start RA failure timer */
2601 /* RA was successfull or sent twice */
2602 mm->lupd_ra_failure = 0;
2604 /* continue with failure handling */
2605 return gsm48_mm_loc_upd_failed(ms, NULL);
2608 /* location update has timed out */
2609 static int gsm48_mm_loc_upd_timeout(struct osmocom_ms *ms, struct msgb *msg)
2612 struct gsm48_rr_hdr *nrrh;
2614 /* abort RR connection */
2615 nmsg = gsm48_rr_msgb_alloc(GSM48_RR_ABORT_REQ);
2618 nrrh = (struct gsm48_rr_hdr *) msgb_put(nmsg, sizeof(*nrrh));
2619 nrrh->cause = GSM48_RR_CAUSE_ABNORMAL_TIMER;
2620 gsm48_rr_downmsg(ms, nmsg);
2622 /* continue with failure handling */
2623 return gsm48_mm_loc_upd_failed(ms, NULL);
2627 * process handlers for MM connections
2630 /* cm reestablish request message from upper layer */
2631 static int gsm48_mm_tx_cm_serv_req(struct osmocom_ms *ms, int rr_prim,
2632 uint8_t cause, uint8_t cm_serv)
2634 struct gsm_subscriber *subscr = &ms->subscr;
2635 struct gsm_settings *settings = &ms->settings;
2637 struct gsm48_hdr *ngh;
2638 struct gsm48_service_request *nsr; /* NOTE: includes MI length */
2642 LOGP(DMM, LOGL_INFO, "CM SERVICE REQUEST (cause %d)\n", cause);
2644 nmsg = gsm48_l3_msgb_alloc();
2647 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
2648 nsr = (struct gsm48_service_request *)msgb_put(nmsg, sizeof(*nsr));
2649 cm2lv = (uint8_t *)&nsr->classmark;
2651 ngh->proto_discr = GSM48_PDISC_MM;
2652 ngh->msg_type = GSM48_MT_MM_CM_SERV_REQ;
2655 nsr->cm_service_type = cm_serv;
2656 nsr->cipher_key_seq = subscr->key_seq;
2658 cm2lv[0] = sizeof(struct gsm48_classmark2);
2659 gsm48_rr_enc_cm2(ms, (struct gsm48_classmark2 *)(cm2lv + 1));
2661 if (!subscr->sim_valid) { /* have no SIM ? */
2662 if (settings->emergency_imsi[0])
2663 gsm48_generate_mid_from_imsi(buf,
2664 settings->emergency_imsi);
2666 gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_IMEI);
2667 } else if (subscr->tmsi_valid) /* have TMSI ? */
2668 gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_TMSI);
2670 gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_IMSI);
2671 msgb_put(nmsg, buf[1]); /* length is part of nsr */
2672 memcpy(&nsr->mi_len, buf + 1, 1 + buf[1]);
2673 /* prio is optional for eMLPP */
2675 /* push RR header and send down */
2676 return gsm48_mm_to_rr(ms, nmsg, rr_prim, cause);
2679 /* cm service abort message from upper layer
2680 * NOTE: T3240 is started by the calling function
2682 static int gsm48_mm_tx_cm_service_abort(struct osmocom_ms *ms)
2685 struct gsm48_hdr *ngh;
2687 LOGP(DMM, LOGL_INFO, "CM SERVICE ABORT\n");
2689 nmsg = gsm48_l3_msgb_alloc();
2692 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
2694 ngh->proto_discr = GSM48_PDISC_MM;
2695 ngh->msg_type = GSM48_MT_MM_CM_SERV_ABORT;
2697 /* push RR header and send down */
2698 return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_DATA_REQ, 0);
2701 /* cm service acknowledge is received from lower layer */
2702 static int gsm48_mm_rx_cm_service_acc(struct osmocom_ms *ms, struct msgb *msg)
2704 struct gsm48_mmlayer *mm = &ms->mmlayer;
2706 /* stop MM connection timer */
2709 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
2711 return gsm48_mm_conn_go_dedic(ms);
2714 /* 9.2.6 CM SERVICE REJECT message received */
2715 static int gsm48_mm_rx_cm_service_rej(struct osmocom_ms *ms, struct msgb *msg)
2717 struct gsm48_mmlayer *mm = &ms->mmlayer;
2718 struct gsm_subscriber *subscr = &ms->subscr;
2719 struct gsm48_hdr *gh = msgb_l3(msg);
2720 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2721 uint8_t abort_any = 0;
2722 uint8_t reject_cause;
2724 if (payload_len < 1) {
2725 LOGP(DMM, LOGL_NOTICE, "Short read of cm service reject "
2726 "message error.\n");
2731 reject_cause = *gh->data;
2733 LOGP(DMM, LOGL_INFO, "CM SERVICE REJECT (cause %d)\n", reject_cause);
2735 /* stop MM connection timer */
2738 /* selection action on cause value */
2739 switch (reject_cause) {
2740 case GSM48_REJECT_IMSI_UNKNOWN_IN_VLR:
2741 case GSM48_REJECT_ILLEGAL_ME:
2744 /* TMSI and LAI invalid */
2745 subscr->lai_valid = 0;
2746 subscr->tmsi_valid = 0;
2748 /* key is invalid */
2749 subscr->key_seq = 7;
2752 new_sim_ustate(subscr, GSM_SIM_U2_NOT_UPDATED);
2755 sim: delete tmsi, lai
2756 sim: delete key seq number
2757 sim: set update status
2760 /* change to WAIT_NETWORK_CMD state impied by abort_any == 1 */
2762 if (reject_cause == GSM48_REJECT_ILLEGAL_ME)
2763 subscr->sim_valid = 0;
2767 /* state implied by the number of remaining connections */
2771 /* release MM connection(s) */
2772 gsm48_mm_release_mm_conn(ms, abort_any, 16, 0);
2774 /* state depends on the existance of remaining MM connections */
2775 if (llist_empty(&mm->mm_conn))
2776 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
2778 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
2783 /* initiate an MM connection 4.5.1.1
2785 * this function is called when:
2786 * - no RR connection exists
2787 * - an RR connection exists, but this is the first MM connection
2788 * - an RR connection exists, and there are already MM connection(s)
2790 static int gsm48_mm_init_mm(struct osmocom_ms *ms, struct msgb *msg,
2793 struct gsm48_mmlayer *mm = &ms->mmlayer;
2794 struct gsm_subscriber *subscr = &ms->subscr;
2795 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
2796 int msg_type = mmh->msg_type;
2798 uint8_t cause = 0, cm_serv = 0, proto = 0;
2800 struct gsm48_mmxx_hdr *nmmh;
2801 struct gsm48_mm_conn *conn, *conn_found = NULL;
2803 /* reset loc. upd. counter on CM service request */
2804 mm->lupd_attempt = 0;
2806 /* find if there is already a pending connection */
2807 llist_for_each_entry(conn, &mm->mm_conn, list) {
2808 if (conn->state == GSM48_MMXX_ST_CONN_PEND) {
2814 /* if pending connection */
2816 LOGP(DMM, LOGL_INFO, "Init MM Connection, but already have "
2817 "pending MM Connection.\n");
2822 case GSM48_MMCC_EST_REQ:
2823 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_REL_IND,
2824 mmh->ref, mmh->transaction_id);
2826 case GSM48_MMSS_EST_REQ:
2827 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSS_REL_IND,
2828 mmh->ref, mmh->transaction_id);
2830 case GSM48_MMSMS_EST_REQ:
2831 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSMS_REL_IND,
2832 mmh->ref, mmh->transaction_id);
2837 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
2838 nmmh->cause = cause;
2839 gsm48_mmxx_upmsg(ms, nmsg);
2843 /* in case of an emergency setup */
2844 if (msg_type == GSM48_MMCC_EST_REQ && mmh->emergency)
2847 /* if sim is not updated */
2848 if (!emergency && subscr->ustate != GSM_SIM_U1_UPDATED) {
2849 LOGP(DMM, LOGL_INFO, "Init MM Connection, but SIM not "
2855 if (mm->state == GSM48_MM_ST_MM_IDLE) {
2856 /* current MM idle state */
2857 switch (mm->substate) {
2858 case GSM48_MM_SST_NORMAL_SERVICE:
2859 case GSM48_MM_SST_PLMN_SEARCH_NORMAL:
2860 LOGP(DMM, LOGL_INFO, "Init MM Connection.\n");
2861 break; /* allow when normal */
2862 case GSM48_MM_SST_LOC_UPD_NEEDED:
2863 case GSM48_MM_SST_ATTEMPT_UPDATE:
2864 /* store mm request if attempting to update */
2866 LOGP(DMM, LOGL_INFO, "Init MM Connection, but "
2867 "attempting to update.\n");
2870 /* TODO: implement delay and start loc upd. */
2874 /* reject if not emergency */
2876 LOGP(DMM, LOGL_INFO, "Init MM Connection, not "
2877 "in normal state.\n");
2884 LOGP(DMM, LOGL_INFO, "Init another MM Connection.\n");
2886 /* set cause, service, proto */
2888 case GSM48_MMCC_EST_REQ:
2890 cause = RR_EST_CAUSE_EMERGENCY;
2891 cm_serv = GSM48_CMSERV_EMERGENCY;
2893 cause = RR_EST_CAUSE_ORIG_TCHF;
2894 cm_serv = GSM48_CMSERV_MO_CALL_PACKET;
2896 proto = GSM48_PDISC_CC;
2898 case GSM48_MMSS_EST_REQ:
2899 cause = RR_EST_CAUSE_OTHER_SDCCH;
2900 cm_serv = GSM48_CMSERV_SUP_SERV;
2901 proto = GSM48_PDISC_NC_SS;
2903 case GSM48_MMSMS_EST_REQ:
2904 cause = RR_EST_CAUSE_OTHER_SDCCH;
2905 cm_serv = GSM48_CMSERV_SMS;
2906 proto = GSM48_PDISC_SMS;
2910 /* create MM connection instance */
2911 conn = mm_conn_new(mm, proto, mmh->transaction_id, mmh->ref);
2915 new_conn_state(conn, GSM48_MMXX_ST_CONN_PEND);
2917 /* send CM SERVICE REQUEST */
2919 return gsm48_mm_tx_cm_serv_req(ms, rr_prim, cause, cm_serv);
2924 /* 4.5.1.1 a) MM connection request triggers RR connection */
2925 static int gsm48_mm_init_mm_no_rr(struct osmocom_ms *ms, struct msgb *msg)
2927 struct gsm48_mmlayer *mm = &ms->mmlayer;
2930 /* start MM connection by requesting RR connection */
2931 rc = gsm48_mm_init_mm(ms, msg, GSM48_RR_EST_REQ);
2935 new_mm_state(mm, GSM48_MM_ST_WAIT_RR_CONN_MM_CON, 0);
2940 /* 4.5.1.1 a) RR is esablised during mm connection, wait for CM accepted */
2941 static int gsm48_mm_est_mm_con(struct osmocom_ms *ms, struct msgb *msg)
2943 struct gsm48_mmlayer *mm = &ms->mmlayer;
2945 /* 4.5.1.7 if there is no more MM connection */
2946 if (llist_empty(&mm->mm_conn)) {
2947 LOGP(DMM, LOGL_INFO, "MM Connection, are already gone.\n");
2949 /* start RR release timer */
2952 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
2955 return gsm48_mm_tx_cm_service_abort(ms);
2958 /* start MM connection timer */
2961 new_mm_state(mm, GSM48_MM_ST_WAIT_OUT_MM_CONN, 0);
2966 /* 4.5.1.1 b) MM connection request on existing RR connection */
2967 static int gsm48_mm_init_mm_first(struct osmocom_ms *ms, struct msgb *msg)
2969 struct gsm48_mmlayer *mm = &ms->mmlayer;
2972 /* start MM connection by sending data */
2973 rc = gsm48_mm_init_mm(ms, msg, GSM48_RR_DATA_REQ);
2977 /* stop "RR connection release not allowed" timer */
2980 /* start MM connection timer */
2983 new_mm_state(mm, GSM48_MM_ST_WAIT_OUT_MM_CONN, 0);
2988 /* 4.5.1.1 b) another MM connection request on existing RR connection */
2989 static int gsm48_mm_init_mm_more(struct osmocom_ms *ms, struct msgb *msg)
2991 struct gsm48_mmlayer *mm = &ms->mmlayer;
2994 /* start MM connection by sending data */
2995 rc = gsm48_mm_init_mm(ms, msg, GSM48_RR_DATA_REQ);
2999 /* start MM connection timer */
3002 new_mm_state(mm, GSM48_MM_ST_WAIT_ADD_OUT_MM_CON, 0);
3007 /* 4.5.1.1 b) delay on WAIT FOR NETWORK COMMAND state */
3008 static int gsm48_mm_init_mm_wait(struct osmocom_ms *ms, struct msgb *msg)
3011 gsm48_mm_init_mm_reject(ms, msg);
3013 this requires handling when leaving this state...
3015 struct gsm48_mmlayer *mm = &ms->mmlayer;
3018 /* just create the MM connection in pending state */
3019 rc = gsm48_mm_init_mm(ms, msg, 0);
3023 /* start MM connection timer */
3026 new_mm_state(mm, GSM48_MM_ST_WAIT_ADD_OUT_MM_CON, 0);
3032 /* initiate an mm connection other cases */
3033 static int gsm48_mm_init_mm_reject(struct osmocom_ms *ms, struct msgb *msg)
3035 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3036 int msg_type = mmh->msg_type;
3038 struct gsm48_mmxx_hdr *nmmh;
3043 case GSM48_MMCC_EST_REQ:
3044 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_REL_IND, mmh->ref,
3045 mmh->transaction_id);
3047 case GSM48_MMSS_EST_REQ:
3048 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSS_REL_IND, mmh->ref,
3049 mmh->transaction_id);
3051 case GSM48_MMSMS_EST_REQ:
3052 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSMS_REL_IND, mmh->ref,
3053 mmh->transaction_id);
3058 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
3060 gsm48_mmxx_upmsg(ms, nmsg);
3065 /* accepting pending connection, got dedicated mode
3067 * this function is called:
3068 * - when ciphering command is received
3069 * - when cm service is accepted
3071 static int gsm48_mm_conn_go_dedic(struct osmocom_ms *ms)
3073 struct gsm48_mmlayer *mm = &ms->mmlayer;
3074 struct gsm48_mm_conn *conn, *conn_found = NULL;
3076 struct gsm48_mmxx_hdr *nmmh;
3078 /* the first and only pending connection is the recent requested */
3079 llist_for_each_entry(conn, &mm->mm_conn, list) {
3080 if (conn->state == GSM48_MMXX_ST_CONN_PEND) {
3086 /* if no pending connection (anymore) */
3088 LOGP(DMM, LOGL_INFO, "No pending MM Connection.\n");
3093 new_conn_state(conn, GSM48_MMXX_ST_DEDICATED);
3095 /* send establishment confirm */
3097 switch(conn_found->protocol) {
3098 case GSM48_PDISC_CC:
3099 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_EST_CNF, conn->ref,
3100 conn->transaction_id);
3102 case GSM48_PDISC_NC_SS:
3103 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSS_EST_CNF, conn->ref,
3104 conn->transaction_id);
3106 case GSM48_PDISC_SMS:
3107 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSMS_EST_CNF, conn->ref,
3108 conn->transaction_id);
3113 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
3115 gsm48_mmxx_upmsg(ms, nmsg);
3120 /* a RR-SYNC-IND is received during MM connection establishment */
3121 static int gsm48_mm_sync_ind_wait(struct osmocom_ms *ms, struct msgb *msg)
3123 struct gsm48_mmlayer *mm = &ms->mmlayer;
3125 /* stop MM connection timer */
3128 return gsm48_mm_conn_go_dedic(ms);
3131 /* a RR-SYNC-IND is received during MM connection active */
3132 static int gsm48_mm_sync_ind_active(struct osmocom_ms *ms, struct msgb *msg)
3134 struct gsm48_mmlayer *mm = &ms->mmlayer;
3135 struct gsm48_mm_conn *conn;
3137 struct gsm48_mmxx_hdr *nmmh;
3139 /* stop MM connection timer */
3142 /* broadcast all MMCC connection(s) */
3143 llist_for_each_entry(conn, &mm->mm_conn, list) {
3144 /* send MMCC-SYNC-IND */
3146 switch(conn->protocol) {
3147 case GSM48_PDISC_CC:
3148 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_SYNC_IND,
3149 conn->ref, conn->transaction_id);
3153 continue; /* skip if not of CC type */
3154 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
3156 /* copy L3 message */
3157 nmsg->l3h = msgb_put(nmsg, msgb_l3len(msg));
3158 memcpy(nmsg->l3h, msg->l3h, msgb_l3len(msg));
3159 gsm48_mmxx_upmsg(ms, nmsg);
3165 /* 4.5.1.2 RR abort/release is received during MM connection establishment */
3166 static int gsm48_mm_abort_mm_con(struct osmocom_ms *ms, struct msgb *msg)
3168 struct gsm48_mmlayer *mm = &ms->mmlayer;
3169 struct gsm48_rr_hdr *rrh = (struct gsm48_rr_hdr *)msg->data;
3172 /* stop RR release timer */
3175 /* this conversion is not of any standard */
3176 switch(rrh->cause) {
3177 case RR_REL_CAUSE_NOT_AUTHORIZED:
3178 case RR_REL_CAUSE_EMERGENCY_ONLY:
3179 case RR_REL_CAUSE_TRY_LATER:
3182 case RR_REL_CAUSE_NORMAL:
3189 /* stop MM connection timer */
3192 /* release all connections */
3193 gsm48_mm_release_mm_conn(ms, 1, cause, 1);
3195 /* no RR connection, so we return to MM IDLE */
3196 if (mm->state == GSM48_MM_ST_WAIT_RR_CONN_MM_CON)
3197 return gsm48_mm_return_idle(ms, NULL);
3199 /* CS process will trigger: return to MM IDLE */
3203 /* 4.5.1.2 timeout is received during MM connection establishment */
3204 static int gsm48_mm_timeout_mm_con(struct osmocom_ms *ms, struct msgb *msg)
3206 struct gsm48_mmlayer *mm = &ms->mmlayer;
3208 /* release pending connection */
3209 gsm48_mm_release_mm_conn(ms, 0, 102, 0);
3211 /* state depends on the existance of remaining MM connections */
3212 if (llist_empty(&mm->mm_conn)) {
3213 /* start RR release timer */
3216 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
3218 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
3223 /* respond to paging */
3224 static int gsm48_mm_est(struct osmocom_ms *ms, struct msgb *msg)
3226 struct gsm48_mmlayer *mm = &ms->mmlayer;
3228 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
3234 static int gsm48_mm_data(struct osmocom_ms *ms, struct msgb *msg)
3236 struct gsm48_mmlayer *mm = &ms->mmlayer;
3237 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3238 struct gsm48_mm_conn *conn;
3239 int msg_type = mmh->msg_type;
3241 /* get connection, if not exist (anymore), release */
3242 conn = mm_conn_by_ref(mm, mmh->ref);
3244 LOGP(DMM, LOGL_INFO, "MMXX_DATA_REQ with unknown (already "
3245 "released) ref=%x, sending MMXX_REL_IND\n", mmh->ref);
3246 switch(msg_type & GSM48_MMXX_MASK) {
3247 case GSM48_MMCC_CLASS:
3248 mmh->msg_type = GSM48_MMCC_REL_IND;
3250 case GSM48_MMSS_CLASS:
3251 mmh->msg_type = GSM48_MMSS_REL_IND;
3253 case GSM48_MMSMS_CLASS:
3254 mmh->msg_type = GSM48_MMSMS_REL_IND;
3259 /* mirror message with REL_IND + cause */
3260 return gsm48_mmxx_upmsg(ms, msg);
3263 /* pull MM header */
3264 msgb_pull(msg, sizeof(struct gsm48_mmxx_hdr));
3266 /* push RR header and send down */
3267 return gsm48_mm_to_rr(ms, msg, GSM48_RR_DATA_REQ, 0);
3270 /* release of MM connection (active state) */
3271 static int gsm48_mm_release_active(struct osmocom_ms *ms, struct msgb *msg)
3273 struct gsm48_mmlayer *mm = &ms->mmlayer;
3274 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3275 struct gsm48_mm_conn *conn;
3277 /* get connection, if not exist (anymore), release */
3278 conn = mm_conn_by_ref(mm, mmh->ref);
3282 /* state depends on the existance of remaining MM connections */
3283 if (llist_empty(&mm->mm_conn)) {
3284 /* start RR release timer */
3287 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
3289 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
3294 /* release of MM connection (wait for additional state) */
3295 static int gsm48_mm_release_wait_add(struct osmocom_ms *ms, struct msgb *msg)
3297 struct gsm48_mmlayer *mm = &ms->mmlayer;
3298 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3299 struct gsm48_mm_conn *conn;
3301 /* get connection, if not exist (anymore), release */
3302 conn = mm_conn_by_ref(mm, mmh->ref);
3309 /* release of MM connection (wait for active state) */
3310 static int gsm48_mm_release_wait_active(struct osmocom_ms *ms, struct msgb *msg)
3312 struct gsm48_mmlayer *mm = &ms->mmlayer;
3313 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3314 struct gsm48_mm_conn *conn;
3316 /* get connection, if not exist (anymore), release */
3317 conn = mm_conn_by_ref(mm, mmh->ref);
3321 /* 4.5.1.7 if there is no MM connection during wait for active state */
3322 if (llist_empty(&mm->mm_conn)) {
3323 LOGP(DMM, LOGL_INFO, "No MM Connection during 'wait for "
3324 "active' state.\n");
3326 /* start RR release timer */
3329 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
3332 return gsm48_mm_tx_cm_service_abort(ms);
3338 /* release of MM connection (wait for RR state) */
3339 static int gsm48_mm_release_wait_rr(struct osmocom_ms *ms, struct msgb *msg)
3341 struct gsm48_mmlayer *mm = &ms->mmlayer;
3342 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3343 struct gsm48_mm_conn *conn;
3345 /* get connection, if not exist (anymore), release */
3346 conn = mm_conn_by_ref(mm, mmh->ref);
3350 /* later, if RR connection is established, the CM SERIVE ABORT
3351 * message will be sent
3356 /* abort RR connection (due to T3240) */
3357 static int gsm48_mm_abort_rr(struct osmocom_ms *ms, struct msgb *msg)
3360 struct gsm48_rr_hdr *nrrh;
3362 /* send abort to RR */
3363 nmsg = gsm48_rr_msgb_alloc(GSM48_RR_ABORT_REQ);
3366 nrrh = (struct gsm48_rr_hdr *) msgb_put(nmsg, sizeof(*nrrh));
3367 nrrh->cause = GSM48_RR_CAUSE_ABNORMAL_TIMER;
3368 gsm48_rr_downmsg(ms, nmsg);
3370 /* CS process will trigger: return to MM IDLE / No SIM */
3378 /* RR is released in other states */
3379 static int gsm48_mm_rel_other(struct osmocom_ms *ms, struct msgb *msg)
3381 struct gsm48_mmlayer *mm = &ms->mmlayer;
3383 /* stop RR release timer (if running) */
3386 /* CS process will trigger: return to MM IDLE */
3394 /* state trasitions for MMxx-SAP messages from upper layers */
3395 static struct downstate {
3399 int (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3400 } downstatelist[] = {
3401 /* 4.2.2.1 Normal service */
3402 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3403 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3405 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3406 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_no_rr},
3408 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3409 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_no_rr},
3411 /* 4.2.2.2 Attempt to update / Loc. Upd. needed */
3412 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE) |
3413 SBIT(GSM48_MM_SST_LOC_UPD_NEEDED),
3414 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr}, /* emergency only */
3416 /* 4.2.2.3 Limited service */
3417 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3418 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3420 /* 4.2.2.4 No IMSI */
3421 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NO_IMSI),
3422 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3424 /* 4.2.2.5 PLMN search, normal service */
3425 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3426 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3428 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3429 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_no_rr},
3431 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3432 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_no_rr},
3434 /* 4.2.2.6 PLMN search */
3435 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH),
3436 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3438 /* 4.5.1.1 MM Connection (EST) */
3439 {SBIT(GSM48_MM_ST_RR_CONN_RELEASE_NA), ALL_STATES,
3440 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_first},
3442 {SBIT(GSM48_MM_ST_RR_CONN_RELEASE_NA), ALL_STATES,
3443 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_first},
3445 {SBIT(GSM48_MM_ST_RR_CONN_RELEASE_NA), ALL_STATES,
3446 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_first},
3448 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3449 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_more},
3451 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3452 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_more},
3454 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3455 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_more},
3457 {SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD), ALL_STATES,
3458 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_wait},
3460 {SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD), ALL_STATES,
3461 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_wait},
3463 {SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD), ALL_STATES,
3464 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_wait},
3466 {ALL_STATES, ALL_STATES,
3467 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_reject},
3469 {ALL_STATES, ALL_STATES,
3470 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_reject},
3472 {ALL_STATES, ALL_STATES,
3473 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_reject},
3475 /* 4.5.2.1 MM Connection (DATA) */
3476 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3477 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3478 GSM48_MMCC_DATA_REQ, gsm48_mm_data},
3480 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3481 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3482 GSM48_MMSS_DATA_REQ, gsm48_mm_data},
3484 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3485 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3486 GSM48_MMSMS_DATA_REQ, gsm48_mm_data},
3488 /* 4.5.2.1 MM Connection (REL) */
3489 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3490 GSM48_MMCC_REL_REQ, gsm48_mm_release_active},
3492 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3493 GSM48_MMSS_REL_REQ, gsm48_mm_release_active},
3495 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3496 GSM48_MMSMS_REL_REQ, gsm48_mm_release_active},
3498 {SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3499 GSM48_MMCC_REL_REQ, gsm48_mm_release_wait_add},
3501 {SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3502 GSM48_MMSS_REL_REQ, gsm48_mm_release_wait_add},
3504 {SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3505 GSM48_MMSMS_REL_REQ, gsm48_mm_release_wait_add},
3507 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN), ALL_STATES,
3508 GSM48_MMCC_REL_REQ, gsm48_mm_release_wait_active},
3510 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN), ALL_STATES,
3511 GSM48_MMSS_REL_REQ, gsm48_mm_release_wait_active},
3513 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN), ALL_STATES,
3514 GSM48_MMSMS_REL_REQ, gsm48_mm_release_wait_active},
3516 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON), ALL_STATES,
3517 GSM48_MMCC_REL_REQ, gsm48_mm_release_wait_rr},
3519 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON), ALL_STATES,
3520 GSM48_MMSS_REL_REQ, gsm48_mm_release_wait_rr},
3522 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON), ALL_STATES,
3523 GSM48_MMSMS_REL_REQ, gsm48_mm_release_wait_rr},
3527 (sizeof(downstatelist) / sizeof(struct downstate))
3529 int gsm48_mmxx_downmsg(struct osmocom_ms *ms, struct msgb *msg)
3531 struct gsm48_mmlayer *mm = &ms->mmlayer;
3532 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3533 int msg_type = mmh->msg_type;
3534 struct gsm48_mm_conn *conn;
3537 /* keep up to date with the transaction ID */
3538 conn = mm_conn_by_ref(mm, mmh->ref);
3540 conn->transaction_id = mmh->transaction_id;
3542 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' event in state %s\n",
3543 ms->name, get_mmxx_name(msg_type),
3544 gsm48_mm_state_names[mm->state]);
3545 if (mm->state == GSM48_MM_ST_MM_IDLE)
3546 LOGP(DMM, LOGL_INFO, "-> substate %s\n",
3547 gsm48_mm_substate_names[mm->substate]);
3548 LOGP(DMM, LOGL_INFO, "-> callref %x, transaction_id %d\n",
3549 mmh->ref, mmh->transaction_id);
3551 /* Find function for current state and message */
3552 for (i = 0; i < DOWNSLLEN; i++)
3553 if ((msg_type == downstatelist[i].type)
3554 && ((1 << mm->state) & downstatelist[i].states)
3555 && ((1 << mm->substate) & downstatelist[i].substates))
3557 if (i == DOWNSLLEN) {
3558 LOGP(DMM, LOGL_NOTICE, "Message unhandled at this state.\n");
3563 rc = downstatelist[i].rout(ms, msg);
3565 if (downstatelist[i].rout != gsm48_mm_data)
3571 /* state trasitions for radio ressource messages (lower layer) */
3572 static struct rrdatastate {
3575 int (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3576 } rrdatastatelist[] = {
3578 {SBIT(GSM48_MM_ST_MM_IDLE),
3579 GSM48_RR_EST_IND, gsm48_mm_est},
3582 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_IMSI_D), /* 4.3.4.4 */
3583 GSM48_RR_EST_CNF, gsm48_mm_imsi_detach_sent},
3585 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_IMSI_D), /* 4.3.4.4 (unsuc.) */
3586 GSM48_RR_REL_IND, gsm48_mm_imsi_detach_end},
3587 /* also this may happen if SABM is ackwnowledged with DISC */
3589 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_IMSI_D), /* 4.3.4.4 (lost) */
3590 GSM48_RR_ABORT_IND, gsm48_mm_imsi_detach_end},
3592 {SBIT(GSM48_MM_ST_IMSI_DETACH_INIT), /* 4.3.4.4 (unsuc.) */
3593 GSM48_RR_REL_IND, gsm48_mm_imsi_detach_end},
3595 {SBIT(GSM48_MM_ST_IMSI_DETACH_INIT), /* 4.3.4.4 (lost) */
3596 GSM48_RR_ABORT_IND, gsm48_mm_imsi_detach_end},
3598 /* location update */
3599 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_LUPD), /* 4.4.4.1 */
3600 GSM48_RR_EST_CNF, gsm48_mm_est_loc_upd},
3602 {SBIT(GSM48_MM_ST_LOC_UPD_INIT) |
3603 SBIT(GSM48_MM_ST_WAIT_RR_CONN_LUPD), /* 4.4.4.9 */
3604 GSM48_RR_REL_IND, gsm48_mm_rel_loc_upd_abort},
3606 {SBIT(GSM48_MM_ST_LOC_UPD_INIT) |
3607 SBIT(GSM48_MM_ST_WAIT_RR_CONN_LUPD), /* 4.4.4.9 */
3608 GSM48_RR_ABORT_IND, gsm48_mm_rel_loc_upd_abort},
3610 {SBIT(GSM48_MM_ST_LOC_UPD_REJ), /* 4.4.4.7 */
3611 GSM48_RR_REL_IND, gsm48_mm_rel_loc_upd_rej},
3613 {SBIT(GSM48_MM_ST_LOC_UPD_REJ), /* 4.4.4.7 */
3614 GSM48_RR_ABORT_IND, gsm48_mm_rel_loc_upd_rej},
3616 /* MM connection (EST) */
3617 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON), /* 4.5.1.1 */
3618 GSM48_RR_EST_CNF, gsm48_mm_est_mm_con},
3620 /* MM connection (DATA) */
3622 GSM48_RR_DATA_IND, gsm48_mm_data_ind},
3624 /* MM connection (SYNC) */
3625 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN) |
3626 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), /* 4.5.1.1 */
3627 GSM48_RR_SYNC_IND, gsm48_mm_sync_ind_wait},
3629 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE),
3630 GSM48_RR_SYNC_IND, gsm48_mm_sync_ind_active},
3632 /* MM connection (REL/ABORT) */
3633 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON) |
3634 SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN) |
3635 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), /* 4.5.1.2 */
3636 GSM48_RR_REL_IND, gsm48_mm_abort_mm_con},
3638 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON) |
3639 SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN) |
3640 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), /* 4.5.1.2 */
3641 GSM48_RR_ABORT_IND, gsm48_mm_abort_mm_con},
3643 /* MM connection (REL/ABORT with re-establishment possibility) */
3644 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), /* not supported */
3645 GSM48_RR_REL_IND, gsm48_mm_abort_mm_con},
3647 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3648 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), /* not supported */
3649 GSM48_RR_ABORT_IND, gsm48_mm_abort_mm_con},
3651 /* other (also wait for network command) */
3653 GSM48_RR_REL_IND, gsm48_mm_rel_other},
3656 GSM48_RR_ABORT_IND, gsm48_mm_rel_other},
3659 #define RRDATASLLEN \
3660 (sizeof(rrdatastatelist) / sizeof(struct rrdatastate))
3662 static int gsm48_rcv_rr(struct osmocom_ms *ms, struct msgb *msg)
3664 struct gsm48_mmlayer *mm = &ms->mmlayer;
3665 struct gsm48_rr_hdr *rrh = (struct gsm48_rr_hdr *)msg->data;
3666 int msg_type = rrh->msg_type;
3669 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' from RR in state %s\n",
3670 ms->name, get_rr_name(msg_type),
3671 gsm48_mm_state_names[mm->state]);
3673 /* find function for current state and message */
3674 for (i = 0; i < RRDATASLLEN; i++)
3675 if ((msg_type == rrdatastatelist[i].type)
3676 && ((1 << mm->state) & rrdatastatelist[i].states))
3678 if (i == RRDATASLLEN) {
3679 LOGP(DMM, LOGL_NOTICE, "Message unhandled at this state.\n");
3684 rc = rrdatastatelist[i].rout(ms, msg);
3686 if (rrdatastatelist[i].rout != gsm48_mm_data_ind)
3692 /* state trasitions for mobile managemnt messages (lower layer) */
3693 static struct mmdatastate {
3696 int (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3697 } mmdatastatelist[] = {
3698 {ALL_STATES, /* 4.3.1.2 */
3699 GSM48_MT_MM_TMSI_REALL_CMD, gsm48_mm_rx_tmsi_realloc_cmd},
3701 {ALL_STATES, /* 4.3.2.2 */
3702 GSM48_MT_MM_AUTH_REQ, gsm48_mm_rx_auth_req},
3704 {ALL_STATES, /* 4.3.2.5 */
3705 GSM48_MT_MM_AUTH_REJ, gsm48_mm_rx_auth_rej},
3707 {ALL_STATES, /* 4.3.3.2 */
3708 GSM48_MT_MM_ID_REQ, gsm48_mm_rx_id_req},
3710 {ALL_STATES, /* 4.3.5.2 */
3711 GSM48_MT_MM_ABORT, gsm48_mm_rx_abort},
3713 {ALL_STATES, /* 4.3.6.2 */
3714 GSM48_MT_MM_INFO, gsm48_mm_rx_info},
3716 {SBIT(GSM48_MM_ST_LOC_UPD_INIT), /* 4.4.4.6 */
3717 GSM48_MT_MM_LOC_UPD_ACCEPT, gsm48_mm_rx_loc_upd_acc},
3719 {SBIT(GSM48_MM_ST_LOC_UPD_INIT), /* 4.4.4.7 */
3720 GSM48_MT_MM_LOC_UPD_REJECT, gsm48_mm_rx_loc_upd_rej},
3722 {ALL_STATES, /* 4.5.1.1 */
3723 GSM48_MT_MM_CM_SERV_ACC, gsm48_mm_rx_cm_service_acc},
3725 {ALL_STATES, /* 4.5.1.1 */
3726 GSM48_MT_MM_CM_SERV_REJ, gsm48_mm_rx_cm_service_rej},
3729 #define MMDATASLLEN \
3730 (sizeof(mmdatastatelist) / sizeof(struct mmdatastate))
3732 static int gsm48_mm_data_ind(struct osmocom_ms *ms, struct msgb *msg)
3734 struct gsm48_mmlayer *mm = &ms->mmlayer;
3735 struct gsm48_hdr *gh = msgb_l3(msg);
3736 uint8_t pdisc = gh->proto_discr & 0x0f;
3737 uint8_t msg_type = gh->msg_type & 0xbf;
3738 struct gsm48_mmxx_hdr *mmh;
3739 int msg_supported = 0; /* determine, if message is supported at all */
3740 int rr_prim = -1, rr_est = -1; /* no prim set */
3745 if (msg_type == GSM48_MT_MM_NULL)
3748 if (mm->state == GSM48_MM_ST_IMSI_DETACH_INIT) {
3749 LOGP(DMM, LOGL_NOTICE, "DATA IND ignored during IMSI "
3753 /* pull the RR header */
3754 msgb_pull(msg, sizeof(struct gsm48_rr_hdr));
3756 /* create transaction (if not exists) and push message */
3758 case GSM48_PDISC_CC:
3759 rr_prim = GSM48_MMCC_DATA_IND;
3760 rr_est = GSM48_MMCC_EST_IND;
3763 case GSM48_PDISC_NC_SS:
3764 rr_prim = GSM48_MMSS_DATA_IND;
3765 rr_est = GSM48_MMSS_EST_IND;
3767 case GSM48_PDISC_SMS:
3768 rr_prim = GSM48_MMSMS_DATA_IND;
3769 rr_est = GSM48_MMSMS_EST_IND;
3773 if (rr_prim != -1) {
3774 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4;
3776 struct gsm48_mm_conn *conn;
3778 /* find transaction, if any */
3779 conn = mm_conn_by_id(mm, pdisc, transaction_id);
3781 /* create MM connection instance */
3783 conn = mm_conn_new(mm, pdisc, transaction_id,
3790 /* push new header */
3791 msgb_push(msg, sizeof(struct gsm48_mmxx_hdr));
3792 mmh = (struct gsm48_mmxx_hdr *)msg->data;
3793 mmh->msg_type = rr_prim;
3794 mmh->ref = conn->ref;
3796 /* go MM CONN ACTIVE state */
3797 if (mm->state == GSM48_MM_ST_WAIT_NETWORK_CMD
3798 || mm->state == GSM48_MM_ST_RR_CONN_RELEASE_NA) {
3799 /* stop RR release timer */
3802 /* stop "RR connection release not allowed" timer */
3805 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
3809 /* forward message */
3811 case GSM48_PDISC_MM:
3812 skip_ind = (gh->proto_discr & 0xf0) >> 4;
3814 /* ignore if skip indicator is not B'0000' */
3817 break; /* follow the selection proceedure below */
3819 case GSM48_PDISC_CC:
3820 return gsm48_rcv_cc(ms, msg);
3823 case GSM48_PDISC_NC_SS:
3824 return gsm48_rcv_ss(ms, msg);
3826 case GSM48_PDISC_SMS:
3827 return gsm48_rcv_sms(ms, msg);
3831 LOGP(DMM, LOGL_NOTICE, "Protocol type 0x%02x unsupported.\n",
3834 return gsm48_mm_tx_mm_status(ms,
3835 GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED);
3838 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' in MM state %s\n", ms->name,
3839 get_mm_name(msg_type), gsm48_mm_state_names[mm->state]);
3841 stop_mm_t3212(mm); /* 4.4.2 */
3843 /* 11.2 re-start pending RR release timer */
3844 if (bsc_timer_pending(&mm->t3240)) {
3849 /* find function for current state and message */
3850 for (i = 0; i < MMDATASLLEN; i++) {
3851 if (msg_type == mmdatastatelist[i].type)
3853 if ((msg_type == mmdatastatelist[i].type)
3854 && ((1 << mm->state) & mmdatastatelist[i].states))
3857 if (i == MMDATASLLEN) {
3859 if (msg_supported) {
3860 LOGP(DMM, LOGL_NOTICE, "Message unhandled at this "
3862 return gsm48_mm_tx_mm_status(ms,
3863 GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE);
3865 LOGP(DMM, LOGL_NOTICE, "Message not supported.\n");
3866 return gsm48_mm_tx_mm_status(ms,
3867 GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED);
3871 rc = mmdatastatelist[i].rout(ms, msg);
3878 /* state trasitions for mobile management events */
3879 static struct eventstate {
3883 int (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3884 } eventstatelist[] = {
3885 /* 4.2.3 return to MM IDLE */
3886 {ALL_STATES - SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES,
3887 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_no_cell_found},
3889 {ALL_STATES - SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES,
3890 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_return_idle},
3892 /* 4.2.2.1 Normal service */
3893 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3894 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3896 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3897 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_loc_upd_normal}, /* change */
3899 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3900 GSM48_MM_EVENT_TIMEOUT_T3211, gsm48_mm_loc_upd},
3902 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3903 GSM48_MM_EVENT_TIMEOUT_T3213, gsm48_mm_loc_upd},
3905 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3906 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_periodic},
3908 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3909 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_start},
3911 /* 4.2.2.2 Attempt to update / Loc. upd. needed */
3912 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE) |
3913 SBIT(GSM48_MM_SST_LOC_UPD_NEEDED),
3914 GSM48_MM_EVENT_USER_PLMN_SEL, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3916 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE) |
3917 SBIT(GSM48_MM_SST_LOC_UPD_NEEDED),
3918 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3920 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE) |
3921 SBIT(GSM48_MM_SST_LOC_UPD_NEEDED),
3922 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_loc_upd_normal}, /* change */
3924 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE),
3925 GSM48_MM_EVENT_TIMEOUT_T3211, gsm48_mm_loc_upd},
3927 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE),
3928 GSM48_MM_EVENT_TIMEOUT_T3213, gsm48_mm_loc_upd},
3930 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE),
3931 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_periodic},
3933 /* 4.2.2.3 Limited service */
3934 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3935 GSM48_MM_EVENT_USER_PLMN_SEL, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3937 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3938 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3940 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3941 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_loc_upd_normal}, /* if allow. */
3943 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3944 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_delay_per}, /* 4.4.2 */
3946 /* 4.2.2.4 No IMSI */
3947 /* 4.2.2.5 PLMN search, normal service */
3948 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3949 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_no_cell_found}, /* 4.2.1.1 */
3951 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3952 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_cell_selected}, /* 4.2.1.1 */
3954 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3955 GSM48_MM_EVENT_TIMEOUT_T3211, gsm48_mm_loc_upd_delay_retry},
3957 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3958 GSM48_MM_EVENT_TIMEOUT_T3213, gsm48_mm_loc_upd_delay_retry},
3960 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3961 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_delay_per}, /* 4.4.2 */
3963 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3964 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_start},
3966 /* 4.2.2.6 PLMN search */
3967 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH),
3968 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_no_cell_found}, /* 4.2.1.1 */
3970 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH),
3971 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_cell_selected}, /* 4.2.1.1 */
3973 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH),
3974 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_delay_per}, /* 4.4.2 */
3976 /* No cell available */
3977 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NO_CELL_AVAIL),
3978 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_cell_selected}, /* 4.2.1.1 */
3980 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NO_CELL_AVAIL),
3981 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_delay_per}, /* 4.4.2 */
3983 /* IMSI detach in other cases */
3984 {SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES, /* silently detach */
3985 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_end},
3987 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN) |
3988 SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3989 SBIT(GSM48_MM_ST_PROCESS_CM_SERV_P) |
3990 SBIT(GSM48_MM_ST_WAIT_REEST) |
3991 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON) |
3992 SBIT(GSM48_MM_ST_MM_CONN_ACTIVE_VGCS) |
3993 SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD), ALL_STATES, /* we can release */
3994 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_release},
3996 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_IMSI_D) |
3997 SBIT(GSM48_MM_ST_IMSI_DETACH_INIT) |
3998 SBIT(GSM48_MM_ST_IMSI_DETACH_PEND), ALL_STATES, /* ignore */
3999 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_ignore},
4001 {ALL_STATES, ALL_STATES,
4002 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_delay},
4004 {GSM48_MM_ST_IMSI_DETACH_INIT, ALL_STATES,
4005 GSM48_MM_EVENT_TIMEOUT_T3220, gsm48_mm_imsi_detach_end},
4007 /* location update in other cases */
4008 {ALL_STATES, ALL_STATES,
4009 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_ignore},
4011 {ALL_STATES - SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES,
4012 GSM48_MM_EVENT_TIMEOUT_T3210, gsm48_mm_loc_upd_timeout},
4014 {ALL_STATES - SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES,
4015 GSM48_MM_EVENT_TIMEOUT_T3213, gsm48_mm_loc_upd_failed},
4016 /* 4.4.4.9 c) (but without retry) */
4019 {ALL_STATES, ALL_STATES,
4020 GSM48_MM_EVENT_SYSINFO, gsm48_mm_sysinfo},
4022 /* T3240 timed out */
4023 {SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD) |
4024 SBIT(GSM48_MM_ST_LOC_UPD_REJ), ALL_STATES, /* 4.4.4.8 */
4025 GSM48_MM_EVENT_TIMEOUT_T3240, gsm48_mm_abort_rr},
4027 /* T3230 timed out */
4028 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
4029 GSM48_MM_EVENT_TIMEOUT_T3230, gsm48_mm_timeout_mm_con},
4031 /* SIM reports SRES */
4032 {ALL_STATES, ALL_STATES, /* 4.3.2.2 */
4033 GSM48_MM_EVENT_AUTH_RESPONSE, gsm48_mm_tx_auth_rsp},
4036 /* change in classmark is reported */
4037 {ALL_STATES, ALL_STATES,
4038 GSM48_MM_EVENT_CLASSMARK_CHG, gsm48_mm_classm_chg},
4042 #define EVENTSLLEN \
4043 (sizeof(eventstatelist) / sizeof(struct eventstate))
4045 static int gsm48_mm_ev(struct osmocom_ms *ms, int msg_type, struct msgb *msg)
4047 struct gsm48_mmlayer *mm = &ms->mmlayer;
4050 if (mm->state == GSM48_MM_ST_MM_IDLE)
4051 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' event in state "
4052 "MM IDLE, %s\n", ms->name, get_mmevent_name(msg_type),
4053 gsm48_mm_substate_names[mm->substate]);
4055 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' event in state "
4056 "%s\n", ms->name, get_mmevent_name(msg_type),
4057 gsm48_mm_state_names[mm->state]);
4059 /* Find function for current state and message */
4060 for (i = 0; i < EVENTSLLEN; i++)
4061 if ((msg_type == eventstatelist[i].type)
4062 && ((1 << mm->state) & eventstatelist[i].states)
4063 && ((1 << mm->substate) & eventstatelist[i].substates))
4065 if (i == EVENTSLLEN) {
4066 LOGP(DMM, LOGL_NOTICE, "Message unhandled at this state.\n");
4070 rc = eventstatelist[i].rout(ms, msg);
4076 * MM Register (SIM insert and remove)
4079 /* register new SIM card and trigger attach */
4080 static int gsm48_mmr_reg_req(struct osmocom_ms *ms)
4082 struct gsm48_mmlayer *mm = &ms->mmlayer;
4085 /* schedule insertion of SIM */
4086 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SIM_INSERT);
4089 gsm322_plmn_sendmsg(ms, nmsg);
4091 /* 4.2.1.2 SIM is inserted in state NO IMSI */
4092 if (mm->state == GSM48_MM_ST_MM_IDLE
4093 && mm->substate == GSM48_MM_SST_NO_IMSI)
4094 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
4095 gsm48_mm_set_plmn_search(ms));
4100 /* trigger detach of sim card */
4101 static int gsm48_mmr_nreg_req(struct osmocom_ms *ms)
4103 struct gsm48_mmlayer *mm = &ms->mmlayer;
4106 nmsg = gsm322_msgb_alloc(GSM48_MM_EVENT_IMSI_DETACH);
4109 gsm48_mmevent_msg(mm->ms, nmsg);
4114 static int gsm48_rcv_mmr(struct osmocom_ms *ms, struct msgb *msg)
4116 struct gsm48_mmr *mmr = (struct gsm48_mmr *)msg->data;
4117 int msg_type = mmr->msg_type;
4120 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' event\n", ms->name,
4121 get_mmr_name(msg_type));
4123 case GSM48_MMR_REG_REQ:
4124 rc = gsm48_mmr_reg_req(ms);
4126 case GSM48_MMR_NREG_REQ:
4127 rc = gsm48_mmr_nreg_req(ms);
4130 LOGP(DMM, LOGL_NOTICE, "Message unhandled.\n");