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/bb/common/logging.h>
35 #include <osmocom/bb/common/osmocom_data.h>
36 #include <osmocom/bb/common/l23_app.h>
37 #include <osmocom/bb/common/networks.h>
38 #include <osmocom/bb/common/l1ctl.h>
39 #include <osmocom/bb/mobile/gsm48_cc.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->lac == 0x0000 || subscr->lac >= 0xfffe) {
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->mcc
996 || cs->sel_mnc != subscr->mnc
997 || cs->sel_lac != subscr->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->mcc),
1003 gsm_print_mnc(subscr->mnc), subscr->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->lac /* valid */
1051 && cs->sel_mcc == subscr->mcc
1052 && cs->sel_mnc == subscr->mnc
1053 && cs->sel_lac == subscr->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->lac /* valid */
1116 && cs->sel_mcc == subscr->mcc
1117 && cs->sel_mnc == subscr->mnc
1118 && cs->sel_lac == subscr->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->mcc, &subscr->mnc, &subscr->lac);
1478 mi = gh->data + sizeof(struct gsm48_loc_area_id);
1479 mi_type = mi[1] & GSM_MI_TYPE_MASK;
1481 case GSM_MI_TYPE_TMSI:
1482 if (payload_len + sizeof(struct gsm48_loc_area_id) < 6
1485 memcpy(&tmsi, mi+2, 4);
1486 subscr->tmsi = ntohl(tmsi);
1487 LOGP(DMM, LOGL_INFO, "TMSI 0x%08x (%u) assigned.\n",
1488 subscr->tmsi, subscr->tmsi);
1489 gsm48_mm_tx_tmsi_reall_cpl(ms);
1491 case GSM_MI_TYPE_IMSI:
1492 subscr->tmsi = 0x00000000;
1493 LOGP(DMM, LOGL_INFO, "TMSI removed.\n");
1494 gsm48_mm_tx_tmsi_reall_cpl(ms);
1497 subscr->tmsi = 0x00000000;
1498 LOGP(DMM, LOGL_NOTICE, "TMSI reallocation with unknown MI "
1499 "type %d.\n", mi_type);
1500 gsm48_mm_tx_mm_status(ms, GSM48_REJECT_INCORRECT_MESSAGE);
1503 /* store LOCI on sim */
1504 gsm_subscr_write_loci(ms);
1509 /* 4.3.2.2 AUTHENTICATION REQUEST is received */
1510 static int gsm48_mm_rx_auth_req(struct osmocom_ms *ms, struct msgb *msg)
1512 struct gsm_subscriber *subscr = &ms->subscr;
1513 struct gsm_settings *set = &ms->settings;
1514 struct gsm48_mmlayer *mm = &ms->mmlayer;
1515 struct gsm48_hdr *gh = msgb_l3(msg);
1516 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1517 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) gh->data;
1520 if (payload_len < sizeof(struct gsm48_auth_req)) {
1521 LOGP(DMM, LOGL_NOTICE, "Short read of AUTHENTICATION REQUEST "
1522 "message error.\n");
1526 /* SIM is not available */
1527 if (!subscr->sim_valid) {
1528 LOGP(DMM, LOGL_INFO, "AUTHENTICATION REQUEST without SIM\n");
1529 return gsm48_mm_tx_mm_status(ms,
1530 GSM48_REJECT_MSG_NOT_COMPATIBLE);
1533 LOGP(DMM, LOGL_INFO, "AUTHENTICATION REQUEST (seq %d)\n", ar->key_seq);
1535 /* key_seq and random
1536 * in case of test card, there is a dummy response.
1537 * authentication request is possible during emergency call, if
1538 * IMSI is known to the network. in case of emergency IMSI, we need to
1539 * send a dummy response also.
1541 if (mm->est_cause == RR_EST_CAUSE_EMERGENCY && set->emergency_imsi[0])
1543 gsm_subscr_generate_kc(ms, ar->key_seq, ar->rand, no_sim);
1545 /* wait for auth response event from SIM */
1549 /* 4.3.2.2 sending AUTHENTICATION RESPONSE */
1550 static int gsm48_mm_tx_auth_rsp(struct osmocom_ms *ms, struct msgb *msg)
1552 struct gsm48_mm_event *mme = (struct gsm48_mm_event *) msg->data;
1554 struct gsm48_hdr *ngh;
1557 LOGP(DMM, LOGL_INFO, "AUTHENTICATION RESPONSE\n");
1559 nmsg = gsm48_l3_msgb_alloc();
1562 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
1564 ngh->proto_discr = GSM48_PDISC_MM;
1565 ngh->msg_type = GSM48_MT_MM_AUTH_RESP;
1568 sres = msgb_put(nmsg, 4);
1569 memcpy(sres, mme->sres, 4);
1571 /* push RR header and send down */
1572 return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_DATA_REQ, 0);
1575 /* 4.3.2.5 AUTHENTICATION REJECT is received */
1576 static int gsm48_mm_rx_auth_rej(struct osmocom_ms *ms, struct msgb *msg)
1578 struct gsm_subscriber *subscr = &ms->subscr;
1579 struct gsm48_mmlayer *mm = &ms->mmlayer;
1581 LOGP(DMM, LOGL_INFO, "AUTHENTICATION REJECT\n");
1583 stop_mm_t3212(mm); /* 4.4.2 */
1586 subscr->sim_valid = 0;
1588 /* TMSI and LAI invalid */
1589 subscr->tmsi = 0xffffffff;
1590 subscr->lac = 0x0000;
1592 /* key is invalid */
1593 subscr->key_seq = 7;
1596 new_sim_ustate(subscr, GSM_SIM_U3_ROAMING_NA);
1598 /* store LOCI on sim */
1599 gsm_subscr_write_loci(ms);
1601 /* abort IMSI detach procedure */
1602 if (mm->state == GSM48_MM_ST_IMSI_DETACH_INIT) {
1604 struct gsm48_rr_hdr *nrrh;
1606 /* abort RR connection */
1607 nmsg = gsm48_rr_msgb_alloc(GSM48_RR_ABORT_REQ);
1610 nrrh = (struct gsm48_rr_hdr *) msgb_put(nmsg, sizeof(*nrrh));
1611 nrrh->cause = GSM48_RR_CAUSE_NORMAL;
1612 gsm48_rr_downmsg(ms, nmsg);
1614 /* CS process will trigger: return to MM IDLE / No SIM */
1621 /* 4.3.3.1 IDENTITY REQUEST is received */
1622 static int gsm48_mm_rx_id_req(struct osmocom_ms *ms, struct msgb *msg)
1624 struct gsm_subscriber *subscr = &ms->subscr;
1625 struct gsm48_hdr *gh = msgb_l3(msg);
1626 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1629 if (payload_len < 1) {
1630 LOGP(DMM, LOGL_NOTICE, "Short read of IDENTITY REQUEST message "
1636 mi_type = *gh->data;
1637 LOGP(DMM, LOGL_INFO, "IDENTITY REQUEST (mi_type %d)\n", mi_type);
1639 /* check if request can be fulfilled */
1640 if (!subscr->sim_valid && mi_type != GSM_MI_TYPE_IMEI
1641 && mi_type != GSM_MI_TYPE_IMEISV) {
1642 LOGP(DMM, LOGL_INFO, "IDENTITY REQUEST without SIM\n");
1643 return gsm48_mm_tx_mm_status(ms,
1644 GSM48_REJECT_MSG_NOT_COMPATIBLE);
1646 if (mi_type == GSM_MI_TYPE_TMSI && subscr->tmsi == 0xffffffff) {
1647 LOGP(DMM, LOGL_INFO, "IDENTITY REQUEST of TMSI, but we have no "
1649 return gsm48_mm_tx_mm_status(ms,
1650 GSM48_REJECT_MSG_NOT_COMPATIBLE);
1653 return gsm48_mm_tx_id_rsp(ms, mi_type);
1656 /* send IDENTITY RESPONSE message */
1657 static int gsm48_mm_tx_id_rsp(struct osmocom_ms *ms, uint8_t mi_type)
1660 struct gsm48_hdr *ngh;
1663 LOGP(DMM, LOGL_INFO, "IDENTITY RESPONSE\n");
1665 nmsg = gsm48_l3_msgb_alloc();
1668 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
1670 ngh->proto_discr = GSM48_PDISC_MM;
1671 ngh->msg_type = GSM48_MT_MM_ID_RESP;
1674 gsm48_encode_mi(buf, nmsg, ms, mi_type);
1676 /* push RR header and send down */
1677 return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_DATA_REQ, 0);
1680 /* 4.3.4.1 sending IMSI DETACH INDICATION message */
1681 static int gsm48_mm_tx_imsi_detach(struct osmocom_ms *ms, int rr_prim)
1683 struct gsm_subscriber *subscr = &ms->subscr;
1684 struct gsm48_mmlayer *mm = &ms->mmlayer;
1685 struct gsm_support *sup = &ms->support;
1686 struct gsm48_rrlayer *rr = &ms->rrlayer;
1688 struct gsm48_hdr *ngh;
1691 struct gsm48_classmark1 cm;
1694 LOGP(DMM, LOGL_INFO, "IMSI DETACH INDICATION\n");
1696 nmsg = gsm48_l3_msgb_alloc();
1699 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
1701 ngh->proto_discr = GSM48_PDISC_MM;
1702 ngh->msg_type = GSM48_MT_MM_IMSI_DETACH_IND;
1705 if (rr->cd_now.arfcn >= 512 && rr->cd_now.arfcn <= 885)
1706 pwr_lev = sup->pwr_lev_1800;
1708 pwr_lev = sup->pwr_lev_900;
1709 gsm48_encode_classmark1(&cm, sup->rev_lev, sup->es_ind, sup->a5_1,
1711 msgb_v_put(nmsg, *((uint8_t *)&cm));
1713 if (subscr->tmsi != 0xffffffff) /* have TMSI ? */
1714 gsm48_encode_mi(buf, nmsg, ms, GSM_MI_TYPE_TMSI);
1716 gsm48_encode_mi(buf, nmsg, ms, GSM_MI_TYPE_IMSI);
1718 /* push RR header and send down */
1719 mm->est_cause = RR_EST_CAUSE_OTHER_SDCCH;
1720 return gsm48_mm_to_rr(ms, nmsg, rr_prim, mm->est_cause);
1723 /* detach has ended */
1724 static int gsm48_mm_imsi_detach_end(struct osmocom_ms *ms, struct msgb *msg)
1726 struct gsm48_mmlayer *mm = &ms->mmlayer;
1727 struct gsm_subscriber *subscr = &ms->subscr;
1730 LOGP(DMM, LOGL_INFO, "IMSI has been detached.\n");
1732 /* stop IMSI detach timer (if running) */
1737 subscr->sim_valid = 0;
1739 /* wait for RR idle and then power off when IMSI is detached */
1740 if (mm->power_off) {
1741 if (mm->state == GSM48_MM_ST_MM_IDLE) {
1745 mm->power_off_idle = 1;
1750 /* send SIM remove event to gsm322 */
1751 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SIM_REMOVE);
1754 gsm322_plmn_sendmsg(ms, nmsg);
1756 /* CS process will trigger return to MM IDLE / No SIM */
1760 /* start an IMSI detach in MM IDLE */
1761 static int gsm48_mm_imsi_detach_start(struct osmocom_ms *ms, struct msgb *msg)
1763 struct gsm_subscriber *subscr = &ms->subscr;
1764 struct gsm48_mmlayer *mm = &ms->mmlayer;
1765 struct gsm48_sysinfo *s = &ms->cellsel.sel_si;
1767 /* we may silently finish IMSI detach */
1768 if (!s->att_allowed || !subscr->imsi_attached) {
1769 LOGP(DMM, LOGL_INFO, "IMSI detach not required.\n");
1771 return gsm48_mm_imsi_detach_end(ms, msg);
1773 LOGP(DMM, LOGL_INFO, "IMSI detach started (MM IDLE)\n");
1775 new_mm_state(mm, GSM48_MM_ST_WAIT_RR_CONN_IMSI_D, 0);
1777 /* establish RR and send IMSI detach */
1778 return gsm48_mm_tx_imsi_detach(ms, GSM48_RR_EST_REQ);
1781 /* IMSI detach has been sent, wait for RR release */
1782 static int gsm48_mm_imsi_detach_sent(struct osmocom_ms *ms, struct msgb *msg)
1784 struct gsm48_mmlayer *mm = &ms->mmlayer;
1786 /* start T3220 (4.3.4.1) */
1789 LOGP(DMM, LOGL_INFO, "IMSI detach started (Wait for RR release)\n");
1791 new_mm_state(mm, GSM48_MM_ST_IMSI_DETACH_INIT, 0);
1796 /* release MM connection and proceed with IMSI detach */
1797 static int gsm48_mm_imsi_detach_release(struct osmocom_ms *ms, struct msgb *msg)
1799 struct gsm_subscriber *subscr = &ms->subscr;
1800 struct gsm48_mmlayer *mm = &ms->mmlayer;
1801 struct gsm48_sysinfo *s = &ms->cellsel.sel_si;
1803 /* stop MM connection timer */
1806 /* release all connections */
1807 gsm48_mm_release_mm_conn(ms, 1, 16, 0);
1809 /* wait for release of RR */
1810 if (!s->att_allowed || !subscr->imsi_attached) {
1811 LOGP(DMM, LOGL_INFO, "IMSI detach not required.\n");
1812 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
1815 if (mm->power_off) {
1823 /* send IMSI detach */
1824 gsm48_mm_tx_imsi_detach(ms, GSM48_RR_DATA_REQ);
1826 /* go to sent state */
1827 return gsm48_mm_imsi_detach_sent(ms, msg);
1830 /* ignore ongoing IMSI detach */
1831 static int gsm48_mm_imsi_detach_ignore(struct osmocom_ms *ms, struct msgb *msg)
1836 /* delay until state change (and then retry) */
1837 static int gsm48_mm_imsi_detach_delay(struct osmocom_ms *ms, struct msgb *msg)
1839 struct gsm48_mmlayer *mm = &ms->mmlayer;
1841 LOGP(DMM, LOGL_INFO, "IMSI detach delayed.\n");
1843 /* remember to detach later */
1844 mm->delay_detach = 1;
1849 /* 4.3.5.2 ABORT is received */
1850 static int gsm48_mm_rx_abort(struct osmocom_ms *ms, struct msgb *msg)
1852 struct gsm48_mmlayer *mm = &ms->mmlayer;
1853 struct gsm_subscriber *subscr = &ms->subscr;
1854 struct gsm48_hdr *gh = msgb_l3(msg);
1855 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1856 uint8_t reject_cause;
1858 if (payload_len < 1) {
1859 LOGP(DMM, LOGL_NOTICE, "Short read of ABORT message error.\n");
1863 reject_cause = *gh->data;
1865 if (llist_empty(&mm->mm_conn)) {
1866 LOGP(DMM, LOGL_NOTICE, "ABORT (cause #%d) while no MM "
1867 "connection is established.\n", reject_cause);
1868 return gsm48_mm_tx_mm_status(ms,
1869 GSM48_REJECT_MSG_NOT_COMPATIBLE);
1871 LOGP(DMM, LOGL_NOTICE, "ABORT (cause #%d) while MM connection "
1872 "is established.\n", reject_cause);
1873 /* stop MM connection timer */
1876 gsm48_mm_release_mm_conn(ms, 1, 16, 0);
1879 if (reject_cause == GSM48_REJECT_ILLEGAL_ME) {
1881 subscr->sim_valid = 0;
1883 /* TMSI and LAI invalid */
1884 subscr->tmsi = 0xffffffff;
1885 subscr->lac = 0x0000;
1887 /* key is invalid */
1888 subscr->key_seq = 7;
1891 new_sim_ustate(subscr, GSM_SIM_U3_ROAMING_NA);
1893 /* store LOCI on sim */
1894 gsm_subscr_write_loci(ms);
1896 /* CS process will trigger: return to MM IDLE / No SIM */
1903 /* 4.3.6.2 MM INFORMATION is received */
1904 static int gsm48_mm_rx_info(struct osmocom_ms *ms, struct msgb *msg)
1906 struct gsm48_mmlayer *mm = &ms->mmlayer;
1907 struct gsm48_hdr *gh = msgb_l3(msg);
1908 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1909 struct tlv_parsed tp;
1911 if (payload_len < 0) {
1912 LOGP(DMM, LOGL_NOTICE, "Short read of MM INFORMATION message "
1916 tlv_parse(&tp, &gsm48_mm_att_tlvdef, gh->data, payload_len, 0, 0);
1919 if (TLVP_PRESENT(&tp, GSM48_IE_NAME_LONG)) {
1920 decode_network_name(mm->name_long, sizeof(mm->name_long),
1921 TLVP_VAL(&tp, GSM48_IE_NAME_LONG)-1);
1924 if (TLVP_PRESENT(&tp, GSM48_IE_NAME_SHORT)) {
1925 decode_network_name(mm->name_short, sizeof(mm->name_short),
1926 TLVP_VAL(&tp, GSM48_IE_NAME_SHORT)-1);
1933 * process handlers for Location Update + IMSI attach (MM specific procedures)
1936 /* 4.4.2 received sysinfo change event */
1937 static int gsm48_mm_sysinfo(struct osmocom_ms *ms, struct msgb *msg)
1939 struct gsm48_mmlayer *mm = &ms->mmlayer;
1940 struct gsm48_sysinfo *s = &ms->cellsel.sel_si;
1942 /* t3212 not changed in these states */
1943 if (mm->state == GSM48_MM_ST_MM_IDLE
1944 && (mm->substate == GSM48_MM_SST_NO_CELL_AVAIL
1945 || mm->substate == GSM48_MM_SST_LIMITED_SERVICE
1946 || mm->substate == GSM48_MM_SST_PLMN_SEARCH
1947 || mm->substate == GSM48_MM_SST_PLMN_SEARCH_NORMAL))
1950 /* new periodic location update timer timeout */
1951 if (s->t3212 && s->t3212 != mm->t3212_value) {
1952 if (bsc_timer_pending(&mm->t3212)) {
1954 struct timeval current_time;
1957 gettimeofday(¤t_time, NULL);
1958 t = mm->t3212.timeout.tv_sec - current_time.tv_sec;
1961 LOGP(DMM, LOGL_INFO, "New T3212 while timer is running "
1962 "(value %d rest %d)\n", s->t3212, t);
1964 /* rest time modulo given value */
1965 mm->t3212.timeout.tv_sec = current_time.tv_sec
1968 uint32_t rand = random();
1970 LOGP(DMM, LOGL_INFO, "New T3212 while timer is not "
1971 "running (value %d)\n", s->t3212);
1973 /* value between 0 and given value */
1974 start_mm_t3212(mm, rand % (s->t3212 + 1));
1976 mm->t3212_value = s->t3212;
1982 /* 4.4.4.1 (re)start location update
1984 * this function is called by
1985 * - normal location update
1986 * - periodic location update
1987 * - imsi attach (normal loc. upd. function)
1988 * - retry timers (T3211 and T3213)
1990 static int gsm48_mm_loc_upd(struct osmocom_ms *ms, struct msgb *msg)
1992 struct gsm48_mmlayer *mm = &ms->mmlayer;
1993 struct gsm322_cellsel *cs = &ms->cellsel;
1994 struct gsm48_sysinfo *s = &cs->sel_si;
1995 struct gsm_subscriber *subscr = &ms->subscr;
1996 struct gsm_settings *set = &ms->settings;
2000 /* (re)start only if we still require location update */
2001 if (!mm->lupd_pending) {
2002 LOGP(DMM, LOGL_INFO, "No loc. upd. pending.\n");
2006 /* must camp normally */
2007 if (cs->state != GSM322_C3_CAMPED_NORMALLY) {
2008 LOGP(DMM, LOGL_INFO, "Loc. upd. not camping normally.\n");
2009 msg_type = GSM322_EVENT_REG_FAILED;
2011 LOGP(DSUM, LOGL_INFO, "Location updating not possible\n");
2013 mm->lupd_pending = 0;
2014 /* send message to PLMN search process */
2015 nmsg = gsm322_msgb_alloc(msg_type);
2018 gsm322_plmn_sendmsg(ms, nmsg);
2022 /* deny network, if disabled */
2024 LOGP(DMM, LOGL_INFO, "Loc. upd. disabled, adding "
2025 "forbidden PLMN.\n");
2026 LOGP(DSUM, LOGL_INFO, "Location updating is disabled by "
2028 gsm_subscr_add_forbidden_plmn(subscr, cs->sel_mcc,
2029 cs->sel_mnc, GSM48_REJECT_PLMN_NOT_ALLOWED);
2030 msg_type = GSM322_EVENT_ROAMING_NA;
2034 /* if LAI is forbidden, don't start */
2035 if (gsm_subscr_is_forbidden_plmn(subscr, cs->sel_mcc, cs->sel_mnc)) {
2036 LOGP(DMM, LOGL_INFO, "Loc. upd. not allowed PLMN.\n");
2037 msg_type = GSM322_EVENT_ROAMING_NA;
2040 if (gsm322_is_forbidden_la(ms, cs->sel_mcc,
2041 cs->sel_mnc, cs->sel_lac)) {
2042 LOGP(DMM, LOGL_INFO, "Loc. upd. not allowed LA.\n");
2043 msg_type = GSM322_EVENT_ROAMING_NA;
2047 /* 4.4.4.9 if cell is barred, don't start */
2048 if ((!subscr->acc_barr && s->cell_barr)
2049 || (!subscr->acc_barr && !((subscr->acc_class & 0xfbff) &
2050 (s->class_barr ^ 0xffff)))) {
2051 LOGP(DMM, LOGL_INFO, "Loc. upd. no access.\n");
2052 msg_type = GSM322_EVENT_ROAMING_NA;
2056 mm->lupd_mcc = cs->sel_mcc;
2057 mm->lupd_mnc = cs->sel_mnc;
2058 mm->lupd_lac = cs->sel_lac;
2060 LOGP(DSUM, LOGL_INFO, "Perform location update (MCC %s, MNC %s "
2061 "LAC 0x%04x)\n", gsm_print_mcc(mm->lupd_mcc),
2062 gsm_print_mnc(mm->lupd_mnc), mm->lupd_lac);
2064 return gsm48_mm_tx_loc_upd_req(ms);
2067 /* initiate a normal location update / imsi attach */
2068 static int gsm48_mm_loc_upd_normal(struct osmocom_ms *ms, struct msgb *msg)
2070 struct gsm48_mmlayer *mm = &ms->mmlayer;
2071 struct gsm_subscriber *subscr = &ms->subscr;
2072 struct gsm322_cellsel *cs = &ms->cellsel;
2073 struct gsm48_sysinfo *s = &cs->sel_si;
2076 /* in case we already have a location update going on */
2077 if (mm->lupd_pending) {
2078 LOGP(DMM, LOGL_INFO, "Loc. upd. already pending.\n");
2083 /* no location update, if limited service */
2084 if (cs->state != GSM322_C3_CAMPED_NORMALLY) {
2085 LOGP(DMM, LOGL_INFO, "Loc. upd. not allowed.\n");
2087 /* send message to PLMN search process */
2088 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_FAILED);
2091 gsm322_plmn_sendmsg(ms, nmsg);
2096 /* if location update is not required */
2097 if (subscr->ustate == GSM_SIM_U1_UPDATED
2099 && cs->sel_mcc == subscr->mcc
2100 && cs->sel_mnc == subscr->mnc
2101 && cs->sel_lac == subscr->lac
2102 && (subscr->imsi_attached
2103 || !s->att_allowed)) {
2104 LOGP(DMM, LOGL_INFO, "Loc. upd. not required.\n");
2105 subscr->imsi_attached = 1;
2107 /* send message to PLMN search process */
2108 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_SUCCESS);
2111 gsm322_plmn_sendmsg(ms, nmsg);
2116 /* 4.4.3 is attachment required? */
2117 if (subscr->ustate == GSM_SIM_U1_UPDATED
2119 && cs->sel_mcc == subscr->mcc
2120 && cs->sel_mnc == subscr->mnc
2121 && cs->sel_lac == subscr->lac
2122 && !subscr->imsi_attached
2123 && s->att_allowed) {
2124 /* do location update for IMSI attach */
2125 LOGP(DMM, LOGL_INFO, "Do Loc. upd. for IMSI attach.\n");
2128 /* do normal location update */
2129 LOGP(DMM, LOGL_INFO, "Do normal Loc. upd.\n");
2133 /* start location update */
2134 mm->lupd_attempt = 0;
2135 mm->lupd_pending = 1;
2136 mm->lupd_ra_failure = 0;
2138 return gsm48_mm_loc_upd(ms, msg);
2141 /* initiate a periodic location update */
2142 static int gsm48_mm_loc_upd_periodic(struct osmocom_ms *ms, struct msgb *msg)
2144 struct gsm48_mmlayer *mm = &ms->mmlayer;
2146 /* in case we already have a location update going on */
2147 if (mm->lupd_pending) {
2148 LOGP(DMM, LOGL_INFO, "Loc. upd. already pending.\n");
2152 /* start periodic location update */
2154 mm->lupd_pending = 1;
2155 mm->lupd_ra_failure = 0;
2157 return gsm48_mm_loc_upd(ms, msg);
2160 /* ignore location update */
2161 static int gsm48_mm_loc_upd_ignore(struct osmocom_ms *ms, struct msgb *msg)
2166 /* 9.2.15 send LOCATION UPDATING REQUEST message */
2167 static int gsm48_mm_tx_loc_upd_req(struct osmocom_ms *ms)
2169 struct gsm48_mmlayer *mm = &ms->mmlayer;
2170 struct gsm_support *sup = &ms->support;
2171 struct gsm_subscriber *subscr = &ms->subscr;
2172 struct gsm48_rrlayer *rr = &ms->rrlayer;
2174 struct gsm48_hdr *ngh;
2175 struct gsm48_loc_upd_req *nlu; /* NOTE: mi_len is part of struct */
2179 LOGP(DMM, LOGL_INFO, "LOCATION UPDATING REQUEST\n");
2181 nmsg = gsm48_l3_msgb_alloc();
2184 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
2185 nlu = (struct gsm48_loc_upd_req *)msgb_put(nmsg, sizeof(*nlu));
2187 ngh->proto_discr = GSM48_PDISC_MM;
2188 ngh->msg_type = GSM48_MT_MM_LOC_UPD_REQUEST;
2190 /* location updating type */
2191 nlu->type = mm->lupd_type;
2193 nlu->key_seq = subscr->key_seq;
2194 /* LAI (last SIM stored LAI)
2196 * NOTE: The TMSI is only valid within a LAI!
2198 gsm48_encode_lai(&nlu->lai, subscr->mcc, subscr->mnc, subscr->lac);
2200 if (rr->cd_now.arfcn >= 512 && rr->cd_now.arfcn <= 885)
2201 pwr_lev = sup->pwr_lev_1800;
2203 pwr_lev = sup->pwr_lev_900;
2204 gsm48_encode_classmark1(&nlu->classmark1, sup->rev_lev, sup->es_ind,
2205 sup->a5_1, pwr_lev);
2207 if (subscr->tmsi != 0xffffffff) /* have TMSI ? */
2208 gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_TMSI);
2210 gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_IMSI);
2211 msgb_put(nmsg, buf[1]); /* length is part of nlu */
2212 memcpy(&nlu->mi_len, buf + 1, 1 + buf[1]);
2214 new_mm_state(mm, GSM48_MM_ST_WAIT_RR_CONN_LUPD, 0);
2216 /* push RR header and send down */
2217 mm->est_cause = RR_EST_CAUSE_LOC_UPD;
2218 return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_EST_REQ, mm->est_cause);
2221 /* 4.4.4.1 RR is esablised during location update */
2222 static int gsm48_mm_est_loc_upd(struct osmocom_ms *ms, struct msgb *msg)
2224 struct gsm48_mmlayer *mm = &ms->mmlayer;
2226 /* start location update timer */
2229 new_mm_state(mm, GSM48_MM_ST_LOC_UPD_INIT, 0);
2234 /* 4.4.4.6 LOCATION UPDATING ACCEPT is received */
2235 static int gsm48_mm_rx_loc_upd_acc(struct osmocom_ms *ms, struct msgb *msg)
2237 struct gsm48_mmlayer *mm = &ms->mmlayer;
2238 struct gsm_subscriber *subscr = &ms->subscr;
2239 struct gsm48_hdr *gh = msgb_l3(msg);
2240 struct gsm48_loc_area_id *lai = (struct gsm48_loc_area_id *) gh->data;
2241 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2242 struct tlv_parsed tp;
2245 if (payload_len < sizeof(struct gsm48_loc_area_id)) {
2247 LOGP(DMM, LOGL_NOTICE, "Short read of LOCATION UPDATING ACCEPT "
2248 "message error.\n");
2251 tlv_parse(&tp, &gsm48_mm_att_tlvdef,
2252 gh->data + sizeof(struct gsm48_loc_area_id),
2253 payload_len - sizeof(struct gsm48_loc_area_id), 0, 0);
2255 /* update has finished */
2256 mm->lupd_pending = 0;
2258 /* RA was successfull */
2259 mm->lupd_ra_failure = 0;
2261 /* stop periodic location updating timer */
2262 stop_mm_t3212(mm); /* 4.4.2 */
2265 gsm48_decode_lai(lai, &subscr->mcc, &subscr->mnc, &subscr->lac);
2267 /* stop location update timer */
2270 /* reset attempt counter */
2271 mm->lupd_attempt = 0;
2273 /* mark SIM as attached */
2274 subscr->imsi_attached = 1;
2276 /* set the status in the sim to updated */
2277 new_sim_ustate(subscr, GSM_SIM_U1_UPDATED);
2279 /* store LOCI on sim */
2280 gsm_subscr_write_loci(ms);
2282 /* set last registered PLMN */
2283 if (subscr->lac > 0x0000 && subscr->lac < 0xfffe) {
2284 subscr->plmn_valid = 1;
2285 subscr->plmn_mcc = subscr->mcc;
2286 subscr->plmn_mnc = subscr->mnc;
2289 LOGP(DSUM, LOGL_INFO, "Location update accepted\n");
2290 LOGP(DMM, LOGL_INFO, "LOCATION UPDATING ACCEPT (mcc %s mnc %s "
2291 "lac 0x%04x)\n", gsm_print_mcc(subscr->mcc),
2292 gsm_print_mnc(subscr->mnc), subscr->lac);
2294 /* remove LA from forbidden list */
2295 gsm322_del_forbidden_la(ms, subscr->mcc, subscr->mnc, subscr->lac);
2298 if (TLVP_PRESENT(&tp, GSM48_IE_MOBILE_ID)) {
2303 mi = TLVP_VAL(&tp, GSM48_IE_MOBILE_ID)-1;
2306 mi_type = mi[1] & GSM_MI_TYPE_MASK;
2308 case GSM_MI_TYPE_TMSI:
2309 if (payload_len + sizeof(struct gsm48_loc_area_id) < 6
2312 memcpy(&tmsi, mi+2, 4);
2313 subscr->tmsi = ntohl(tmsi);
2314 LOGP(DMM, LOGL_INFO, "got TMSI 0x%08x (%u)\n",
2315 subscr->tmsi, subscr->tmsi);
2317 /* store LOCI on sim */
2318 gsm_subscr_write_loci(ms);
2321 case GSM_MI_TYPE_IMSI:
2322 LOGP(DMM, LOGL_INFO, "TMSI removed\n");
2323 subscr->tmsi = 0xffffffff;
2325 /* store LOCI on sim */
2326 gsm_subscr_write_loci(ms);
2328 /* send TMSI REALLOCATION COMPLETE */
2329 gsm48_mm_tx_tmsi_reall_cpl(ms);
2332 LOGP(DMM, LOGL_NOTICE, "TMSI reallocation with unknown "
2333 "MI type %d.\n", mi_type);
2337 /* send message to PLMN search process */
2338 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_SUCCESS);
2341 gsm322_plmn_sendmsg(ms, nmsg);
2343 /* follow on proceed */
2344 if (TLVP_PRESENT(&tp, GSM48_IE_MOBILE_ID))
2345 LOGP(DMM, LOGL_NOTICE, "follow-on proceed not supported.\n");
2347 /* start RR release timer */
2350 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
2355 /* 4.4.4.7 LOCATION UPDATING REJECT is received */
2356 static int gsm48_mm_rx_loc_upd_rej(struct osmocom_ms *ms, struct msgb *msg)
2358 struct gsm48_mmlayer *mm = &ms->mmlayer;
2359 struct gsm48_hdr *gh = msgb_l3(msg);
2360 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2362 if (payload_len < 1) {
2363 LOGP(DMM, LOGL_NOTICE, "Short read of LOCATION UPDATING REJECT "
2364 "message error.\n");
2368 /* RA was successfull */
2369 mm->lupd_ra_failure = 0;
2371 /* stop periodic location updating timer */
2372 stop_mm_t3212(mm); /* 4.4.2 */
2374 /* stop location update timer */
2377 /* store until RR is released */
2378 mm->lupd_rej_cause = *gh->data;
2380 /* start RR release timer */
2383 new_mm_state(mm, GSM48_MM_ST_LOC_UPD_REJ, 0);
2388 /* 4.4.4.7 RR is released after location update reject */
2389 static int gsm48_mm_rel_loc_upd_rej(struct osmocom_ms *ms, struct msgb *msg)
2391 struct gsm48_mmlayer *mm = &ms->mmlayer;
2392 struct gsm_subscriber *subscr = &ms->subscr;
2394 struct gsm322_msg *ngm;
2396 LOGP(DMM, LOGL_INFO, "Loc. upd. rejected (cause %d)\n",
2397 mm->lupd_rej_cause);
2399 /* stop RR release timer */
2403 switch (mm->lupd_rej_cause) {
2404 case GSM48_REJECT_IMSI_UNKNOWN_IN_HLR:
2405 case GSM48_REJECT_ILLEGAL_MS:
2406 case GSM48_REJECT_ILLEGAL_ME:
2407 /* reset attempt counter */
2408 mm->lupd_attempt = 0;
2411 subscr->sim_valid = 0;
2414 case GSM48_REJECT_PLMN_NOT_ALLOWED:
2415 case GSM48_REJECT_LOC_NOT_ALLOWED:
2416 case GSM48_REJECT_ROAMING_NOT_ALLOWED:
2417 /* TMSI and LAI invalid */
2418 subscr->tmsi = 0xffffffff;
2419 subscr->lac = 0x0000;
2421 /* key is invalid */
2422 subscr->key_seq = 7;
2425 new_sim_ustate(subscr, GSM_SIM_U3_ROAMING_NA);
2427 /* store LOCI on sim */
2428 gsm_subscr_write_loci(ms);
2430 /* update has finished */
2431 mm->lupd_pending = 0;
2434 /* send event to PLMN search process */
2435 switch(mm->lupd_rej_cause) {
2436 case GSM48_REJECT_ROAMING_NOT_ALLOWED:
2437 nmsg = gsm322_msgb_alloc(GSM322_EVENT_ROAMING_NA);
2439 case GSM48_REJECT_IMSI_UNKNOWN_IN_HLR:
2440 case GSM48_REJECT_ILLEGAL_MS:
2441 case GSM48_REJECT_ILLEGAL_ME:
2442 nmsg = gsm322_msgb_alloc(GSM322_EVENT_INVALID_SIM);
2445 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_FAILED);
2449 ngm = (struct gsm322_msg *)nmsg->data;
2450 ngm->reject = mm->lupd_rej_cause;
2451 gsm322_plmn_sendmsg(ms, nmsg);
2453 /* forbidden list */
2454 switch (mm->lupd_rej_cause) {
2455 case GSM48_REJECT_IMSI_UNKNOWN_IN_HLR:
2456 LOGP(DSUM, LOGL_INFO, "Location update failed (IMSI unknown "
2459 case GSM48_REJECT_ILLEGAL_MS:
2460 LOGP(DSUM, LOGL_INFO, "Location update failed (Illegal MS)\n");
2462 case GSM48_REJECT_ILLEGAL_ME:
2463 LOGP(DSUM, LOGL_INFO, "Location update failed (Illegal ME)\n");
2465 case GSM48_REJECT_PLMN_NOT_ALLOWED:
2466 gsm_subscr_add_forbidden_plmn(subscr, mm->lupd_mcc,
2467 mm->lupd_mnc, mm->lupd_rej_cause);
2468 LOGP(DSUM, LOGL_INFO, "Location update failed (PLMN not "
2471 case GSM48_REJECT_LOC_NOT_ALLOWED:
2472 case GSM48_REJECT_ROAMING_NOT_ALLOWED:
2473 gsm322_add_forbidden_la(ms, mm->lupd_mcc, mm->lupd_mnc,
2474 mm->lupd_lac, mm->lupd_rej_cause);
2475 LOGP(DSUM, LOGL_INFO, "Location update failed (LAI not "
2479 /* 4.4.4.9 continue with failure handling */
2480 return gsm48_mm_loc_upd_failed(ms, NULL);
2483 /* CS proc triggers: return to IDLE, case 13 is also handled there */
2487 /* 4.2.2 delay a location update */
2488 static int gsm48_mm_loc_upd_delay_per(struct osmocom_ms *ms, struct msgb *msg)
2490 struct gsm48_mmlayer *mm = &ms->mmlayer;
2492 LOGP(DMM, LOGL_INFO, "Schedule a pending periodic loc. upd.\n");
2493 mm->lupd_periodic = 1;
2498 /* delay a location update retry */
2499 static int gsm48_mm_loc_upd_delay_retry(struct osmocom_ms *ms, struct msgb *msg)
2501 struct gsm48_mmlayer *mm = &ms->mmlayer;
2503 LOGP(DMM, LOGL_INFO, "Schedule a pending periodic loc. upd.\n");
2509 /* process failues as described in the lower part of 4.4.4.9 */
2510 static int gsm48_mm_loc_upd_failed(struct osmocom_ms *ms, struct msgb *msg)
2512 struct gsm48_mmlayer *mm = &ms->mmlayer;
2513 struct gsm_subscriber *subscr = &ms->subscr;
2515 LOGP(DSUM, LOGL_INFO, "Location update failed\n");
2517 /* stop location update timer, if running */
2520 if (subscr->ustate == GSM_SIM_U1_UPDATED
2521 && mm->lupd_mcc == subscr->mcc
2522 && mm->lupd_mnc == subscr->mnc
2523 && mm->lupd_lac == subscr->lac) {
2524 if (mm->lupd_attempt < 4) {
2525 LOGP(DSUM, LOGL_INFO, "Try location update later\n");
2526 LOGP(DMM, LOGL_INFO, "Loc. upd. failed, retry #%d\n",
2529 /* start update retry timer */
2532 /* CS process will trigger: return to MM IDLE */
2535 LOGP(DMM, LOGL_INFO, "Loc. upd. failed too often.\n");
2538 /* TMSI and LAI invalid */
2539 subscr->tmsi = 0xffffffff;
2540 subscr->lac = 0x0000;
2542 /* key is invalid */
2543 subscr->key_seq = 7;
2546 new_sim_ustate(subscr, GSM_SIM_U2_NOT_UPDATED);
2548 /* store LOCI on sim */
2549 gsm_subscr_write_loci(ms);
2551 /* start update retry timer (RR connection is released) */
2552 if (mm->lupd_attempt < 4) {
2553 mm->start_t3211 = 1;
2554 LOGP(DSUM, LOGL_INFO, "Try location update later\n");
2557 /* CS process will trigger: return to MM IDLE */
2561 /* abort a location update due to radio failure or release */
2562 static int gsm48_mm_rel_loc_upd_abort(struct osmocom_ms *ms, struct msgb *msg)
2564 struct gsm48_mmlayer *mm = &ms->mmlayer;
2565 struct gsm48_rr_hdr *rrh = (struct gsm48_rr_hdr *)msg->data;
2567 /* stop RR release timer */
2570 if (rrh->msg_type == GSM48_RR_REL_IND) {
2571 LOGP(DMM, LOGL_INFO, "RR link released after loc. upd.\n");
2573 /* continue with failure handling */
2574 return gsm48_mm_loc_upd_failed(ms, NULL);
2577 LOGP(DMM, LOGL_INFO, "Loc. upd. aborted by radio (cause #%d)\n",
2580 /* random access failure, but not two successive failures */
2581 if (rrh->cause == RR_REL_CAUSE_RA_FAILURE && !mm->lupd_ra_failure) {
2582 mm->lupd_ra_failure = 1;
2584 /* start RA failure timer */
2590 /* RA was successfull or sent twice */
2591 mm->lupd_ra_failure = 0;
2593 /* continue with failure handling */
2594 return gsm48_mm_loc_upd_failed(ms, NULL);
2597 /* location update has timed out */
2598 static int gsm48_mm_loc_upd_timeout(struct osmocom_ms *ms, struct msgb *msg)
2601 struct gsm48_rr_hdr *nrrh;
2603 /* abort RR connection */
2604 nmsg = gsm48_rr_msgb_alloc(GSM48_RR_ABORT_REQ);
2607 nrrh = (struct gsm48_rr_hdr *) msgb_put(nmsg, sizeof(*nrrh));
2608 nrrh->cause = GSM48_RR_CAUSE_ABNORMAL_TIMER;
2609 gsm48_rr_downmsg(ms, nmsg);
2611 /* continue with failure handling */
2612 return gsm48_mm_loc_upd_failed(ms, NULL);
2616 * process handlers for MM connections
2619 /* cm reestablish request message from upper layer */
2620 static int gsm48_mm_tx_cm_serv_req(struct osmocom_ms *ms, int rr_prim,
2623 struct gsm48_mmlayer *mm = &ms->mmlayer;
2624 struct gsm_subscriber *subscr = &ms->subscr;
2625 struct gsm_settings *set = &ms->settings;
2627 struct gsm48_hdr *ngh;
2628 struct gsm48_service_request *nsr; /* NOTE: includes MI length */
2632 LOGP(DMM, LOGL_INFO, "CM SERVICE REQUEST (cause %d)\n", mm->est_cause);
2634 nmsg = gsm48_l3_msgb_alloc();
2637 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
2638 nsr = (struct gsm48_service_request *)msgb_put(nmsg, sizeof(*nsr));
2639 cm2lv = (uint8_t *)&nsr->classmark;
2641 ngh->proto_discr = GSM48_PDISC_MM;
2642 ngh->msg_type = GSM48_MT_MM_CM_SERV_REQ;
2645 nsr->cm_service_type = cm_serv;
2646 nsr->cipher_key_seq = subscr->key_seq;
2648 cm2lv[0] = sizeof(struct gsm48_classmark2);
2649 gsm48_rr_enc_cm2(ms, (struct gsm48_classmark2 *)(cm2lv + 1));
2651 if (mm->est_cause == RR_EST_CAUSE_EMERGENCY && set->emergency_imsi[0]) {
2652 LOGP(DMM, LOGL_INFO, "-> Using IMSI %s for emergency\n",
2653 set->emergency_imsi);
2654 gsm48_generate_mid_from_imsi(buf, set->emergency_imsi);
2656 if (!subscr->sim_valid) { /* have no SIM ? */
2657 LOGP(DMM, LOGL_INFO, "-> Using IMEI %s\n",
2659 gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_IMEI);
2661 if (subscr->tmsi != 0xffffffff) { /* have TMSI ? */
2662 gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_TMSI);
2663 LOGP(DMM, LOGL_INFO, "-> Using TMSI\n");
2665 gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_IMSI);
2666 LOGP(DMM, LOGL_INFO, "-> Using IMSI %s\n",
2669 msgb_put(nmsg, buf[1]); /* length is part of nsr */
2670 memcpy(&nsr->mi_len, buf + 1, 1 + buf[1]);
2671 /* prio is optional for eMLPP */
2673 /* push RR header and send down */
2674 return gsm48_mm_to_rr(ms, nmsg, rr_prim, mm->est_cause);
2677 /* cm service abort message from upper layer
2678 * NOTE: T3240 is started by the calling function
2680 static int gsm48_mm_tx_cm_service_abort(struct osmocom_ms *ms)
2683 struct gsm48_hdr *ngh;
2685 LOGP(DMM, LOGL_INFO, "CM SERVICE ABORT\n");
2687 nmsg = gsm48_l3_msgb_alloc();
2690 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
2692 ngh->proto_discr = GSM48_PDISC_MM;
2693 ngh->msg_type = GSM48_MT_MM_CM_SERV_ABORT;
2695 /* push RR header and send down */
2696 return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_DATA_REQ, 0);
2699 /* cm service acknowledge is received from lower layer */
2700 static int gsm48_mm_rx_cm_service_acc(struct osmocom_ms *ms, struct msgb *msg)
2702 struct gsm48_mmlayer *mm = &ms->mmlayer;
2704 /* stop MM connection timer */
2707 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
2709 return gsm48_mm_conn_go_dedic(ms);
2712 /* 9.2.6 CM SERVICE REJECT message received */
2713 static int gsm48_mm_rx_cm_service_rej(struct osmocom_ms *ms, struct msgb *msg)
2715 struct gsm48_mmlayer *mm = &ms->mmlayer;
2716 struct gsm_subscriber *subscr = &ms->subscr;
2717 struct gsm48_hdr *gh = msgb_l3(msg);
2718 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2719 uint8_t abort_any = 0;
2720 uint8_t reject_cause;
2722 if (payload_len < 1) {
2723 LOGP(DMM, LOGL_NOTICE, "Short read of cm service reject "
2724 "message error.\n");
2729 reject_cause = *gh->data;
2731 LOGP(DMM, LOGL_INFO, "CM SERVICE REJECT (cause %d)\n", reject_cause);
2733 /* stop MM connection timer */
2736 /* selection action on cause value */
2737 switch (reject_cause) {
2738 case GSM48_REJECT_IMSI_UNKNOWN_IN_VLR:
2739 case GSM48_REJECT_ILLEGAL_ME:
2742 /* TMSI and LAI invalid */
2743 subscr->tmsi = 0xffffffff;
2744 subscr->lac = 0x0000;
2746 /* key is invalid */
2747 subscr->key_seq = 7;
2750 new_sim_ustate(subscr, GSM_SIM_U2_NOT_UPDATED);
2752 /* store LOCI on sim */
2753 gsm_subscr_write_loci(ms);
2755 /* change to WAIT_NETWORK_CMD state impied by abort_any == 1 */
2757 if (reject_cause == GSM48_REJECT_ILLEGAL_ME)
2758 subscr->sim_valid = 0;
2762 /* state implied by the number of remaining connections */
2766 /* release MM connection(s) */
2767 gsm48_mm_release_mm_conn(ms, abort_any, 16, 0);
2769 /* state depends on the existance of remaining MM connections */
2770 if (llist_empty(&mm->mm_conn))
2771 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
2773 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
2778 /* initiate an MM connection 4.5.1.1
2780 * this function is called when:
2781 * - no RR connection exists
2782 * - an RR connection exists, but this is the first MM connection
2783 * - an RR connection exists, and there are already MM connection(s)
2785 static int gsm48_mm_init_mm(struct osmocom_ms *ms, struct msgb *msg,
2788 struct gsm48_mmlayer *mm = &ms->mmlayer;
2789 struct gsm_subscriber *subscr = &ms->subscr;
2790 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
2791 int msg_type = mmh->msg_type;
2793 uint8_t cause = 0, cm_serv = 0, proto = 0;
2795 struct gsm48_mmxx_hdr *nmmh;
2796 struct gsm48_mm_conn *conn, *conn_found = NULL;
2798 /* reset loc. upd. counter on CM service request */
2799 mm->lupd_attempt = 0;
2801 /* find if there is already a pending connection */
2802 llist_for_each_entry(conn, &mm->mm_conn, list) {
2803 if (conn->state == GSM48_MMXX_ST_CONN_PEND) {
2809 /* if pending connection */
2811 LOGP(DMM, LOGL_INFO, "Init MM Connection, but already have "
2812 "pending MM Connection.\n");
2817 case GSM48_MMCC_EST_REQ:
2818 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_REL_IND,
2819 mmh->ref, mmh->transaction_id);
2821 case GSM48_MMSS_EST_REQ:
2822 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSS_REL_IND,
2823 mmh->ref, mmh->transaction_id);
2825 case GSM48_MMSMS_EST_REQ:
2826 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSMS_REL_IND,
2827 mmh->ref, mmh->transaction_id);
2832 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
2833 nmmh->cause = cause;
2834 gsm48_mmxx_upmsg(ms, nmsg);
2838 /* in case of an emergency setup */
2839 if (msg_type == GSM48_MMCC_EST_REQ && mmh->emergency)
2842 /* if sim is not updated */
2843 if (!emergency && subscr->ustate != GSM_SIM_U1_UPDATED) {
2844 LOGP(DMM, LOGL_INFO, "Init MM Connection, but SIM not "
2850 if (mm->state == GSM48_MM_ST_MM_IDLE) {
2851 /* current MM idle state */
2852 switch (mm->substate) {
2853 case GSM48_MM_SST_NORMAL_SERVICE:
2854 case GSM48_MM_SST_PLMN_SEARCH_NORMAL:
2855 LOGP(DMM, LOGL_INFO, "Init MM Connection.\n");
2856 break; /* allow when normal */
2857 case GSM48_MM_SST_LOC_UPD_NEEDED:
2858 case GSM48_MM_SST_ATTEMPT_UPDATE:
2859 /* store mm request if attempting to update */
2861 LOGP(DMM, LOGL_INFO, "Init MM Connection, but "
2862 "attempting to update.\n");
2865 /* TODO: implement delay and start loc upd. */
2869 /* reject if not emergency */
2871 LOGP(DMM, LOGL_INFO, "Init MM Connection, not "
2872 "in normal state.\n");
2879 LOGP(DMM, LOGL_INFO, "Init another MM Connection.\n");
2881 /* set cause, service, proto */
2883 case GSM48_MMCC_EST_REQ:
2885 cause = RR_EST_CAUSE_EMERGENCY;
2886 cm_serv = GSM48_CMSERV_EMERGENCY;
2888 cause = RR_EST_CAUSE_ORIG_TCHF;
2889 cm_serv = GSM48_CMSERV_MO_CALL_PACKET;
2891 proto = GSM48_PDISC_CC;
2893 case GSM48_MMSS_EST_REQ:
2894 cause = RR_EST_CAUSE_OTHER_SDCCH;
2895 cm_serv = GSM48_CMSERV_SUP_SERV;
2896 proto = GSM48_PDISC_NC_SS;
2898 case GSM48_MMSMS_EST_REQ:
2899 cause = RR_EST_CAUSE_OTHER_SDCCH;
2900 cm_serv = GSM48_CMSERV_SMS;
2901 proto = GSM48_PDISC_SMS;
2905 /* create MM connection instance */
2906 conn = mm_conn_new(mm, proto, mmh->transaction_id, mmh->ref);
2910 new_conn_state(conn, GSM48_MMXX_ST_CONN_PEND);
2912 /* send CM SERVICE REQUEST */
2914 mm->est_cause = cause;
2915 return gsm48_mm_tx_cm_serv_req(ms, rr_prim, cm_serv);
2920 /* 4.5.1.1 a) MM connection request triggers RR connection */
2921 static int gsm48_mm_init_mm_no_rr(struct osmocom_ms *ms, struct msgb *msg)
2923 struct gsm48_mmlayer *mm = &ms->mmlayer;
2926 /* start MM connection by requesting RR connection */
2927 rc = gsm48_mm_init_mm(ms, msg, GSM48_RR_EST_REQ);
2931 new_mm_state(mm, GSM48_MM_ST_WAIT_RR_CONN_MM_CON, 0);
2936 /* 4.5.1.1 a) RR is esablised during mm connection, wait for CM accepted */
2937 static int gsm48_mm_est_mm_con(struct osmocom_ms *ms, struct msgb *msg)
2939 struct gsm48_mmlayer *mm = &ms->mmlayer;
2941 /* 4.5.1.7 if there is no more MM connection */
2942 if (llist_empty(&mm->mm_conn)) {
2943 LOGP(DMM, LOGL_INFO, "MM Connection, are already gone.\n");
2945 /* start RR release timer */
2948 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
2951 return gsm48_mm_tx_cm_service_abort(ms);
2954 /* start MM connection timer */
2957 new_mm_state(mm, GSM48_MM_ST_WAIT_OUT_MM_CONN, 0);
2962 /* 4.5.1.1 b) MM connection request on existing RR connection */
2963 static int gsm48_mm_init_mm_first(struct osmocom_ms *ms, struct msgb *msg)
2965 struct gsm48_mmlayer *mm = &ms->mmlayer;
2968 /* start MM connection by sending data */
2969 rc = gsm48_mm_init_mm(ms, msg, GSM48_RR_DATA_REQ);
2973 /* stop "RR connection release not allowed" timer */
2976 /* start MM connection timer */
2979 new_mm_state(mm, GSM48_MM_ST_WAIT_OUT_MM_CONN, 0);
2984 /* 4.5.1.1 b) another MM connection request on existing RR connection */
2985 static int gsm48_mm_init_mm_more(struct osmocom_ms *ms, struct msgb *msg)
2987 struct gsm48_mmlayer *mm = &ms->mmlayer;
2990 /* start MM connection by sending data */
2991 rc = gsm48_mm_init_mm(ms, msg, GSM48_RR_DATA_REQ);
2995 /* start MM connection timer */
2998 new_mm_state(mm, GSM48_MM_ST_WAIT_ADD_OUT_MM_CON, 0);
3003 /* 4.5.1.1 b) delay on WAIT FOR NETWORK COMMAND state */
3004 static int gsm48_mm_init_mm_wait(struct osmocom_ms *ms, struct msgb *msg)
3007 gsm48_mm_init_mm_reject(ms, msg);
3009 this requires handling when leaving this state...
3011 struct gsm48_mmlayer *mm = &ms->mmlayer;
3014 /* just create the MM connection in pending state */
3015 rc = gsm48_mm_init_mm(ms, msg, 0);
3019 /* start MM connection timer */
3022 new_mm_state(mm, GSM48_MM_ST_WAIT_ADD_OUT_MM_CON, 0);
3028 /* initiate an mm connection other cases */
3029 static int gsm48_mm_init_mm_reject(struct osmocom_ms *ms, struct msgb *msg)
3031 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3032 int msg_type = mmh->msg_type;
3034 struct gsm48_mmxx_hdr *nmmh;
3039 case GSM48_MMCC_EST_REQ:
3040 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_REL_IND, mmh->ref,
3041 mmh->transaction_id);
3043 case GSM48_MMSS_EST_REQ:
3044 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSS_REL_IND, mmh->ref,
3045 mmh->transaction_id);
3047 case GSM48_MMSMS_EST_REQ:
3048 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSMS_REL_IND, mmh->ref,
3049 mmh->transaction_id);
3054 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
3056 gsm48_mmxx_upmsg(ms, nmsg);
3061 /* accepting pending connection, got dedicated mode
3063 * this function is called:
3064 * - when ciphering command is received
3065 * - when cm service is accepted
3067 static int gsm48_mm_conn_go_dedic(struct osmocom_ms *ms)
3069 struct gsm48_mmlayer *mm = &ms->mmlayer;
3070 struct gsm48_mm_conn *conn, *conn_found = NULL;
3072 struct gsm48_mmxx_hdr *nmmh;
3074 /* the first and only pending connection is the recent requested */
3075 llist_for_each_entry(conn, &mm->mm_conn, list) {
3076 if (conn->state == GSM48_MMXX_ST_CONN_PEND) {
3082 /* if no pending connection (anymore) */
3084 LOGP(DMM, LOGL_INFO, "No pending MM Connection.\n");
3089 new_conn_state(conn, GSM48_MMXX_ST_DEDICATED);
3091 /* send establishment confirm */
3093 switch(conn_found->protocol) {
3094 case GSM48_PDISC_CC:
3095 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_EST_CNF, conn->ref,
3096 conn->transaction_id);
3098 case GSM48_PDISC_NC_SS:
3099 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSS_EST_CNF, conn->ref,
3100 conn->transaction_id);
3102 case GSM48_PDISC_SMS:
3103 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSMS_EST_CNF, conn->ref,
3104 conn->transaction_id);
3109 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
3111 gsm48_mmxx_upmsg(ms, nmsg);
3116 /* a RR-SYNC-IND is received during MM connection establishment */
3117 static int gsm48_mm_sync_ind_wait(struct osmocom_ms *ms, struct msgb *msg)
3119 struct gsm48_mmlayer *mm = &ms->mmlayer;
3121 /* stop MM connection timer */
3124 return gsm48_mm_conn_go_dedic(ms);
3127 /* a RR-SYNC-IND is received during MM connection active */
3128 static int gsm48_mm_sync_ind_active(struct osmocom_ms *ms, struct msgb *msg)
3130 struct gsm48_mmlayer *mm = &ms->mmlayer;
3131 struct gsm48_mm_conn *conn;
3133 struct gsm48_mmxx_hdr *nmmh;
3135 /* stop MM connection timer */
3138 /* broadcast all MMCC connection(s) */
3139 llist_for_each_entry(conn, &mm->mm_conn, list) {
3140 /* send MMCC-SYNC-IND */
3142 switch(conn->protocol) {
3143 case GSM48_PDISC_CC:
3144 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_SYNC_IND,
3145 conn->ref, conn->transaction_id);
3149 continue; /* skip if not of CC type */
3150 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
3152 /* copy L3 message */
3153 nmsg->l3h = msgb_put(nmsg, msgb_l3len(msg));
3154 memcpy(nmsg->l3h, msg->l3h, msgb_l3len(msg));
3155 gsm48_mmxx_upmsg(ms, nmsg);
3161 /* 4.5.1.2 RR abort/release is received during MM connection establishment */
3162 static int gsm48_mm_abort_mm_con(struct osmocom_ms *ms, struct msgb *msg)
3164 struct gsm48_mmlayer *mm = &ms->mmlayer;
3165 struct gsm48_rr_hdr *rrh = (struct gsm48_rr_hdr *)msg->data;
3168 /* stop RR release timer */
3171 /* this conversion is not of any standard */
3172 switch(rrh->cause) {
3173 case RR_REL_CAUSE_NOT_AUTHORIZED:
3174 case RR_REL_CAUSE_EMERGENCY_ONLY:
3175 case RR_REL_CAUSE_TRY_LATER:
3178 case RR_REL_CAUSE_NORMAL:
3185 /* stop MM connection timer */
3188 /* release all connections */
3189 gsm48_mm_release_mm_conn(ms, 1, cause, 1);
3191 /* no RR connection, so we return to MM IDLE */
3192 if (mm->state == GSM48_MM_ST_WAIT_RR_CONN_MM_CON)
3193 return gsm48_mm_return_idle(ms, NULL);
3195 /* CS process will trigger: return to MM IDLE */
3199 /* 4.5.1.2 timeout is received during MM connection establishment */
3200 static int gsm48_mm_timeout_mm_con(struct osmocom_ms *ms, struct msgb *msg)
3202 struct gsm48_mmlayer *mm = &ms->mmlayer;
3204 /* release pending connection */
3205 gsm48_mm_release_mm_conn(ms, 0, 102, 0);
3207 /* state depends on the existance of remaining MM connections */
3208 if (llist_empty(&mm->mm_conn)) {
3209 /* start RR release timer */
3212 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
3214 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
3219 /* respond to paging */
3220 static int gsm48_mm_est(struct osmocom_ms *ms, struct msgb *msg)
3222 struct gsm48_mmlayer *mm = &ms->mmlayer;
3224 mm->est_cause = RR_EST_CAUSE_ANS_PAG_ANY;
3225 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
3231 static int gsm48_mm_data(struct osmocom_ms *ms, struct msgb *msg)
3233 struct gsm48_mmlayer *mm = &ms->mmlayer;
3234 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3235 struct gsm48_mm_conn *conn;
3236 int msg_type = mmh->msg_type;
3238 /* get connection, if not exist (anymore), release */
3239 conn = mm_conn_by_ref(mm, mmh->ref);
3241 LOGP(DMM, LOGL_INFO, "MMXX_DATA_REQ with unknown (already "
3242 "released) ref=%x, sending MMXX_REL_IND\n", mmh->ref);
3243 switch(msg_type & GSM48_MMXX_MASK) {
3244 case GSM48_MMCC_CLASS:
3245 mmh->msg_type = GSM48_MMCC_REL_IND;
3247 case GSM48_MMSS_CLASS:
3248 mmh->msg_type = GSM48_MMSS_REL_IND;
3250 case GSM48_MMSMS_CLASS:
3251 mmh->msg_type = GSM48_MMSMS_REL_IND;
3256 /* mirror message with REL_IND + cause */
3257 return gsm48_mmxx_upmsg(ms, msg);
3260 /* pull MM header */
3261 msgb_pull(msg, sizeof(struct gsm48_mmxx_hdr));
3263 /* push RR header and send down */
3264 return gsm48_mm_to_rr(ms, msg, GSM48_RR_DATA_REQ, 0);
3267 /* release of MM connection (active state) */
3268 static int gsm48_mm_release_active(struct osmocom_ms *ms, struct msgb *msg)
3270 struct gsm48_mmlayer *mm = &ms->mmlayer;
3271 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3272 struct gsm48_mm_conn *conn;
3274 /* get connection, if not exist (anymore), release */
3275 conn = mm_conn_by_ref(mm, mmh->ref);
3279 /* state depends on the existance of remaining MM connections */
3280 if (llist_empty(&mm->mm_conn)) {
3281 /* start RR release timer */
3284 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
3286 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
3291 /* release of MM connection (wait for additional state) */
3292 static int gsm48_mm_release_wait_add(struct osmocom_ms *ms, struct msgb *msg)
3294 struct gsm48_mmlayer *mm = &ms->mmlayer;
3295 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3296 struct gsm48_mm_conn *conn;
3298 /* get connection, if not exist (anymore), release */
3299 conn = mm_conn_by_ref(mm, mmh->ref);
3306 /* release of MM connection (wait for active state) */
3307 static int gsm48_mm_release_wait_active(struct osmocom_ms *ms, struct msgb *msg)
3309 struct gsm48_mmlayer *mm = &ms->mmlayer;
3310 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3311 struct gsm48_mm_conn *conn;
3313 /* get connection, if not exist (anymore), release */
3314 conn = mm_conn_by_ref(mm, mmh->ref);
3318 /* 4.5.1.7 if there is no MM connection during wait for active state */
3319 if (llist_empty(&mm->mm_conn)) {
3320 LOGP(DMM, LOGL_INFO, "No MM Connection during 'wait for "
3321 "active' state.\n");
3323 /* start RR release timer */
3326 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
3329 return gsm48_mm_tx_cm_service_abort(ms);
3335 /* release of MM connection (wait for RR state) */
3336 static int gsm48_mm_release_wait_rr(struct osmocom_ms *ms, struct msgb *msg)
3338 struct gsm48_mmlayer *mm = &ms->mmlayer;
3339 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3340 struct gsm48_mm_conn *conn;
3342 /* get connection, if not exist (anymore), release */
3343 conn = mm_conn_by_ref(mm, mmh->ref);
3347 /* later, if RR connection is established, the CM SERIVE ABORT
3348 * message will be sent
3353 /* abort RR connection (due to T3240) */
3354 static int gsm48_mm_abort_rr(struct osmocom_ms *ms, struct msgb *msg)
3357 struct gsm48_rr_hdr *nrrh;
3359 /* send abort to RR */
3360 nmsg = gsm48_rr_msgb_alloc(GSM48_RR_ABORT_REQ);
3363 nrrh = (struct gsm48_rr_hdr *) msgb_put(nmsg, sizeof(*nrrh));
3364 nrrh->cause = GSM48_RR_CAUSE_ABNORMAL_TIMER;
3365 gsm48_rr_downmsg(ms, nmsg);
3367 /* CS process will trigger: return to MM IDLE / No SIM */
3375 /* RR is released in other states */
3376 static int gsm48_mm_rel_other(struct osmocom_ms *ms, struct msgb *msg)
3378 struct gsm48_mmlayer *mm = &ms->mmlayer;
3380 /* stop RR release timer (if running) */
3383 /* CS process will trigger: return to MM IDLE */
3391 /* state trasitions for MMxx-SAP messages from upper layers */
3392 static struct downstate {
3396 int (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3397 } downstatelist[] = {
3398 /* 4.2.2.1 Normal service */
3399 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3400 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3402 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3403 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_no_rr},
3405 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3406 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_no_rr},
3408 /* 4.2.2.2 Attempt to update / Loc. Upd. needed */
3409 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE) |
3410 SBIT(GSM48_MM_SST_LOC_UPD_NEEDED),
3411 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr}, /* emergency only */
3413 /* 4.2.2.3 Limited service */
3414 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3415 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3417 /* 4.2.2.4 No IMSI */
3418 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NO_IMSI),
3419 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3421 /* 4.2.2.5 PLMN search, normal service */
3422 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3423 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3425 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3426 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_no_rr},
3428 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3429 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_no_rr},
3431 /* 4.2.2.6 PLMN search */
3432 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH),
3433 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3435 /* 4.5.1.1 MM Connection (EST) */
3436 {SBIT(GSM48_MM_ST_RR_CONN_RELEASE_NA), ALL_STATES,
3437 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_first},
3439 {SBIT(GSM48_MM_ST_RR_CONN_RELEASE_NA), ALL_STATES,
3440 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_first},
3442 {SBIT(GSM48_MM_ST_RR_CONN_RELEASE_NA), ALL_STATES,
3443 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_first},
3445 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3446 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_more},
3448 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3449 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_more},
3451 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3452 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_more},
3454 {SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD), ALL_STATES,
3455 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_wait},
3457 {SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD), ALL_STATES,
3458 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_wait},
3460 {SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD), ALL_STATES,
3461 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_wait},
3463 {ALL_STATES, ALL_STATES,
3464 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_reject},
3466 {ALL_STATES, ALL_STATES,
3467 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_reject},
3469 {ALL_STATES, ALL_STATES,
3470 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_reject},
3472 /* 4.5.2.1 MM Connection (DATA) */
3473 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3474 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3475 GSM48_MMCC_DATA_REQ, gsm48_mm_data},
3477 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3478 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3479 GSM48_MMSS_DATA_REQ, gsm48_mm_data},
3481 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3482 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3483 GSM48_MMSMS_DATA_REQ, gsm48_mm_data},
3485 /* 4.5.2.1 MM Connection (REL) */
3486 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3487 GSM48_MMCC_REL_REQ, gsm48_mm_release_active},
3489 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3490 GSM48_MMSS_REL_REQ, gsm48_mm_release_active},
3492 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3493 GSM48_MMSMS_REL_REQ, gsm48_mm_release_active},
3495 {SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3496 GSM48_MMCC_REL_REQ, gsm48_mm_release_wait_add},
3498 {SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3499 GSM48_MMSS_REL_REQ, gsm48_mm_release_wait_add},
3501 {SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3502 GSM48_MMSMS_REL_REQ, gsm48_mm_release_wait_add},
3504 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN), ALL_STATES,
3505 GSM48_MMCC_REL_REQ, gsm48_mm_release_wait_active},
3507 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN), ALL_STATES,
3508 GSM48_MMSS_REL_REQ, gsm48_mm_release_wait_active},
3510 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN), ALL_STATES,
3511 GSM48_MMSMS_REL_REQ, gsm48_mm_release_wait_active},
3513 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON), ALL_STATES,
3514 GSM48_MMCC_REL_REQ, gsm48_mm_release_wait_rr},
3516 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON), ALL_STATES,
3517 GSM48_MMSS_REL_REQ, gsm48_mm_release_wait_rr},
3519 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON), ALL_STATES,
3520 GSM48_MMSMS_REL_REQ, gsm48_mm_release_wait_rr},
3524 (sizeof(downstatelist) / sizeof(struct downstate))
3526 int gsm48_mmxx_downmsg(struct osmocom_ms *ms, struct msgb *msg)
3528 struct gsm48_mmlayer *mm = &ms->mmlayer;
3529 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3530 int msg_type = mmh->msg_type;
3531 struct gsm48_mm_conn *conn;
3534 /* keep up to date with the transaction ID */
3535 conn = mm_conn_by_ref(mm, mmh->ref);
3537 conn->transaction_id = mmh->transaction_id;
3539 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' event in state %s\n",
3540 ms->name, get_mmxx_name(msg_type),
3541 gsm48_mm_state_names[mm->state]);
3542 if (mm->state == GSM48_MM_ST_MM_IDLE)
3543 LOGP(DMM, LOGL_INFO, "-> substate %s\n",
3544 gsm48_mm_substate_names[mm->substate]);
3545 LOGP(DMM, LOGL_INFO, "-> callref %x, transaction_id %d\n",
3546 mmh->ref, mmh->transaction_id);
3548 /* Find function for current state and message */
3549 for (i = 0; i < DOWNSLLEN; i++)
3550 if ((msg_type == downstatelist[i].type)
3551 && ((1 << mm->state) & downstatelist[i].states)
3552 && ((1 << mm->substate) & downstatelist[i].substates))
3554 if (i == DOWNSLLEN) {
3555 LOGP(DMM, LOGL_NOTICE, "Message unhandled at this state.\n");
3560 rc = downstatelist[i].rout(ms, msg);
3562 if (downstatelist[i].rout != gsm48_mm_data)
3568 /* state trasitions for radio ressource messages (lower layer) */
3569 static struct rrdatastate {
3572 int (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3573 } rrdatastatelist[] = {
3575 {SBIT(GSM48_MM_ST_MM_IDLE),
3576 GSM48_RR_EST_IND, gsm48_mm_est},
3579 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_IMSI_D), /* 4.3.4.4 */
3580 GSM48_RR_EST_CNF, gsm48_mm_imsi_detach_sent},
3582 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_IMSI_D), /* 4.3.4.4 (unsuc.) */
3583 GSM48_RR_REL_IND, gsm48_mm_imsi_detach_end},
3584 /* also this may happen if SABM is ackwnowledged with DISC */
3586 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_IMSI_D), /* 4.3.4.4 (lost) */
3587 GSM48_RR_ABORT_IND, gsm48_mm_imsi_detach_end},
3589 {SBIT(GSM48_MM_ST_IMSI_DETACH_INIT), /* 4.3.4.4 (unsuc.) */
3590 GSM48_RR_REL_IND, gsm48_mm_imsi_detach_end},
3592 {SBIT(GSM48_MM_ST_IMSI_DETACH_INIT), /* 4.3.4.4 (lost) */
3593 GSM48_RR_ABORT_IND, gsm48_mm_imsi_detach_end},
3595 /* location update */
3596 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_LUPD), /* 4.4.4.1 */
3597 GSM48_RR_EST_CNF, gsm48_mm_est_loc_upd},
3599 {SBIT(GSM48_MM_ST_LOC_UPD_INIT) |
3600 SBIT(GSM48_MM_ST_WAIT_RR_CONN_LUPD), /* 4.4.4.9 */
3601 GSM48_RR_REL_IND, gsm48_mm_rel_loc_upd_abort},
3603 {SBIT(GSM48_MM_ST_LOC_UPD_INIT) |
3604 SBIT(GSM48_MM_ST_WAIT_RR_CONN_LUPD), /* 4.4.4.9 */
3605 GSM48_RR_ABORT_IND, gsm48_mm_rel_loc_upd_abort},
3607 {SBIT(GSM48_MM_ST_LOC_UPD_REJ), /* 4.4.4.7 */
3608 GSM48_RR_REL_IND, gsm48_mm_rel_loc_upd_rej},
3610 {SBIT(GSM48_MM_ST_LOC_UPD_REJ), /* 4.4.4.7 */
3611 GSM48_RR_ABORT_IND, gsm48_mm_rel_loc_upd_rej},
3613 /* MM connection (EST) */
3614 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON), /* 4.5.1.1 */
3615 GSM48_RR_EST_CNF, gsm48_mm_est_mm_con},
3617 /* MM connection (DATA) */
3619 GSM48_RR_DATA_IND, gsm48_mm_data_ind},
3621 /* MM connection (SYNC) */
3622 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN) |
3623 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), /* 4.5.1.1 */
3624 GSM48_RR_SYNC_IND, gsm48_mm_sync_ind_wait},
3626 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE),
3627 GSM48_RR_SYNC_IND, gsm48_mm_sync_ind_active},
3629 /* MM connection (REL/ABORT) */
3630 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON) |
3631 SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN) |
3632 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), /* 4.5.1.2 */
3633 GSM48_RR_REL_IND, gsm48_mm_abort_mm_con},
3635 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON) |
3636 SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN) |
3637 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), /* 4.5.1.2 */
3638 GSM48_RR_ABORT_IND, gsm48_mm_abort_mm_con},
3640 /* MM connection (REL/ABORT with re-establishment possibility) */
3641 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), /* not supported */
3642 GSM48_RR_REL_IND, gsm48_mm_abort_mm_con},
3644 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3645 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), /* not supported */
3646 GSM48_RR_ABORT_IND, gsm48_mm_abort_mm_con},
3648 /* other (also wait for network command) */
3650 GSM48_RR_REL_IND, gsm48_mm_rel_other},
3653 GSM48_RR_ABORT_IND, gsm48_mm_rel_other},
3656 #define RRDATASLLEN \
3657 (sizeof(rrdatastatelist) / sizeof(struct rrdatastate))
3659 static int gsm48_rcv_rr(struct osmocom_ms *ms, struct msgb *msg)
3661 struct gsm48_mmlayer *mm = &ms->mmlayer;
3662 struct gsm48_rr_hdr *rrh = (struct gsm48_rr_hdr *)msg->data;
3663 int msg_type = rrh->msg_type;
3666 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' from RR in state %s\n",
3667 ms->name, get_rr_name(msg_type),
3668 gsm48_mm_state_names[mm->state]);
3670 /* find function for current state and message */
3671 for (i = 0; i < RRDATASLLEN; i++)
3672 if ((msg_type == rrdatastatelist[i].type)
3673 && ((1 << mm->state) & rrdatastatelist[i].states))
3675 if (i == RRDATASLLEN) {
3676 LOGP(DMM, LOGL_NOTICE, "Message unhandled at this state.\n");
3681 rc = rrdatastatelist[i].rout(ms, msg);
3683 if (rrdatastatelist[i].rout != gsm48_mm_data_ind)
3689 /* state trasitions for mobile managemnt messages (lower layer) */
3690 static struct mmdatastate {
3693 int (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3694 } mmdatastatelist[] = {
3695 {ALL_STATES, /* 4.3.1.2 */
3696 GSM48_MT_MM_TMSI_REALL_CMD, gsm48_mm_rx_tmsi_realloc_cmd},
3698 {ALL_STATES, /* 4.3.2.2 */
3699 GSM48_MT_MM_AUTH_REQ, gsm48_mm_rx_auth_req},
3701 {ALL_STATES, /* 4.3.2.5 */
3702 GSM48_MT_MM_AUTH_REJ, gsm48_mm_rx_auth_rej},
3704 {ALL_STATES, /* 4.3.3.2 */
3705 GSM48_MT_MM_ID_REQ, gsm48_mm_rx_id_req},
3707 {ALL_STATES, /* 4.3.5.2 */
3708 GSM48_MT_MM_ABORT, gsm48_mm_rx_abort},
3710 {ALL_STATES, /* 4.3.6.2 */
3711 GSM48_MT_MM_INFO, gsm48_mm_rx_info},
3713 {SBIT(GSM48_MM_ST_LOC_UPD_INIT), /* 4.4.4.6 */
3714 GSM48_MT_MM_LOC_UPD_ACCEPT, gsm48_mm_rx_loc_upd_acc},
3716 {SBIT(GSM48_MM_ST_LOC_UPD_INIT), /* 4.4.4.7 */
3717 GSM48_MT_MM_LOC_UPD_REJECT, gsm48_mm_rx_loc_upd_rej},
3719 {ALL_STATES, /* 4.5.1.1 */
3720 GSM48_MT_MM_CM_SERV_ACC, gsm48_mm_rx_cm_service_acc},
3722 {ALL_STATES, /* 4.5.1.1 */
3723 GSM48_MT_MM_CM_SERV_REJ, gsm48_mm_rx_cm_service_rej},
3726 #define MMDATASLLEN \
3727 (sizeof(mmdatastatelist) / sizeof(struct mmdatastate))
3729 static int gsm48_mm_data_ind(struct osmocom_ms *ms, struct msgb *msg)
3731 struct gsm48_mmlayer *mm = &ms->mmlayer;
3732 struct gsm48_hdr *gh = msgb_l3(msg);
3733 uint8_t pdisc = gh->proto_discr & 0x0f;
3734 uint8_t msg_type = gh->msg_type & 0xbf;
3735 struct gsm48_mmxx_hdr *mmh;
3736 int msg_supported = 0; /* determine, if message is supported at all */
3737 int rr_prim = -1, rr_est = -1; /* no prim set */
3742 if (msg_type == GSM48_MT_MM_NULL)
3745 if (mm->state == GSM48_MM_ST_IMSI_DETACH_INIT) {
3746 LOGP(DMM, LOGL_NOTICE, "DATA IND ignored during IMSI "
3750 /* pull the RR header */
3751 msgb_pull(msg, sizeof(struct gsm48_rr_hdr));
3753 /* create transaction (if not exists) and push message */
3755 case GSM48_PDISC_CC:
3756 rr_prim = GSM48_MMCC_DATA_IND;
3757 rr_est = GSM48_MMCC_EST_IND;
3760 case GSM48_PDISC_NC_SS:
3761 rr_prim = GSM48_MMSS_DATA_IND;
3762 rr_est = GSM48_MMSS_EST_IND;
3764 case GSM48_PDISC_SMS:
3765 rr_prim = GSM48_MMSMS_DATA_IND;
3766 rr_est = GSM48_MMSMS_EST_IND;
3770 if (rr_prim != -1) {
3771 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4;
3773 struct gsm48_mm_conn *conn;
3775 /* find transaction, if any */
3776 conn = mm_conn_by_id(mm, pdisc, transaction_id);
3778 /* create MM connection instance */
3780 conn = mm_conn_new(mm, pdisc, transaction_id,
3787 /* push new header */
3788 msgb_push(msg, sizeof(struct gsm48_mmxx_hdr));
3789 mmh = (struct gsm48_mmxx_hdr *)msg->data;
3790 mmh->msg_type = rr_prim;
3791 mmh->ref = conn->ref;
3793 /* go MM CONN ACTIVE state */
3794 if (mm->state == GSM48_MM_ST_WAIT_NETWORK_CMD
3795 || mm->state == GSM48_MM_ST_RR_CONN_RELEASE_NA) {
3796 /* stop RR release timer */
3799 /* stop "RR connection release not allowed" timer */
3802 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
3806 /* forward message */
3808 case GSM48_PDISC_MM:
3809 skip_ind = (gh->proto_discr & 0xf0) >> 4;
3811 /* ignore if skip indicator is not B'0000' */
3814 break; /* follow the selection proceedure below */
3816 case GSM48_PDISC_CC:
3817 return gsm48_rcv_cc(ms, msg);
3820 case GSM48_PDISC_NC_SS:
3821 return gsm48_rcv_ss(ms, msg);
3823 case GSM48_PDISC_SMS:
3824 return gsm48_rcv_sms(ms, msg);
3828 LOGP(DMM, LOGL_NOTICE, "Protocol type 0x%02x unsupported.\n",
3831 return gsm48_mm_tx_mm_status(ms,
3832 GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED);
3835 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' in MM state %s\n", ms->name,
3836 get_mm_name(msg_type), gsm48_mm_state_names[mm->state]);
3838 stop_mm_t3212(mm); /* 4.4.2 */
3840 /* 11.2 re-start pending RR release timer */
3841 if (bsc_timer_pending(&mm->t3240)) {
3846 /* find function for current state and message */
3847 for (i = 0; i < MMDATASLLEN; i++) {
3848 if (msg_type == mmdatastatelist[i].type)
3850 if ((msg_type == mmdatastatelist[i].type)
3851 && ((1 << mm->state) & mmdatastatelist[i].states))
3854 if (i == MMDATASLLEN) {
3856 if (msg_supported) {
3857 LOGP(DMM, LOGL_NOTICE, "Message unhandled at this "
3859 return gsm48_mm_tx_mm_status(ms,
3860 GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE);
3862 LOGP(DMM, LOGL_NOTICE, "Message not supported.\n");
3863 return gsm48_mm_tx_mm_status(ms,
3864 GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED);
3868 rc = mmdatastatelist[i].rout(ms, msg);
3875 /* state trasitions for mobile management events */
3876 static struct eventstate {
3880 int (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3881 } eventstatelist[] = {
3882 /* 4.2.3 return to MM IDLE */
3883 {ALL_STATES - SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES,
3884 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_no_cell_found},
3886 {ALL_STATES - SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES,
3887 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_return_idle},
3889 /* 4.2.2.1 Normal service */
3890 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3891 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3893 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3894 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_loc_upd_normal}, /* change */
3896 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3897 GSM48_MM_EVENT_TIMEOUT_T3211, gsm48_mm_loc_upd},
3899 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3900 GSM48_MM_EVENT_TIMEOUT_T3213, gsm48_mm_loc_upd},
3902 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3903 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_periodic},
3905 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3906 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_start},
3908 /* 4.2.2.2 Attempt to update / Loc. upd. needed */
3909 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE) |
3910 SBIT(GSM48_MM_SST_LOC_UPD_NEEDED),
3911 GSM48_MM_EVENT_USER_PLMN_SEL, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3913 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE) |
3914 SBIT(GSM48_MM_SST_LOC_UPD_NEEDED),
3915 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3917 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE) |
3918 SBIT(GSM48_MM_SST_LOC_UPD_NEEDED),
3919 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_loc_upd_normal}, /* change */
3921 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE),
3922 GSM48_MM_EVENT_TIMEOUT_T3211, gsm48_mm_loc_upd},
3924 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE),
3925 GSM48_MM_EVENT_TIMEOUT_T3213, gsm48_mm_loc_upd},
3927 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE),
3928 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_periodic},
3930 /* 4.2.2.3 Limited service */
3931 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3932 GSM48_MM_EVENT_USER_PLMN_SEL, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3934 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3935 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3937 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3938 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_loc_upd_normal}, /* if allow. */
3940 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3941 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_delay_per}, /* 4.4.2 */
3943 /* 4.2.2.4 No IMSI */
3944 /* 4.2.2.5 PLMN search, normal service */
3945 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3946 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_no_cell_found}, /* 4.2.1.1 */
3948 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3949 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_cell_selected}, /* 4.2.1.1 */
3951 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3952 GSM48_MM_EVENT_TIMEOUT_T3211, gsm48_mm_loc_upd_delay_retry},
3954 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3955 GSM48_MM_EVENT_TIMEOUT_T3213, gsm48_mm_loc_upd_delay_retry},
3957 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3958 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_delay_per}, /* 4.4.2 */
3960 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3961 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_start},
3963 /* 4.2.2.6 PLMN search */
3964 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH),
3965 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_no_cell_found}, /* 4.2.1.1 */
3967 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH),
3968 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_cell_selected}, /* 4.2.1.1 */
3970 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH),
3971 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_delay_per}, /* 4.4.2 */
3973 /* No cell available */
3974 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NO_CELL_AVAIL),
3975 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_cell_selected}, /* 4.2.1.1 */
3977 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NO_CELL_AVAIL),
3978 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_delay_per}, /* 4.4.2 */
3980 /* IMSI detach in other cases */
3981 {SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES, /* silently detach */
3982 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_end},
3984 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN) |
3985 SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3986 SBIT(GSM48_MM_ST_PROCESS_CM_SERV_P) |
3987 SBIT(GSM48_MM_ST_WAIT_REEST) |
3988 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON) |
3989 SBIT(GSM48_MM_ST_MM_CONN_ACTIVE_VGCS) |
3990 SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD), ALL_STATES, /* we can release */
3991 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_release},
3993 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_IMSI_D) |
3994 SBIT(GSM48_MM_ST_IMSI_DETACH_INIT) |
3995 SBIT(GSM48_MM_ST_IMSI_DETACH_PEND), ALL_STATES, /* ignore */
3996 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_ignore},
3998 {ALL_STATES, ALL_STATES,
3999 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_delay},
4001 {GSM48_MM_ST_IMSI_DETACH_INIT, ALL_STATES,
4002 GSM48_MM_EVENT_TIMEOUT_T3220, gsm48_mm_imsi_detach_end},
4004 /* location update in other cases */
4005 {ALL_STATES, ALL_STATES,
4006 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_ignore},
4008 {ALL_STATES - SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES,
4009 GSM48_MM_EVENT_TIMEOUT_T3210, gsm48_mm_loc_upd_timeout},
4011 {ALL_STATES - SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES,
4012 GSM48_MM_EVENT_TIMEOUT_T3213, gsm48_mm_loc_upd_failed},
4013 /* 4.4.4.9 c) (but without retry) */
4016 {ALL_STATES, ALL_STATES,
4017 GSM48_MM_EVENT_SYSINFO, gsm48_mm_sysinfo},
4019 /* T3240 timed out */
4020 {SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD) |
4021 SBIT(GSM48_MM_ST_LOC_UPD_REJ), ALL_STATES, /* 4.4.4.8 */
4022 GSM48_MM_EVENT_TIMEOUT_T3240, gsm48_mm_abort_rr},
4024 /* T3230 timed out */
4025 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
4026 GSM48_MM_EVENT_TIMEOUT_T3230, gsm48_mm_timeout_mm_con},
4028 /* SIM reports SRES */
4029 {ALL_STATES, ALL_STATES, /* 4.3.2.2 */
4030 GSM48_MM_EVENT_AUTH_RESPONSE, gsm48_mm_tx_auth_rsp},
4033 /* change in classmark is reported */
4034 {ALL_STATES, ALL_STATES,
4035 GSM48_MM_EVENT_CLASSMARK_CHG, gsm48_mm_classm_chg},
4039 #define EVENTSLLEN \
4040 (sizeof(eventstatelist) / sizeof(struct eventstate))
4042 static int gsm48_mm_ev(struct osmocom_ms *ms, int msg_type, struct msgb *msg)
4044 struct gsm48_mmlayer *mm = &ms->mmlayer;
4047 if (mm->state == GSM48_MM_ST_MM_IDLE)
4048 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' event in state "
4049 "MM IDLE, %s\n", ms->name, get_mmevent_name(msg_type),
4050 gsm48_mm_substate_names[mm->substate]);
4052 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' event in state "
4053 "%s\n", ms->name, get_mmevent_name(msg_type),
4054 gsm48_mm_state_names[mm->state]);
4056 /* Find function for current state and message */
4057 for (i = 0; i < EVENTSLLEN; i++)
4058 if ((msg_type == eventstatelist[i].type)
4059 && ((1 << mm->state) & eventstatelist[i].states)
4060 && ((1 << mm->substate) & eventstatelist[i].substates))
4062 if (i == EVENTSLLEN) {
4063 LOGP(DMM, LOGL_NOTICE, "Message unhandled at this state.\n");
4067 rc = eventstatelist[i].rout(ms, msg);
4073 * MM Register (SIM insert and remove)
4076 /* register new SIM card and trigger attach */
4077 static int gsm48_mmr_reg_req(struct osmocom_ms *ms)
4079 struct gsm48_mmlayer *mm = &ms->mmlayer;
4082 /* schedule insertion of SIM */
4083 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SIM_INSERT);
4086 gsm322_plmn_sendmsg(ms, nmsg);
4088 /* 4.2.1.2 SIM is inserted in state NO IMSI */
4089 if (mm->state == GSM48_MM_ST_MM_IDLE
4090 && mm->substate == GSM48_MM_SST_NO_IMSI)
4091 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
4092 gsm48_mm_set_plmn_search(ms));
4097 /* trigger detach of sim card */
4098 static int gsm48_mmr_nreg_req(struct osmocom_ms *ms)
4100 struct gsm48_mmlayer *mm = &ms->mmlayer;
4103 nmsg = gsm48_mmevent_msgb_alloc(GSM48_MM_EVENT_IMSI_DETACH);
4106 gsm48_mmevent_msg(mm->ms, nmsg);
4111 static int gsm48_rcv_mmr(struct osmocom_ms *ms, struct msgb *msg)
4113 struct gsm48_mmr *mmr = (struct gsm48_mmr *)msg->data;
4114 int msg_type = mmr->msg_type;
4117 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' event\n", ms->name,
4118 get_mmr_name(msg_type));
4120 case GSM48_MMR_REG_REQ:
4121 rc = gsm48_mmr_reg_req(ms);
4123 case GSM48_MMR_NREG_REQ:
4124 rc = gsm48_mmr_nreg_req(ms);
4127 LOGP(DMM, LOGL_NOTICE, "Message unhandled.\n");