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 /* current MM idle state
2856 * (implies MM IDLE state, otherwise this function is not called)
2858 switch (mm->substate) {
2859 case GSM48_MM_SST_NORMAL_SERVICE:
2860 case GSM48_MM_SST_PLMN_SEARCH_NORMAL:
2861 LOGP(DMM, LOGL_INFO, "Init MM Connection.\n");
2862 break; /* allow when normal */
2863 case GSM48_MM_SST_LOC_UPD_NEEDED:
2864 case GSM48_MM_SST_ATTEMPT_UPDATE:
2865 /* store mm request if attempting to update */
2867 LOGP(DMM, LOGL_INFO, "Init MM Connection, but "
2868 "attempting to update.\n");
2871 /* Some day implement delay and start loc upd. */
2875 /* reject if not emergency */
2877 LOGP(DMM, LOGL_INFO, "Init MM Connection, not in "
2885 /* set cause, service, proto */
2887 case GSM48_MMCC_EST_REQ:
2889 cause = RR_EST_CAUSE_EMERGENCY;
2890 cm_serv = GSM48_CMSERV_EMERGENCY;
2892 cause = RR_EST_CAUSE_ORIG_TCHF;
2893 cm_serv = GSM48_CMSERV_MO_CALL_PACKET;
2895 proto = GSM48_PDISC_CC;
2897 case GSM48_MMSS_EST_REQ:
2898 cause = RR_EST_CAUSE_OTHER_SDCCH;
2899 cm_serv = GSM48_CMSERV_SUP_SERV;
2900 proto = GSM48_PDISC_NC_SS;
2902 case GSM48_MMSMS_EST_REQ:
2903 cause = RR_EST_CAUSE_OTHER_SDCCH;
2904 cm_serv = GSM48_CMSERV_SMS;
2905 proto = GSM48_PDISC_SMS;
2909 /* create MM connection instance */
2910 conn = mm_conn_new(mm, proto, mmh->transaction_id, mmh->ref);
2914 new_conn_state(conn, GSM48_MMXX_ST_CONN_PEND);
2916 /* send CM SERVICE REQUEST */
2918 return gsm48_mm_tx_cm_serv_req(ms, rr_prim, cause, cm_serv);
2923 /* 4.5.1.1 a) MM connection request triggers RR connection */
2924 static int gsm48_mm_init_mm_no_rr(struct osmocom_ms *ms, struct msgb *msg)
2926 struct gsm48_mmlayer *mm = &ms->mmlayer;
2929 /* start MM connection by requesting RR connection */
2930 rc = gsm48_mm_init_mm(ms, msg, GSM48_RR_EST_REQ);
2934 new_mm_state(mm, GSM48_MM_ST_WAIT_RR_CONN_MM_CON, 0);
2939 /* 4.5.1.1 a) RR is esablised during mm connection, wait for CM accepted */
2940 static int gsm48_mm_est_mm_con(struct osmocom_ms *ms, struct msgb *msg)
2942 struct gsm48_mmlayer *mm = &ms->mmlayer;
2944 /* 4.5.1.7 if there is no more MM connection */
2945 if (llist_empty(&mm->mm_conn)) {
2946 LOGP(DMM, LOGL_INFO, "MM Connection, are already gone.\n");
2948 /* start RR release timer */
2951 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
2954 return gsm48_mm_tx_cm_service_abort(ms);
2957 /* start MM connection timer */
2960 new_mm_state(mm, GSM48_MM_ST_WAIT_OUT_MM_CONN, 0);
2965 /* 4.5.1.1 b) MM connection request on existing RR connection */
2966 static int gsm48_mm_init_mm_first(struct osmocom_ms *ms, struct msgb *msg)
2968 struct gsm48_mmlayer *mm = &ms->mmlayer;
2971 /* start MM connection by sending data */
2972 rc = gsm48_mm_init_mm(ms, msg, GSM48_RR_DATA_REQ);
2976 /* stop "RR connection release not allowed" timer */
2979 /* start MM connection timer */
2982 new_mm_state(mm, GSM48_MM_ST_WAIT_OUT_MM_CONN, 0);
2987 /* 4.5.1.1 b) another MM connection request on existing RR connection */
2988 static int gsm48_mm_init_mm_more(struct osmocom_ms *ms, struct msgb *msg)
2990 struct gsm48_mmlayer *mm = &ms->mmlayer;
2993 /* start MM connection by sending data */
2994 rc = gsm48_mm_init_mm(ms, msg, GSM48_RR_DATA_REQ);
2998 /* start MM connection timer */
3001 new_mm_state(mm, GSM48_MM_ST_WAIT_ADD_OUT_MM_CON, 0);
3006 /* 4.5.1.1 b) delay on WAIT FOR NETWORK COMMAND state */
3007 static int gsm48_mm_init_mm_wait(struct osmocom_ms *ms, struct msgb *msg)
3010 gsm48_mm_init_mm_reject(ms, msg);
3012 this requires handling when leaving this state...
3014 struct gsm48_mmlayer *mm = &ms->mmlayer;
3017 /* just create the MM connection in pending state */
3018 rc = gsm48_mm_init_mm(ms, msg, 0);
3022 /* start MM connection timer */
3025 new_mm_state(mm, GSM48_MM_ST_WAIT_ADD_OUT_MM_CON, 0);
3031 /* initiate an mm connection other cases */
3032 static int gsm48_mm_init_mm_reject(struct osmocom_ms *ms, struct msgb *msg)
3034 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3035 int msg_type = mmh->msg_type;
3037 struct gsm48_mmxx_hdr *nmmh;
3042 case GSM48_MMCC_EST_REQ:
3043 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_REL_IND, mmh->ref,
3044 mmh->transaction_id);
3046 case GSM48_MMSS_EST_REQ:
3047 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSS_REL_IND, mmh->ref,
3048 mmh->transaction_id);
3050 case GSM48_MMSMS_EST_REQ:
3051 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSMS_REL_IND, mmh->ref,
3052 mmh->transaction_id);
3057 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
3059 gsm48_mmxx_upmsg(ms, nmsg);
3064 /* accepting pending connection, got dedicated mode
3066 * this function is called:
3067 * - when ciphering command is received
3068 * - when cm service is accepted
3070 static int gsm48_mm_conn_go_dedic(struct osmocom_ms *ms)
3072 struct gsm48_mmlayer *mm = &ms->mmlayer;
3073 struct gsm48_mm_conn *conn, *conn_found = NULL;
3075 struct gsm48_mmxx_hdr *nmmh;
3077 /* the first and only pending connection is the recent requested */
3078 llist_for_each_entry(conn, &mm->mm_conn, list) {
3079 if (conn->state == GSM48_MMXX_ST_CONN_PEND) {
3085 /* if no pending connection (anymore) */
3087 LOGP(DMM, LOGL_INFO, "No pending MM Connection.\n");
3092 new_conn_state(conn, GSM48_MMXX_ST_DEDICATED);
3094 /* send establishment confirm */
3096 switch(conn_found->protocol) {
3097 case GSM48_PDISC_CC:
3098 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_EST_CNF, conn->ref,
3099 conn->transaction_id);
3101 case GSM48_PDISC_NC_SS:
3102 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSS_EST_CNF, conn->ref,
3103 conn->transaction_id);
3105 case GSM48_PDISC_SMS:
3106 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSMS_EST_CNF, conn->ref,
3107 conn->transaction_id);
3112 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
3114 gsm48_mmxx_upmsg(ms, nmsg);
3119 /* a RR-SYNC-IND is received during MM connection establishment */
3120 static int gsm48_mm_sync_ind_wait(struct osmocom_ms *ms, struct msgb *msg)
3122 struct gsm48_mmlayer *mm = &ms->mmlayer;
3124 /* stop MM connection timer */
3127 return gsm48_mm_conn_go_dedic(ms);
3130 /* a RR-SYNC-IND is received during MM connection active */
3131 static int gsm48_mm_sync_ind_active(struct osmocom_ms *ms, struct msgb *msg)
3133 struct gsm48_mmlayer *mm = &ms->mmlayer;
3134 struct gsm48_mm_conn *conn;
3136 struct gsm48_mmxx_hdr *nmmh;
3138 /* stop MM connection timer */
3141 /* broadcast all MMCC connection(s) */
3142 llist_for_each_entry(conn, &mm->mm_conn, list) {
3143 /* send MMCC-SYNC-IND */
3145 switch(conn->protocol) {
3146 case GSM48_PDISC_CC:
3147 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_SYNC_IND,
3148 conn->ref, conn->transaction_id);
3152 continue; /* skip if not of CC type */
3153 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
3155 /* copy L3 message */
3156 nmsg->l3h = msgb_put(nmsg, msgb_l3len(msg));
3157 memcpy(nmsg->l3h, msg->l3h, msgb_l3len(msg));
3158 gsm48_mmxx_upmsg(ms, nmsg);
3164 /* 4.5.1.2 RR abort/release is received during MM connection establishment */
3165 static int gsm48_mm_abort_mm_con(struct osmocom_ms *ms, struct msgb *msg)
3167 struct gsm48_mmlayer *mm = &ms->mmlayer;
3168 struct gsm48_rr_hdr *rrh = (struct gsm48_rr_hdr *)msg->data;
3171 /* stop RR release timer */
3174 /* this conversion is not of any standard */
3175 switch(rrh->cause) {
3176 case RR_REL_CAUSE_NOT_AUTHORIZED:
3177 case RR_REL_CAUSE_EMERGENCY_ONLY:
3178 case RR_REL_CAUSE_TRY_LATER:
3181 case RR_REL_CAUSE_NORMAL:
3188 /* stop MM connection timer */
3191 /* release all connections */
3192 gsm48_mm_release_mm_conn(ms, 1, cause, 1);
3194 /* no RR connection, so we return to MM IDLE */
3195 if (mm->state == GSM48_MM_ST_WAIT_RR_CONN_MM_CON)
3196 return gsm48_mm_return_idle(ms, NULL);
3198 /* CS process will trigger: return to MM IDLE */
3202 /* 4.5.1.2 timeout is received during MM connection establishment */
3203 static int gsm48_mm_timeout_mm_con(struct osmocom_ms *ms, struct msgb *msg)
3205 struct gsm48_mmlayer *mm = &ms->mmlayer;
3207 /* release pending connection */
3208 gsm48_mm_release_mm_conn(ms, 0, 102, 0);
3210 /* state depends on the existance of remaining MM connections */
3211 if (llist_empty(&mm->mm_conn)) {
3212 /* start RR release timer */
3215 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
3217 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
3222 /* respond to paging */
3223 static int gsm48_mm_est(struct osmocom_ms *ms, struct msgb *msg)
3225 struct gsm48_mmlayer *mm = &ms->mmlayer;
3227 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
3233 static int gsm48_mm_data(struct osmocom_ms *ms, struct msgb *msg)
3235 struct gsm48_mmlayer *mm = &ms->mmlayer;
3236 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3237 struct gsm48_mm_conn *conn;
3238 int msg_type = mmh->msg_type;
3240 /* get connection, if not exist (anymore), release */
3241 conn = mm_conn_by_ref(mm, mmh->ref);
3243 LOGP(DMM, LOGL_INFO, "MMXX_DATA_REQ with unknown (already "
3244 "released) ref=%x, sending MMXX_REL_IND\n", mmh->ref);
3245 switch(msg_type & GSM48_MMXX_MASK) {
3246 case GSM48_MMCC_CLASS:
3247 mmh->msg_type = GSM48_MMCC_REL_IND;
3249 case GSM48_MMSS_CLASS:
3250 mmh->msg_type = GSM48_MMSS_REL_IND;
3252 case GSM48_MMSMS_CLASS:
3253 mmh->msg_type = GSM48_MMSMS_REL_IND;
3258 /* mirror message with REL_IND + cause */
3259 return gsm48_mmxx_upmsg(ms, msg);
3262 /* pull MM header */
3263 msgb_pull(msg, sizeof(struct gsm48_mmxx_hdr));
3265 /* push RR header and send down */
3266 return gsm48_mm_to_rr(ms, msg, GSM48_RR_DATA_REQ, 0);
3269 /* release of MM connection (active state) */
3270 static int gsm48_mm_release_active(struct osmocom_ms *ms, struct msgb *msg)
3272 struct gsm48_mmlayer *mm = &ms->mmlayer;
3273 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3274 struct gsm48_mm_conn *conn;
3276 /* get connection, if not exist (anymore), release */
3277 conn = mm_conn_by_ref(mm, mmh->ref);
3281 /* state depends on the existance of remaining MM connections */
3282 if (llist_empty(&mm->mm_conn)) {
3283 /* start RR release timer */
3286 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
3288 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
3293 /* release of MM connection (wait for additional state) */
3294 static int gsm48_mm_release_wait_add(struct osmocom_ms *ms, struct msgb *msg)
3296 struct gsm48_mmlayer *mm = &ms->mmlayer;
3297 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3298 struct gsm48_mm_conn *conn;
3300 /* get connection, if not exist (anymore), release */
3301 conn = mm_conn_by_ref(mm, mmh->ref);
3308 /* release of MM connection (wait for active state) */
3309 static int gsm48_mm_release_wait_active(struct osmocom_ms *ms, struct msgb *msg)
3311 struct gsm48_mmlayer *mm = &ms->mmlayer;
3312 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3313 struct gsm48_mm_conn *conn;
3315 /* get connection, if not exist (anymore), release */
3316 conn = mm_conn_by_ref(mm, mmh->ref);
3320 /* 4.5.1.7 if there is no MM connection during wait for active state */
3321 if (llist_empty(&mm->mm_conn)) {
3322 LOGP(DMM, LOGL_INFO, "No MM Connection during 'wait for "
3323 "active' state.\n");
3325 /* start RR release timer */
3328 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
3331 return gsm48_mm_tx_cm_service_abort(ms);
3337 /* release of MM connection (wait for RR state) */
3338 static int gsm48_mm_release_wait_rr(struct osmocom_ms *ms, struct msgb *msg)
3340 struct gsm48_mmlayer *mm = &ms->mmlayer;
3341 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3342 struct gsm48_mm_conn *conn;
3344 /* get connection, if not exist (anymore), release */
3345 conn = mm_conn_by_ref(mm, mmh->ref);
3349 /* later, if RR connection is established, the CM SERIVE ABORT
3350 * message will be sent
3355 /* abort RR connection (due to T3240) */
3356 static int gsm48_mm_abort_rr(struct osmocom_ms *ms, struct msgb *msg)
3359 struct gsm48_rr_hdr *nrrh;
3361 /* send abort to RR */
3362 nmsg = gsm48_rr_msgb_alloc(GSM48_RR_ABORT_REQ);
3365 nrrh = (struct gsm48_rr_hdr *) msgb_put(nmsg, sizeof(*nrrh));
3366 nrrh->cause = GSM48_RR_CAUSE_ABNORMAL_TIMER;
3367 gsm48_rr_downmsg(ms, nmsg);
3369 /* CS process will trigger: return to MM IDLE / No SIM */
3377 /* RR is released in other states */
3378 static int gsm48_mm_rel_other(struct osmocom_ms *ms, struct msgb *msg)
3380 struct gsm48_mmlayer *mm = &ms->mmlayer;
3382 /* stop RR release timer (if running) */
3385 /* CS process will trigger: return to MM IDLE */
3393 /* state trasitions for MMxx-SAP messages from upper layers */
3394 static struct downstate {
3398 int (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3399 } downstatelist[] = {
3400 /* 4.2.2.1 Normal service */
3401 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3402 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3404 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3405 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_no_rr},
3407 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3408 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_no_rr},
3410 /* 4.2.2.2 Attempt to update / Loc. Upd. needed */
3411 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE) |
3412 SBIT(GSM48_MM_SST_LOC_UPD_NEEDED),
3413 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr}, /* emergency only */
3415 /* 4.2.2.3 Limited service */
3416 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3417 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3419 /* 4.2.2.4 No IMSI */
3420 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NO_IMSI),
3421 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3423 /* 4.2.2.5 PLMN search, normal service */
3424 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3425 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3427 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3428 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_no_rr},
3430 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3431 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_no_rr},
3433 /* 4.2.2.6 PLMN search */
3434 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH),
3435 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3437 /* 4.5.1.1 MM Connection (EST) */
3438 {SBIT(GSM48_MM_ST_RR_CONN_RELEASE_NA), ALL_STATES,
3439 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_first},
3441 {SBIT(GSM48_MM_ST_RR_CONN_RELEASE_NA), ALL_STATES,
3442 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_first},
3444 {SBIT(GSM48_MM_ST_RR_CONN_RELEASE_NA), ALL_STATES,
3445 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_first},
3447 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3448 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_more},
3450 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3451 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_more},
3453 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3454 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_more},
3456 {SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD), ALL_STATES,
3457 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_wait},
3459 {SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD), ALL_STATES,
3460 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_wait},
3462 {SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD), ALL_STATES,
3463 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_wait},
3465 {ALL_STATES, ALL_STATES,
3466 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_reject},
3468 {ALL_STATES, ALL_STATES,
3469 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_reject},
3471 {ALL_STATES, ALL_STATES,
3472 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_reject},
3474 /* 4.5.2.1 MM Connection (DATA) */
3475 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3476 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3477 GSM48_MMCC_DATA_REQ, gsm48_mm_data},
3479 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3480 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3481 GSM48_MMSS_DATA_REQ, gsm48_mm_data},
3483 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3484 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3485 GSM48_MMSMS_DATA_REQ, gsm48_mm_data},
3487 /* 4.5.2.1 MM Connection (REL) */
3488 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3489 GSM48_MMCC_REL_REQ, gsm48_mm_release_active},
3491 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3492 GSM48_MMSS_REL_REQ, gsm48_mm_release_active},
3494 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3495 GSM48_MMSMS_REL_REQ, gsm48_mm_release_active},
3497 {SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3498 GSM48_MMCC_REL_REQ, gsm48_mm_release_wait_add},
3500 {SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3501 GSM48_MMSS_REL_REQ, gsm48_mm_release_wait_add},
3503 {SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3504 GSM48_MMSMS_REL_REQ, gsm48_mm_release_wait_add},
3506 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN), ALL_STATES,
3507 GSM48_MMCC_REL_REQ, gsm48_mm_release_wait_active},
3509 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN), ALL_STATES,
3510 GSM48_MMSS_REL_REQ, gsm48_mm_release_wait_active},
3512 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN), ALL_STATES,
3513 GSM48_MMSMS_REL_REQ, gsm48_mm_release_wait_active},
3515 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON), ALL_STATES,
3516 GSM48_MMCC_REL_REQ, gsm48_mm_release_wait_rr},
3518 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON), ALL_STATES,
3519 GSM48_MMSS_REL_REQ, gsm48_mm_release_wait_rr},
3521 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON), ALL_STATES,
3522 GSM48_MMSMS_REL_REQ, gsm48_mm_release_wait_rr},
3526 (sizeof(downstatelist) / sizeof(struct downstate))
3528 int gsm48_mmxx_downmsg(struct osmocom_ms *ms, struct msgb *msg)
3530 struct gsm48_mmlayer *mm = &ms->mmlayer;
3531 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3532 int msg_type = mmh->msg_type;
3533 struct gsm48_mm_conn *conn;
3536 /* keep up to date with the transaction ID */
3537 conn = mm_conn_by_ref(mm, mmh->ref);
3539 conn->transaction_id = mmh->transaction_id;
3541 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' event in state %s\n",
3542 ms->name, get_mmxx_name(msg_type),
3543 gsm48_mm_state_names[mm->state]);
3544 if (mm->state == GSM48_MM_ST_MM_IDLE)
3545 LOGP(DMM, LOGL_INFO, "-> substate %s\n",
3546 gsm48_mm_substate_names[mm->substate]);
3547 LOGP(DMM, LOGL_INFO, "-> callref %x, transaction_id %d\n",
3548 mmh->ref, mmh->transaction_id);
3550 /* Find function for current state and message */
3551 for (i = 0; i < DOWNSLLEN; i++)
3552 if ((msg_type == downstatelist[i].type)
3553 && ((1 << mm->state) & downstatelist[i].states)
3554 && ((1 << mm->substate) & downstatelist[i].substates))
3556 if (i == DOWNSLLEN) {
3557 LOGP(DMM, LOGL_NOTICE, "Message unhandled at this state.\n");
3562 rc = downstatelist[i].rout(ms, msg);
3564 if (downstatelist[i].rout != gsm48_mm_data)
3570 /* state trasitions for radio ressource messages (lower layer) */
3571 static struct rrdatastate {
3574 int (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3575 } rrdatastatelist[] = {
3577 {SBIT(GSM48_MM_ST_MM_IDLE),
3578 GSM48_RR_EST_IND, gsm48_mm_est},
3581 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_IMSI_D), /* 4.3.4.4 */
3582 GSM48_RR_EST_CNF, gsm48_mm_imsi_detach_sent},
3584 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_IMSI_D), /* 4.3.4.4 (unsuc.) */
3585 GSM48_RR_REL_IND, gsm48_mm_imsi_detach_end},
3586 /* also this may happen if SABM is ackwnowledged with DISC */
3588 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_IMSI_D), /* 4.3.4.4 (lost) */
3589 GSM48_RR_ABORT_IND, gsm48_mm_imsi_detach_end},
3591 {SBIT(GSM48_MM_ST_IMSI_DETACH_INIT), /* 4.3.4.4 (unsuc.) */
3592 GSM48_RR_REL_IND, gsm48_mm_imsi_detach_end},
3594 {SBIT(GSM48_MM_ST_IMSI_DETACH_INIT), /* 4.3.4.4 (lost) */
3595 GSM48_RR_ABORT_IND, gsm48_mm_imsi_detach_end},
3597 /* location update */
3598 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_LUPD), /* 4.4.4.1 */
3599 GSM48_RR_EST_CNF, gsm48_mm_est_loc_upd},
3601 {SBIT(GSM48_MM_ST_LOC_UPD_INIT) |
3602 SBIT(GSM48_MM_ST_WAIT_RR_CONN_LUPD), /* 4.4.4.9 */
3603 GSM48_RR_REL_IND, gsm48_mm_rel_loc_upd_abort},
3605 {SBIT(GSM48_MM_ST_LOC_UPD_INIT) |
3606 SBIT(GSM48_MM_ST_WAIT_RR_CONN_LUPD), /* 4.4.4.9 */
3607 GSM48_RR_ABORT_IND, gsm48_mm_rel_loc_upd_abort},
3609 {SBIT(GSM48_MM_ST_LOC_UPD_REJ), /* 4.4.4.7 */
3610 GSM48_RR_REL_IND, gsm48_mm_rel_loc_upd_rej},
3612 {SBIT(GSM48_MM_ST_LOC_UPD_REJ), /* 4.4.4.7 */
3613 GSM48_RR_ABORT_IND, gsm48_mm_rel_loc_upd_rej},
3615 /* MM connection (EST) */
3616 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON), /* 4.5.1.1 */
3617 GSM48_RR_EST_CNF, gsm48_mm_est_mm_con},
3619 /* MM connection (DATA) */
3621 GSM48_RR_DATA_IND, gsm48_mm_data_ind},
3623 /* MM connection (SYNC) */
3624 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN) |
3625 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), /* 4.5.1.1 */
3626 GSM48_RR_SYNC_IND, gsm48_mm_sync_ind_wait},
3628 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE),
3629 GSM48_RR_SYNC_IND, gsm48_mm_sync_ind_active},
3631 /* MM connection (REL/ABORT) */
3632 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON) |
3633 SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN) |
3634 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), /* 4.5.1.2 */
3635 GSM48_RR_REL_IND, gsm48_mm_abort_mm_con},
3637 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON) |
3638 SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN) |
3639 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), /* 4.5.1.2 */
3640 GSM48_RR_ABORT_IND, gsm48_mm_abort_mm_con},
3642 /* MM connection (REL/ABORT with re-establishment possibility) */
3643 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), /* not supported */
3644 GSM48_RR_REL_IND, gsm48_mm_abort_mm_con},
3646 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3647 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), /* not supported */
3648 GSM48_RR_ABORT_IND, gsm48_mm_abort_mm_con},
3650 /* other (also wait for network command) */
3652 GSM48_RR_REL_IND, gsm48_mm_rel_other},
3655 GSM48_RR_ABORT_IND, gsm48_mm_rel_other},
3658 #define RRDATASLLEN \
3659 (sizeof(rrdatastatelist) / sizeof(struct rrdatastate))
3661 static int gsm48_rcv_rr(struct osmocom_ms *ms, struct msgb *msg)
3663 struct gsm48_mmlayer *mm = &ms->mmlayer;
3664 struct gsm48_rr_hdr *rrh = (struct gsm48_rr_hdr *)msg->data;
3665 int msg_type = rrh->msg_type;
3668 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' from RR in state %s\n",
3669 ms->name, get_rr_name(msg_type),
3670 gsm48_mm_state_names[mm->state]);
3672 /* find function for current state and message */
3673 for (i = 0; i < RRDATASLLEN; i++)
3674 if ((msg_type == rrdatastatelist[i].type)
3675 && ((1 << mm->state) & rrdatastatelist[i].states))
3677 if (i == RRDATASLLEN) {
3678 LOGP(DMM, LOGL_NOTICE, "Message unhandled at this state.\n");
3683 rc = rrdatastatelist[i].rout(ms, msg);
3685 if (rrdatastatelist[i].rout != gsm48_mm_data_ind)
3691 /* state trasitions for mobile managemnt messages (lower layer) */
3692 static struct mmdatastate {
3695 int (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3696 } mmdatastatelist[] = {
3697 {ALL_STATES, /* 4.3.1.2 */
3698 GSM48_MT_MM_TMSI_REALL_CMD, gsm48_mm_rx_tmsi_realloc_cmd},
3700 {ALL_STATES, /* 4.3.2.2 */
3701 GSM48_MT_MM_AUTH_REQ, gsm48_mm_rx_auth_req},
3703 {ALL_STATES, /* 4.3.2.5 */
3704 GSM48_MT_MM_AUTH_REJ, gsm48_mm_rx_auth_rej},
3706 {ALL_STATES, /* 4.3.3.2 */
3707 GSM48_MT_MM_ID_REQ, gsm48_mm_rx_id_req},
3709 {ALL_STATES, /* 4.3.5.2 */
3710 GSM48_MT_MM_ABORT, gsm48_mm_rx_abort},
3712 {ALL_STATES, /* 4.3.6.2 */
3713 GSM48_MT_MM_INFO, gsm48_mm_rx_info},
3715 {SBIT(GSM48_MM_ST_LOC_UPD_INIT), /* 4.4.4.6 */
3716 GSM48_MT_MM_LOC_UPD_ACCEPT, gsm48_mm_rx_loc_upd_acc},
3718 {SBIT(GSM48_MM_ST_LOC_UPD_INIT), /* 4.4.4.7 */
3719 GSM48_MT_MM_LOC_UPD_REJECT, gsm48_mm_rx_loc_upd_rej},
3721 {ALL_STATES, /* 4.5.1.1 */
3722 GSM48_MT_MM_CM_SERV_ACC, gsm48_mm_rx_cm_service_acc},
3724 {ALL_STATES, /* 4.5.1.1 */
3725 GSM48_MT_MM_CM_SERV_REJ, gsm48_mm_rx_cm_service_rej},
3728 #define MMDATASLLEN \
3729 (sizeof(mmdatastatelist) / sizeof(struct mmdatastate))
3731 static int gsm48_mm_data_ind(struct osmocom_ms *ms, struct msgb *msg)
3733 struct gsm48_mmlayer *mm = &ms->mmlayer;
3734 struct gsm48_hdr *gh = msgb_l3(msg);
3735 uint8_t pdisc = gh->proto_discr & 0x0f;
3736 uint8_t msg_type = gh->msg_type & 0xbf;
3737 struct gsm48_mmxx_hdr *mmh;
3738 int msg_supported = 0; /* determine, if message is supported at all */
3739 int rr_prim = -1, rr_est = -1; /* no prim set */
3744 if (msg_type == GSM48_MT_MM_NULL)
3747 if (mm->state == GSM48_MM_ST_IMSI_DETACH_INIT) {
3748 LOGP(DMM, LOGL_NOTICE, "DATA IND ignored during IMSI "
3752 /* pull the RR header */
3753 msgb_pull(msg, sizeof(struct gsm48_rr_hdr));
3755 /* create transaction (if not exists) and push message */
3757 case GSM48_PDISC_CC:
3758 rr_prim = GSM48_MMCC_DATA_IND;
3759 rr_est = GSM48_MMCC_EST_IND;
3762 case GSM48_PDISC_NC_SS:
3763 rr_prim = GSM48_MMSS_DATA_IND;
3764 rr_est = GSM48_MMSS_EST_IND;
3766 case GSM48_PDISC_SMS:
3767 rr_prim = GSM48_MMSMS_DATA_IND;
3768 rr_est = GSM48_MMSMS_EST_IND;
3772 if (rr_prim != -1) {
3773 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4;
3775 struct gsm48_mm_conn *conn;
3777 /* find transaction, if any */
3778 conn = mm_conn_by_id(mm, pdisc, transaction_id);
3780 /* create MM connection instance */
3782 conn = mm_conn_new(mm, pdisc, transaction_id,
3789 /* push new header */
3790 msgb_push(msg, sizeof(struct gsm48_mmxx_hdr));
3791 mmh = (struct gsm48_mmxx_hdr *)msg->data;
3792 mmh->msg_type = rr_prim;
3793 mmh->ref = conn->ref;
3795 /* go MM CONN ACTIVE state */
3796 if (mm->state == GSM48_MM_ST_WAIT_NETWORK_CMD
3797 || mm->state == GSM48_MM_ST_RR_CONN_RELEASE_NA) {
3798 /* stop RR release timer */
3801 /* stop "RR connection release not allowed" timer */
3804 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
3808 /* forward message */
3810 case GSM48_PDISC_MM:
3811 skip_ind = (gh->proto_discr & 0xf0) >> 4;
3813 /* ignore if skip indicator is not B'0000' */
3816 break; /* follow the selection proceedure below */
3818 case GSM48_PDISC_CC:
3819 return gsm48_rcv_cc(ms, msg);
3822 case GSM48_PDISC_NC_SS:
3823 return gsm48_rcv_ss(ms, msg);
3825 case GSM48_PDISC_SMS:
3826 return gsm48_rcv_sms(ms, msg);
3830 LOGP(DMM, LOGL_NOTICE, "Protocol type 0x%02x unsupported.\n",
3833 return gsm48_mm_tx_mm_status(ms,
3834 GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED);
3837 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' in MM state %s\n", ms->name,
3838 get_mm_name(msg_type), gsm48_mm_state_names[mm->state]);
3840 stop_mm_t3212(mm); /* 4.4.2 */
3842 /* 11.2 re-start pending RR release timer */
3843 if (bsc_timer_pending(&mm->t3240)) {
3848 /* find function for current state and message */
3849 for (i = 0; i < MMDATASLLEN; i++) {
3850 if (msg_type == mmdatastatelist[i].type)
3852 if ((msg_type == mmdatastatelist[i].type)
3853 && ((1 << mm->state) & mmdatastatelist[i].states))
3856 if (i == MMDATASLLEN) {
3858 if (msg_supported) {
3859 LOGP(DMM, LOGL_NOTICE, "Message unhandled at this "
3861 return gsm48_mm_tx_mm_status(ms,
3862 GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE);
3864 LOGP(DMM, LOGL_NOTICE, "Message not supported.\n");
3865 return gsm48_mm_tx_mm_status(ms,
3866 GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED);
3870 rc = mmdatastatelist[i].rout(ms, msg);
3877 /* state trasitions for mobile management events */
3878 static struct eventstate {
3882 int (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3883 } eventstatelist[] = {
3884 /* 4.2.3 return to MM IDLE */
3885 {ALL_STATES - SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES,
3886 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_no_cell_found},
3888 {ALL_STATES - SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES,
3889 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_return_idle},
3891 /* 4.2.2.1 Normal service */
3892 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3893 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3895 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3896 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_loc_upd_normal}, /* change */
3898 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3899 GSM48_MM_EVENT_TIMEOUT_T3211, gsm48_mm_loc_upd},
3901 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3902 GSM48_MM_EVENT_TIMEOUT_T3213, gsm48_mm_loc_upd},
3904 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3905 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_periodic},
3907 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3908 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_start},
3910 /* 4.2.2.2 Attempt to update / Loc. upd. needed */
3911 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE) |
3912 SBIT(GSM48_MM_SST_LOC_UPD_NEEDED),
3913 GSM48_MM_EVENT_USER_PLMN_SEL, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3915 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE) |
3916 SBIT(GSM48_MM_SST_LOC_UPD_NEEDED),
3917 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3919 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE) |
3920 SBIT(GSM48_MM_SST_LOC_UPD_NEEDED),
3921 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_loc_upd_normal}, /* change */
3923 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE),
3924 GSM48_MM_EVENT_TIMEOUT_T3211, gsm48_mm_loc_upd},
3926 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE),
3927 GSM48_MM_EVENT_TIMEOUT_T3213, gsm48_mm_loc_upd},
3929 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE),
3930 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_periodic},
3932 /* 4.2.2.3 Limited service */
3933 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3934 GSM48_MM_EVENT_USER_PLMN_SEL, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3936 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3937 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3939 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3940 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_loc_upd_normal}, /* if allow. */
3942 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3943 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_delay_per}, /* 4.4.2 */
3945 /* 4.2.2.4 No IMSI */
3946 /* 4.2.2.5 PLMN search, normal service */
3947 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3948 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_no_cell_found}, /* 4.2.1.1 */
3950 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3951 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_cell_selected}, /* 4.2.1.1 */
3953 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3954 GSM48_MM_EVENT_TIMEOUT_T3211, gsm48_mm_loc_upd_delay_retry},
3956 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3957 GSM48_MM_EVENT_TIMEOUT_T3213, gsm48_mm_loc_upd_delay_retry},
3959 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3960 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_delay_per}, /* 4.4.2 */
3962 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3963 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_start},
3965 /* 4.2.2.6 PLMN search */
3966 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH),
3967 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_no_cell_found}, /* 4.2.1.1 */
3969 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH),
3970 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_cell_selected}, /* 4.2.1.1 */
3972 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH),
3973 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_delay_per}, /* 4.4.2 */
3975 /* No cell available */
3976 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NO_CELL_AVAIL),
3977 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_cell_selected}, /* 4.2.1.1 */
3979 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NO_CELL_AVAIL),
3980 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_delay_per}, /* 4.4.2 */
3982 /* IMSI detach in other cases */
3983 {SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES, /* silently detach */
3984 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_end},
3986 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN) |
3987 SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3988 SBIT(GSM48_MM_ST_PROCESS_CM_SERV_P) |
3989 SBIT(GSM48_MM_ST_WAIT_REEST) |
3990 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON) |
3991 SBIT(GSM48_MM_ST_MM_CONN_ACTIVE_VGCS) |
3992 SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD), ALL_STATES, /* we can release */
3993 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_release},
3995 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_IMSI_D) |
3996 SBIT(GSM48_MM_ST_IMSI_DETACH_INIT) |
3997 SBIT(GSM48_MM_ST_IMSI_DETACH_PEND), ALL_STATES, /* ignore */
3998 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_ignore},
4000 {ALL_STATES, ALL_STATES,
4001 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_delay},
4003 {GSM48_MM_ST_IMSI_DETACH_INIT, ALL_STATES,
4004 GSM48_MM_EVENT_TIMEOUT_T3220, gsm48_mm_imsi_detach_end},
4006 /* location update in other cases */
4007 {ALL_STATES, ALL_STATES,
4008 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_ignore},
4010 {ALL_STATES - SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES,
4011 GSM48_MM_EVENT_TIMEOUT_T3210, gsm48_mm_loc_upd_timeout},
4013 {ALL_STATES - SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES,
4014 GSM48_MM_EVENT_TIMEOUT_T3213, gsm48_mm_loc_upd_failed},
4015 /* 4.4.4.9 c) (but without retry) */
4018 {ALL_STATES, ALL_STATES,
4019 GSM48_MM_EVENT_SYSINFO, gsm48_mm_sysinfo},
4021 /* T3240 timed out */
4022 {SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD) |
4023 SBIT(GSM48_MM_ST_LOC_UPD_REJ), ALL_STATES, /* 4.4.4.8 */
4024 GSM48_MM_EVENT_TIMEOUT_T3240, gsm48_mm_abort_rr},
4026 /* T3230 timed out */
4027 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
4028 GSM48_MM_EVENT_TIMEOUT_T3230, gsm48_mm_timeout_mm_con},
4030 /* SIM reports SRES */
4031 {ALL_STATES, ALL_STATES, /* 4.3.2.2 */
4032 GSM48_MM_EVENT_AUTH_RESPONSE, gsm48_mm_tx_auth_rsp},
4035 /* change in classmark is reported */
4036 {ALL_STATES, ALL_STATES,
4037 GSM48_MM_EVENT_CLASSMARK_CHG, gsm48_mm_classm_chg},
4041 #define EVENTSLLEN \
4042 (sizeof(eventstatelist) / sizeof(struct eventstate))
4044 static int gsm48_mm_ev(struct osmocom_ms *ms, int msg_type, struct msgb *msg)
4046 struct gsm48_mmlayer *mm = &ms->mmlayer;
4049 if (mm->state == GSM48_MM_ST_MM_IDLE)
4050 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' event in state "
4051 "MM IDLE, %s\n", ms->name, get_mmevent_name(msg_type),
4052 gsm48_mm_substate_names[mm->substate]);
4054 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' event in state "
4055 "%s\n", ms->name, get_mmevent_name(msg_type),
4056 gsm48_mm_state_names[mm->state]);
4058 /* Find function for current state and message */
4059 for (i = 0; i < EVENTSLLEN; i++)
4060 if ((msg_type == eventstatelist[i].type)
4061 && ((1 << mm->state) & eventstatelist[i].states)
4062 && ((1 << mm->substate) & eventstatelist[i].substates))
4064 if (i == EVENTSLLEN) {
4065 LOGP(DMM, LOGL_NOTICE, "Message unhandled at this state.\n");
4069 rc = eventstatelist[i].rout(ms, msg);
4075 * MM Register (SIM insert and remove)
4078 /* register new SIM card and trigger attach */
4079 static int gsm48_mmr_reg_req(struct osmocom_ms *ms)
4081 struct gsm48_mmlayer *mm = &ms->mmlayer;
4084 /* schedule insertion of SIM */
4085 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SIM_INSERT);
4088 gsm322_plmn_sendmsg(ms, nmsg);
4090 /* 4.2.1.2 SIM is inserted in state NO IMSI */
4091 if (mm->state == GSM48_MM_ST_MM_IDLE
4092 && mm->substate == GSM48_MM_SST_NO_IMSI)
4093 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
4094 gsm48_mm_set_plmn_search(ms));
4099 /* trigger detach of sim card */
4100 static int gsm48_mmr_nreg_req(struct osmocom_ms *ms)
4102 struct gsm48_mmlayer *mm = &ms->mmlayer;
4105 nmsg = gsm322_msgb_alloc(GSM48_MM_EVENT_IMSI_DETACH);
4108 gsm48_mmevent_msg(mm->ms, nmsg);
4113 static int gsm48_rcv_mmr(struct osmocom_ms *ms, struct msgb *msg)
4115 struct gsm48_mmr *mmr = (struct gsm48_mmr *)msg->data;
4116 int msg_type = mmr->msg_type;
4119 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' event\n", ms->name,
4120 get_mmr_name(msg_type));
4122 case GSM48_MMR_REG_REQ:
4123 rc = gsm48_mmr_reg_req(ms);
4125 case GSM48_MMR_NREG_REQ:
4126 rc = gsm48_mmr_nreg_req(ms);
4129 LOGP(DMM, LOGL_NOTICE, "Message unhandled.\n");