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->lai_valid) {
982 LOGP(DMM, LOGL_INFO, "Selecting PLMN SEARCH state, because "
983 "LAI in SIM not valid.\n");
984 return GSM48_MM_SST_PLMN_SEARCH;
987 /* no cell selected */
989 LOGP(DMM, LOGL_INFO, "Selecting PLMN SEARCH state, because "
990 "no cell selected.\n");
991 return GSM48_MM_SST_PLMN_SEARCH;
994 /* selected cell's LAI not equal to LAI stored on the sim */
995 if (cs->sel_mcc != subscr->lai_mcc
996 || cs->sel_mnc != subscr->lai_mnc
997 || cs->sel_lac != subscr->lai_lac) {
998 LOGP(DMM, LOGL_INFO, "Selecting PLMN SEARCH state, because "
999 "LAI of selected cell (MCC %s MNC %s LAC 0x%04x) "
1000 "!= LAI in SIM (MCC %s MNC %s LAC 0x%04x).\n",
1001 gsm_print_mcc(cs->sel_mcc), gsm_print_mnc(cs->sel_mnc),
1002 cs->sel_lac, gsm_print_mcc(subscr->lai_mcc),
1003 gsm_print_mnc(subscr->lai_mnc), subscr->lai_lac);
1004 return GSM48_MM_SST_PLMN_SEARCH;
1007 /* SIM is updated in this LA */
1008 LOGP(DMM, LOGL_INFO, "Selecting PLMN SEARCH NORMAL state.\n");
1009 return GSM48_MM_SST_PLMN_SEARCH_NORMAL;
1012 /* 4.2.3 when returning to MM IDLE state, this function is called */
1013 static int gsm48_mm_return_idle(struct osmocom_ms *ms, struct msgb *msg)
1015 struct gsm_subscriber *subscr = &ms->subscr;
1016 struct gsm48_mmlayer *mm = &ms->mmlayer;
1017 struct gsm322_cellsel *cs = &ms->cellsel;
1019 /* 4.4.4.9 start T3211 when RR is released */
1020 if (mm->start_t3211) {
1021 LOGP(DMM, LOGL_INFO, "Starting T3211 after RR release.\n");
1022 mm->start_t3211 = 0;
1026 /* return from location update with "Roaming not allowed" */
1027 if (mm->state == GSM48_MM_ST_LOC_UPD_REJ && mm->lupd_rej_cause == 13) {
1028 LOGP(DMM, LOGL_INFO, "Roaming not allowed as returning to "
1030 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1031 gsm48_mm_set_plmn_search(ms));
1036 /* no SIM present or invalid */
1037 if (!subscr->sim_valid) {
1038 LOGP(DMM, LOGL_INFO, "SIM invalid as returning to MM IDLE\n");
1040 /* stop periodic location updating */
1041 mm->lupd_pending = 0;
1042 stop_mm_t3212(mm); /* 4.4.2 */
1044 new_mm_state(mm, GSM48_MM_ST_MM_IDLE, GSM48_MM_SST_NO_IMSI);
1049 /* selected cell equals the registered LAI */
1050 if (subscr->lai_valid
1051 && cs->sel_mcc == subscr->lai_mcc
1052 && cs->sel_mnc == subscr->lai_mnc
1053 && cs->sel_lac == subscr->lai_lac) {
1054 LOGP(DMM, LOGL_INFO, "We are in registered LAI as returning "
1056 /* if SIM not updated (abnormal case as described in 4.4.4.9) */
1057 if (subscr->ustate != GSM_SIM_U1_UPDATED)
1058 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1059 GSM48_MM_SST_ATTEMPT_UPDATE);
1061 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1062 GSM48_MM_SST_NORMAL_SERVICE);
1067 /* location update allowed */
1068 if (cs->state == GSM322_C3_CAMPED_NORMALLY) {
1069 LOGP(DMM, LOGL_INFO, "We are camping normally as returning to "
1071 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1072 GSM48_MM_SST_LOC_UPD_NEEDED);
1073 } else { /* location update not allowed */
1074 LOGP(DMM, LOGL_INFO, "We are camping on any cell as returning "
1076 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1077 GSM48_MM_SST_LIMITED_SERVICE);
1083 /* 4.2.1.1 Service state PLMN SEARCH (NORMAL) is left if no cell found */
1084 static int gsm48_mm_no_cell_found(struct osmocom_ms *ms, struct msgb *msg)
1086 struct gsm48_mmlayer *mm = &ms->mmlayer;
1088 new_mm_state(mm, GSM48_MM_ST_MM_IDLE, GSM48_MM_SST_NO_CELL_AVAIL);
1093 /* 4.2.1.1 Service state PLMN SEARCH (NORMAL) / NO CELL AVAILABLE is left
1096 static int gsm48_mm_cell_selected(struct osmocom_ms *ms, struct msgb *msg)
1098 struct gsm_subscriber *subscr = &ms->subscr;
1099 struct gsm48_mmlayer *mm = &ms->mmlayer;
1100 struct gsm322_plmn *plmn = &ms->plmn;
1101 struct gsm322_cellsel *cs = &ms->cellsel;
1102 struct gsm48_sysinfo *s = &cs->sel_si;
1103 struct gsm_settings *set = &ms->settings;
1105 /* no SIM is inserted */
1106 if (!subscr->sim_valid) {
1107 LOGP(DMM, LOGL_INFO, "SIM invalid as cell is selected.\n");
1108 new_mm_state(mm, GSM48_MM_ST_MM_IDLE, GSM48_MM_SST_NO_IMSI);
1113 /* SIM not updated in this LA */
1114 if (subscr->ustate == GSM_SIM_U1_UPDATED
1115 && subscr->lai_valid
1116 && cs->sel_mcc == subscr->lai_mcc
1117 && cs->sel_mnc == subscr->lai_mnc
1118 && cs->sel_lac == subscr->lai_lac
1119 && !mm->lupd_periodic) {
1120 if (subscr->imsi_attached) {
1123 LOGP(DMM, LOGL_INFO, "Valid in location area.\n");
1124 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1125 GSM48_MM_SST_NORMAL_SERVICE);
1127 /* send message to PLMN search process */
1128 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_SUCCESS);
1131 gsm322_plmn_sendmsg(ms, nmsg);
1135 if (!s->att_allowed) {
1138 LOGP(DMM, LOGL_INFO, "Attachment not required.\n");
1139 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1140 GSM48_MM_SST_NORMAL_SERVICE);
1142 /* send message to PLMN search process */
1143 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_SUCCESS);
1146 gsm322_plmn_sendmsg(ms, nmsg);
1150 /* else, continue */
1153 /* PLMN mode auto and selected cell is forbidden */
1154 if (set->plmn_mode == PLMN_MODE_AUTO
1155 && (gsm_subscr_is_forbidden_plmn(subscr, cs->sel_mcc, cs->sel_mnc)
1156 || gsm322_is_forbidden_la(ms, cs->sel_mcc, cs->sel_mnc,
1160 LOGP(DMM, LOGL_INFO, "Selected cell is forbidden.\n");
1161 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1162 GSM48_MM_SST_LIMITED_SERVICE);
1164 /* send message to PLMN search process */
1165 nmsg = gsm322_msgb_alloc(GSM322_EVENT_ROAMING_NA);
1168 gsm322_plmn_sendmsg(ms, nmsg);
1173 /* PLMN mode manual and selected cell not selected PLMN */
1174 if (set->plmn_mode == PLMN_MODE_MANUAL
1175 && (plmn->mcc != cs->sel_mcc
1176 || plmn->mnc != cs->sel_mnc)) {
1179 LOGP(DMM, LOGL_INFO, "Selected cell not found.\n");
1180 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
1181 GSM48_MM_SST_LIMITED_SERVICE);
1183 /* send message to PLMN search process */
1184 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_FAILED);
1187 gsm322_plmn_sendmsg(ms, nmsg);
1193 new_mm_state(mm, GSM48_MM_ST_MM_IDLE, GSM48_MM_SST_LOC_UPD_NEEDED);
1198 /* 4.2.1.2 Service state PLMN SEARCH (NORMAL) is entered */
1199 static int gsm48_mm_plmn_search(struct osmocom_ms *ms, struct msgb *msg)
1201 struct gsm48_mmlayer *mm = &ms->mmlayer;
1203 new_mm_state(mm, GSM48_MM_ST_MM_IDLE, gsm48_mm_set_plmn_search(ms));
1212 /* initialize Mobility Management process */
1213 int gsm48_mm_init(struct osmocom_ms *ms)
1215 struct gsm48_mmlayer *mm = &ms->mmlayer;
1217 memset(mm, 0, sizeof(*mm));
1220 LOGP(DMM, LOGL_INFO, "init Mobility Management process\n");
1223 mm->state = GSM48_MM_ST_MM_IDLE;
1224 mm->substate = gsm48_mm_set_plmn_search(ms);
1227 INIT_LLIST_HEAD(&mm->mm_conn);
1228 INIT_LLIST_HEAD(&mm->rr_upqueue);
1229 INIT_LLIST_HEAD(&mm->mmxx_upqueue);
1230 INIT_LLIST_HEAD(&mm->mmr_downqueue);
1231 INIT_LLIST_HEAD(&mm->event_queue);
1236 /* exit MM process */
1237 int gsm48_mm_exit(struct osmocom_ms *ms)
1239 struct gsm48_mmlayer *mm = &ms->mmlayer;
1240 struct gsm48_mm_conn *conn;
1243 LOGP(DMM, LOGL_INFO, "exit Mobility Management process\n");
1246 while (!llist_empty(&mm->mm_conn)) {
1247 conn = llist_entry(mm->mm_conn.next,
1248 struct gsm48_mm_conn, list);
1251 while ((msg = msgb_dequeue(&mm->rr_upqueue)))
1253 while ((msg = msgb_dequeue(&mm->mmxx_upqueue)))
1255 while ((msg = msgb_dequeue(&mm->mmr_downqueue)))
1257 while ((msg = msgb_dequeue(&mm->event_queue)))
1273 * MM connection management
1276 static const char *gsm48_mmxx_state_names[] = {
1284 uint32_t mm_conn_new_ref = 0x80000001;
1286 /* new MM connection state */
1287 static void new_conn_state(struct gsm48_mm_conn *conn, int state)
1289 LOGP(DMM, LOGL_INFO, "(ref %x) new state %s -> %s\n", conn->ref,
1290 gsm48_mmxx_state_names[conn->state],
1291 gsm48_mmxx_state_names[state]);
1292 conn->state = state;
1295 /* find MM connection by protocol+ID */
1296 struct gsm48_mm_conn *mm_conn_by_id(struct gsm48_mmlayer *mm,
1297 uint8_t proto, uint8_t transaction_id)
1299 struct gsm48_mm_conn *conn;
1301 llist_for_each_entry(conn, &mm->mm_conn, list) {
1302 if (conn->protocol == proto &&
1303 conn->transaction_id == transaction_id)
1309 /* find MM connection by reference */
1310 struct gsm48_mm_conn *mm_conn_by_ref(struct gsm48_mmlayer *mm,
1313 struct gsm48_mm_conn *conn;
1315 llist_for_each_entry(conn, &mm->mm_conn, list) {
1316 if (conn->ref == ref)
1322 /* create MM connection instance */
1323 static struct gsm48_mm_conn* mm_conn_new(struct gsm48_mmlayer *mm,
1324 int proto, uint8_t transaction_id, uint32_t ref)
1326 struct gsm48_mm_conn *conn = talloc_zero(l23_ctx, struct gsm48_mm_conn);
1331 LOGP(DMM, LOGL_INFO, "New MM Connection (proto 0x%02x trans_id %d "
1332 "ref %d)\n", proto, transaction_id, ref);
1335 conn->state = GSM48_MMXX_ST_IDLE;
1336 conn->transaction_id = transaction_id;
1337 conn->protocol = proto;
1340 llist_add(&conn->list, &mm->mm_conn);
1345 /* destroy MM connection instance */
1346 void mm_conn_free(struct gsm48_mm_conn *conn)
1348 LOGP(DMM, LOGL_INFO, "Freeing MM Connection\n");
1350 new_conn_state(conn, GSM48_MMXX_ST_IDLE);
1352 llist_del(&conn->list);
1357 /* support function to release pending/all ongoing MM connections */
1358 static int gsm48_mm_release_mm_conn(struct osmocom_ms *ms, int abort_any,
1359 uint8_t cause, int error)
1361 struct gsm48_mmlayer *mm = &ms->mmlayer;
1362 struct gsm48_mm_conn *conn, *conn2;
1364 struct gsm48_mmxx_hdr *nmmh;
1367 LOGP(DMM, LOGL_INFO, "Release any MM Connection\n");
1369 LOGP(DMM, LOGL_INFO, "Release pending MM Connections\n");
1371 /* release MM connection(s) */
1372 llist_for_each_entry_safe(conn, conn2, &mm->mm_conn, list) {
1373 /* abort any OR the pending connection */
1374 if (abort_any || conn->state == GSM48_MMXX_ST_CONN_PEND) {
1375 /* send MMxx-REL-IND */
1377 switch(conn->protocol) {
1378 case GSM48_PDISC_CC:
1379 nmsg = gsm48_mmxx_msgb_alloc(
1380 error ? GSM48_MMCC_ERR_IND
1381 : GSM48_MMCC_REL_IND, conn->ref,
1382 conn->transaction_id);
1384 case GSM48_PDISC_NC_SS:
1385 nmsg = gsm48_mmxx_msgb_alloc(
1386 error ? GSM48_MMSS_ERR_IND
1387 : GSM48_MMSS_REL_IND, conn->ref,
1388 conn->transaction_id);
1390 case GSM48_PDISC_SMS:
1391 nmsg = gsm48_mmxx_msgb_alloc(
1392 error ? GSM48_MMSMS_ERR_IND
1393 : GSM48_MMSMS_REL_IND, conn->ref,
1394 conn->transaction_id);
1398 /* this should not happen */
1400 continue; /* skip if not of CC type */
1402 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
1403 nmmh->cause = cause;
1404 gsm48_mmxx_upmsg(ms, nmsg);
1413 * process handlers (Common procedures)
1416 /* sending MM STATUS message */
1417 static int gsm48_mm_tx_mm_status(struct osmocom_ms *ms, uint8_t cause)
1420 struct gsm48_hdr *ngh;
1421 uint8_t *reject_cause;
1423 LOGP(DMM, LOGL_INFO, "MM STATUS (cause #%d)\n", cause);
1425 nmsg = gsm48_l3_msgb_alloc();
1428 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
1429 reject_cause = msgb_put(nmsg, 1);
1431 ngh->proto_discr = GSM48_PDISC_MM;
1432 ngh->msg_type = GSM48_MT_MM_STATUS;
1433 *reject_cause = cause;
1435 /* push RR header and send down */
1436 return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_DATA_REQ, 0);
1439 /* 4.3.1.2 sending TMSI REALLOCATION COMPLETE message */
1440 static int gsm48_mm_tx_tmsi_reall_cpl(struct osmocom_ms *ms)
1443 struct gsm48_hdr *ngh;
1445 LOGP(DMM, LOGL_INFO, "TMSI REALLOCATION COMPLETE\n");
1447 nmsg = gsm48_l3_msgb_alloc();
1450 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
1452 ngh->proto_discr = GSM48_PDISC_MM;
1453 ngh->msg_type = GSM48_MT_MM_TMSI_REALL_COMPL;
1455 /* push RR header and send down */
1456 return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_DATA_REQ, 0);
1459 /* 4.3.1 TMSI REALLOCATION COMMAND is received */
1460 static int gsm48_mm_rx_tmsi_realloc_cmd(struct osmocom_ms *ms, struct msgb *msg)
1462 struct gsm_subscriber *subscr = &ms->subscr;
1463 struct gsm48_hdr *gh = msgb_l3(msg);
1464 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1465 struct gsm48_loc_area_id *lai = (struct gsm48_loc_area_id *) gh->data;
1466 uint8_t mi_type, *mi;
1469 if (payload_len < sizeof(struct gsm48_loc_area_id) + 2) {
1471 LOGP(DMM, LOGL_NOTICE, "Short read of TMSI REALLOCATION "
1472 "COMMAND message error.\n");
1476 gsm48_decode_lai(lai, &subscr->lai_mcc, &subscr->lai_mnc,
1479 mi = gh->data + sizeof(struct gsm48_loc_area_id);
1480 mi_type = mi[1] & GSM_MI_TYPE_MASK;
1482 case GSM_MI_TYPE_TMSI:
1483 if (payload_len + sizeof(struct gsm48_loc_area_id) < 6
1486 memcpy(&tmsi, mi+2, 4);
1487 subscr->tmsi = ntohl(tmsi);
1488 subscr->tmsi_valid = 1;
1489 LOGP(DMM, LOGL_INFO, "TMSI 0x%08x (%u) assigned.\n",
1490 subscr->tmsi, subscr->tmsi);
1491 gsm48_mm_tx_tmsi_reall_cpl(ms);
1493 case GSM_MI_TYPE_IMSI:
1494 subscr->tmsi_valid = 0;
1495 LOGP(DMM, LOGL_INFO, "TMSI removed.\n");
1496 gsm48_mm_tx_tmsi_reall_cpl(ms);
1499 LOGP(DMM, LOGL_NOTICE, "TMSI reallocation with unknown MI "
1500 "type %d.\n", mi_type);
1501 gsm48_mm_tx_mm_status(ms, GSM48_REJECT_INCORRECT_MESSAGE);
1503 return 0; /* don't store in SIM */
1507 store / remove from sim
1514 static int gsm48_mm_tx_auth_rsp(struct osmocom_ms *ms, struct msgb *msg);
1517 /* 4.3.2.2 AUTHENTICATION REQUEST is received */
1518 static int gsm48_mm_rx_auth_req(struct osmocom_ms *ms, struct msgb *msg)
1520 struct gsm_subscriber *subscr = &ms->subscr;
1521 struct gsm48_hdr *gh = msgb_l3(msg);
1522 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1523 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) gh->data;
1525 if (payload_len < sizeof(struct gsm48_auth_req)) {
1526 LOGP(DMM, LOGL_NOTICE, "Short read of AUTHENTICATION REQUEST "
1527 "message error.\n");
1531 /* SIM is not available */
1532 if (!subscr->sim_valid) {
1533 LOGP(DMM, LOGL_INFO, "AUTHENTICATION REQUEST without SIM\n");
1534 return gsm48_mm_tx_mm_status(ms,
1535 GSM48_REJECT_MSG_NOT_COMPATIBLE);
1538 LOGP(DMM, LOGL_INFO, "AUTHENTICATION REQUEST (seq %d)\n", ar->key_seq);
1540 /* key_seq and random */
1543 (..., ar->key_seq, ar->rand);
1547 struct gsm48_mm_event *nmme;
1548 nmsg = gsm48_l3_msgb_alloc();
1551 nmme = (struct gsm48_mm_event *)msgb_put(nmsg, sizeof(*nmme));
1552 *((uint32_t *)nmme->sres) = 0x12345678;
1553 gsm48_mm_tx_auth_rsp(ms, nmsg);
1557 /* wait for auth response event from SIM */
1561 /* 4.3.2.2 sending AUTHENTICATION RESPONSE */
1562 static int gsm48_mm_tx_auth_rsp(struct osmocom_ms *ms, struct msgb *msg)
1564 struct gsm48_mm_event *mme = (struct gsm48_mm_event *) msg->data;
1566 struct gsm48_hdr *ngh;
1569 LOGP(DMM, LOGL_INFO, "AUTHENTICATION RESPONSE\n");
1571 nmsg = gsm48_l3_msgb_alloc();
1574 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
1576 ngh->proto_discr = GSM48_PDISC_MM;
1577 ngh->msg_type = GSM48_MT_MM_AUTH_RESP;
1580 sres = msgb_put(nmsg, 4);
1581 memcpy(sres, mme->sres, 4);
1583 /* push RR header and send down */
1584 return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_DATA_REQ, 0);
1587 /* 4.3.2.5 AUTHENTICATION REJECT is received */
1588 static int gsm48_mm_rx_auth_rej(struct osmocom_ms *ms, struct msgb *msg)
1590 struct gsm_subscriber *subscr = &ms->subscr;
1591 struct gsm48_mmlayer *mm = &ms->mmlayer;
1593 LOGP(DMM, LOGL_INFO, "AUTHENTICATION REJECT\n");
1595 stop_mm_t3212(mm); /* 4.4.2 */
1598 subscr->sim_valid = 0;
1600 /* TMSI and LAI invalid */
1601 subscr->lai_valid = 0;
1602 subscr->tmsi_valid = 0;
1604 /* key is invalid */
1605 subscr->key_seq = 7;
1608 new_sim_ustate(subscr, GSM_SIM_U3_ROAMING_NA);
1611 sim: delete tmsi, lai
1612 sim: delete key seq number
1613 sim: set update status
1616 /* abort IMSI detach procedure */
1617 if (mm->state == GSM48_MM_ST_IMSI_DETACH_INIT) {
1619 struct gsm48_rr_hdr *nrrh;
1621 /* abort RR connection */
1622 nmsg = gsm48_rr_msgb_alloc(GSM48_RR_ABORT_REQ);
1625 nrrh = (struct gsm48_rr_hdr *) msgb_put(nmsg, sizeof(*nrrh));
1626 nrrh->cause = GSM48_RR_CAUSE_NORMAL;
1627 gsm48_rr_downmsg(ms, nmsg);
1629 /* CS process will trigger: return to MM IDLE / No SIM */
1636 /* 4.3.3.1 IDENTITY REQUEST is received */
1637 static int gsm48_mm_rx_id_req(struct osmocom_ms *ms, struct msgb *msg)
1639 struct gsm_subscriber *subscr = &ms->subscr;
1640 struct gsm48_hdr *gh = msgb_l3(msg);
1641 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1644 if (payload_len < 1) {
1645 LOGP(DMM, LOGL_NOTICE, "Short read of IDENTITY REQUEST message "
1651 mi_type = *gh->data;
1652 LOGP(DMM, LOGL_INFO, "IDENTITY REQUEST (mi_type %d)\n", mi_type);
1654 /* check if request can be fulfilled */
1655 if (!subscr->sim_valid && mi_type != GSM_MI_TYPE_IMEI
1656 && mi_type != GSM_MI_TYPE_IMEISV) {
1657 LOGP(DMM, LOGL_INFO, "IDENTITY REQUEST without SIM\n");
1658 return gsm48_mm_tx_mm_status(ms,
1659 GSM48_REJECT_MSG_NOT_COMPATIBLE);
1661 if (mi_type == GSM_MI_TYPE_TMSI && !subscr->tmsi_valid) {
1662 LOGP(DMM, LOGL_INFO, "IDENTITY REQUEST of TMSI, but we have no "
1664 return gsm48_mm_tx_mm_status(ms,
1665 GSM48_REJECT_MSG_NOT_COMPATIBLE);
1668 return gsm48_mm_tx_id_rsp(ms, mi_type);
1671 /* send IDENTITY RESPONSE message */
1672 static int gsm48_mm_tx_id_rsp(struct osmocom_ms *ms, uint8_t mi_type)
1675 struct gsm48_hdr *ngh;
1678 LOGP(DMM, LOGL_INFO, "IDENTITY RESPONSE\n");
1680 nmsg = gsm48_l3_msgb_alloc();
1683 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
1685 ngh->proto_discr = GSM48_PDISC_MM;
1686 ngh->msg_type = GSM48_MT_MM_ID_RESP;
1689 gsm48_encode_mi(buf, nmsg, ms, mi_type);
1691 /* push RR header and send down */
1692 return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_DATA_REQ, 0);
1695 /* 4.3.4.1 sending IMSI DETACH INDICATION message */
1696 static int gsm48_mm_tx_imsi_detach(struct osmocom_ms *ms, int rr_prim)
1698 struct gsm_subscriber *subscr = &ms->subscr;
1699 struct gsm_support *sup = &ms->support;
1700 struct gsm48_rrlayer *rr = &ms->rrlayer;
1702 struct gsm48_hdr *ngh;
1705 struct gsm48_classmark1 cm;
1708 LOGP(DMM, LOGL_INFO, "IMSI DETACH INDICATION\n");
1710 nmsg = gsm48_l3_msgb_alloc();
1713 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
1715 ngh->proto_discr = GSM48_PDISC_MM;
1716 ngh->msg_type = GSM48_MT_MM_IMSI_DETACH_IND;
1719 if (rr->cd_now.arfcn >= 512 && rr->cd_now.arfcn <= 885)
1720 pwr_lev = sup->pwr_lev_1800;
1722 pwr_lev = sup->pwr_lev_900;
1723 gsm48_encode_classmark1(&cm, sup->rev_lev, sup->es_ind, sup->a5_1,
1725 msgb_v_put(nmsg, *((uint8_t *)&cm));
1727 if (subscr->tmsi_valid) /* have TMSI ? */
1728 gsm48_encode_mi(buf, nmsg, ms, GSM_MI_TYPE_TMSI);
1730 gsm48_encode_mi(buf, nmsg, ms, GSM_MI_TYPE_IMSI);
1732 /* push RR header and send down */
1733 return gsm48_mm_to_rr(ms, nmsg, rr_prim, RR_EST_CAUSE_OTHER_SDCCH);
1736 /* detach has ended */
1737 static int gsm48_mm_imsi_detach_end(struct osmocom_ms *ms, struct msgb *msg)
1739 struct gsm48_mmlayer *mm = &ms->mmlayer;
1740 struct gsm_subscriber *subscr = &ms->subscr;
1743 LOGP(DMM, LOGL_INFO, "IMSI has been detached.\n");
1745 /* stop IMSI detach timer (if running) */
1755 subscr->sim_valid = 0;
1757 /* wait for RR idle and then power off when IMSI is detached */
1758 if (mm->power_off) {
1759 if (mm->state == GSM48_MM_ST_MM_IDLE) {
1763 mm->power_off_idle = 1;
1768 /* send SIM remove event to gsm322 */
1769 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SIM_REMOVE);
1772 gsm322_plmn_sendmsg(ms, nmsg);
1774 /* CS process will trigger return to MM IDLE / No SIM */
1778 /* start an IMSI detach in MM IDLE */
1779 static int gsm48_mm_imsi_detach_start(struct osmocom_ms *ms, struct msgb *msg)
1781 struct gsm_subscriber *subscr = &ms->subscr;
1782 struct gsm48_mmlayer *mm = &ms->mmlayer;
1783 struct gsm48_sysinfo *s = &ms->cellsel.sel_si;
1785 /* we may silently finish IMSI detach */
1786 if (!s->att_allowed || !subscr->imsi_attached) {
1787 LOGP(DMM, LOGL_INFO, "IMSI detach not required.\n");
1789 return gsm48_mm_imsi_detach_end(ms, msg);
1791 LOGP(DMM, LOGL_INFO, "IMSI detach started (MM IDLE)\n");
1793 new_mm_state(mm, GSM48_MM_ST_WAIT_RR_CONN_IMSI_D, 0);
1795 /* establish RR and send IMSI detach */
1796 return gsm48_mm_tx_imsi_detach(ms, GSM48_RR_EST_REQ);
1799 /* IMSI detach has been sent, wait for RR release */
1800 static int gsm48_mm_imsi_detach_sent(struct osmocom_ms *ms, struct msgb *msg)
1802 struct gsm48_mmlayer *mm = &ms->mmlayer;
1804 /* start T3220 (4.3.4.1) */
1807 LOGP(DMM, LOGL_INFO, "IMSI detach started (Wait for RR release)\n");
1809 new_mm_state(mm, GSM48_MM_ST_IMSI_DETACH_INIT, 0);
1814 /* release MM connection and proceed with IMSI detach */
1815 static int gsm48_mm_imsi_detach_release(struct osmocom_ms *ms, struct msgb *msg)
1817 struct gsm_subscriber *subscr = &ms->subscr;
1818 struct gsm48_mmlayer *mm = &ms->mmlayer;
1819 struct gsm48_sysinfo *s = &ms->cellsel.sel_si;
1821 /* stop MM connection timer */
1824 /* release all connections */
1825 gsm48_mm_release_mm_conn(ms, 1, 16, 0);
1827 /* wait for release of RR */
1828 if (!s->att_allowed || !subscr->imsi_attached) {
1829 LOGP(DMM, LOGL_INFO, "IMSI detach not required.\n");
1830 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
1833 if (mm->power_off) {
1841 /* send IMSI detach */
1842 gsm48_mm_tx_imsi_detach(ms, GSM48_RR_DATA_REQ);
1844 /* go to sent state */
1845 return gsm48_mm_imsi_detach_sent(ms, msg);
1848 /* ignore ongoing IMSI detach */
1849 static int gsm48_mm_imsi_detach_ignore(struct osmocom_ms *ms, struct msgb *msg)
1854 /* delay until state change (and then retry) */
1855 static int gsm48_mm_imsi_detach_delay(struct osmocom_ms *ms, struct msgb *msg)
1857 struct gsm48_mmlayer *mm = &ms->mmlayer;
1859 LOGP(DMM, LOGL_INFO, "IMSI detach delayed.\n");
1861 /* remember to detach later */
1862 mm->delay_detach = 1;
1867 /* 4.3.5.2 ABORT is received */
1868 static int gsm48_mm_rx_abort(struct osmocom_ms *ms, struct msgb *msg)
1870 struct gsm48_mmlayer *mm = &ms->mmlayer;
1871 struct gsm_subscriber *subscr = &ms->subscr;
1872 struct gsm48_hdr *gh = msgb_l3(msg);
1873 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1874 uint8_t reject_cause;
1876 if (payload_len < 1) {
1877 LOGP(DMM, LOGL_NOTICE, "Short read of ABORT message error.\n");
1881 reject_cause = *gh->data;
1883 if (llist_empty(&mm->mm_conn)) {
1884 LOGP(DMM, LOGL_NOTICE, "ABORT (cause #%d) while no MM "
1885 "connection is established.\n", reject_cause);
1886 return gsm48_mm_tx_mm_status(ms,
1887 GSM48_REJECT_MSG_NOT_COMPATIBLE);
1889 LOGP(DMM, LOGL_NOTICE, "ABORT (cause #%d) while MM connection "
1890 "is established.\n", reject_cause);
1891 /* stop MM connection timer */
1894 gsm48_mm_release_mm_conn(ms, 1, 16, 0);
1897 if (reject_cause == GSM48_REJECT_ILLEGAL_ME) {
1899 subscr->sim_valid = 0;
1901 /* TMSI and LAI invalid */
1902 subscr->lai_valid = 0;
1903 subscr->tmsi_valid = 0;
1905 /* key is invalid */
1906 subscr->key_seq = 7;
1909 new_sim_ustate(subscr, GSM_SIM_U3_ROAMING_NA);
1912 sim: delete tmsi, lai
1913 sim: delete key seq number
1914 sim: apply update state
1917 /* CS process will trigger: return to MM IDLE / No SIM */
1924 /* 4.3.6.2 MM INFORMATION is received */
1925 static int gsm48_mm_rx_info(struct osmocom_ms *ms, struct msgb *msg)
1927 struct gsm48_mmlayer *mm = &ms->mmlayer;
1928 struct gsm48_hdr *gh = msgb_l3(msg);
1929 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1930 struct tlv_parsed tp;
1932 if (payload_len < 0) {
1933 LOGP(DMM, LOGL_NOTICE, "Short read of MM INFORMATION message "
1937 tlv_parse(&tp, &gsm48_mm_att_tlvdef, gh->data, payload_len, 0, 0);
1940 if (TLVP_PRESENT(&tp, GSM48_IE_NAME_LONG)) {
1941 decode_network_name(mm->name_long, sizeof(mm->name_long),
1942 TLVP_VAL(&tp, GSM48_IE_NAME_LONG)-1);
1945 if (TLVP_PRESENT(&tp, GSM48_IE_NAME_SHORT)) {
1946 decode_network_name(mm->name_short, sizeof(mm->name_short),
1947 TLVP_VAL(&tp, GSM48_IE_NAME_SHORT)-1);
1954 * process handlers for Location Update + IMSI attach (MM specific procedures)
1957 /* 4.4.2 received sysinfo change event */
1958 static int gsm48_mm_sysinfo(struct osmocom_ms *ms, struct msgb *msg)
1960 struct gsm48_mmlayer *mm = &ms->mmlayer;
1961 struct gsm48_sysinfo *s = &ms->cellsel.sel_si;
1963 /* t3212 not changed in these states */
1964 if (mm->state == GSM48_MM_ST_MM_IDLE
1965 && (mm->substate == GSM48_MM_SST_NO_CELL_AVAIL
1966 || mm->substate == GSM48_MM_SST_LIMITED_SERVICE
1967 || mm->substate == GSM48_MM_SST_PLMN_SEARCH
1968 || mm->substate == GSM48_MM_SST_PLMN_SEARCH_NORMAL))
1971 /* new periodic location update timer timeout */
1972 if (s->t3212 && s->t3212 != mm->t3212_value) {
1973 if (bsc_timer_pending(&mm->t3212)) {
1975 struct timeval current_time;
1978 gettimeofday(¤t_time, NULL);
1979 t = mm->t3212.timeout.tv_sec - current_time.tv_sec;
1982 LOGP(DMM, LOGL_INFO, "New T3212 while timer is running "
1983 "(value %d rest %d)\n", s->t3212, t);
1985 /* rest time modulo given value */
1986 mm->t3212.timeout.tv_sec = current_time.tv_sec
1989 uint32_t rand = random();
1991 LOGP(DMM, LOGL_INFO, "New T3212 while timer is not "
1992 "running (value %d)\n", s->t3212);
1994 /* value between 0 and given value */
1995 start_mm_t3212(mm, rand % (s->t3212 + 1));
1997 mm->t3212_value = s->t3212;
2003 /* 4.4.4.1 (re)start location update
2005 * this function is called by
2006 * - normal location update
2007 * - periodic location update
2008 * - imsi attach (normal loc. upd. function)
2009 * - retry timers (T3211 and T3213)
2011 static int gsm48_mm_loc_upd(struct osmocom_ms *ms, struct msgb *msg)
2013 struct gsm48_mmlayer *mm = &ms->mmlayer;
2014 struct gsm322_cellsel *cs = &ms->cellsel;
2015 struct gsm48_sysinfo *s = &cs->sel_si;
2016 struct gsm_subscriber *subscr = &ms->subscr;
2017 struct gsm_settings *set = &ms->settings;
2021 /* (re)start only if we still require location update */
2022 if (!mm->lupd_pending) {
2023 LOGP(DMM, LOGL_INFO, "No loc. upd. pending.\n");
2027 /* must camp normally */
2028 if (cs->state != GSM322_C3_CAMPED_NORMALLY) {
2029 LOGP(DMM, LOGL_INFO, "Loc. upd. not camping normally.\n");
2030 msg_type = GSM322_EVENT_REG_FAILED;
2032 LOGP(DSUM, LOGL_INFO, "Location update not possible\n");
2033 mm->lupd_pending = 0;
2034 /* send message to PLMN search process */
2035 nmsg = gsm322_msgb_alloc(msg_type);
2038 gsm322_plmn_sendmsg(ms, nmsg);
2042 /* deny network, if disabled */
2044 LOGP(DMM, LOGL_INFO, "Loc. upd. disabled, adding "
2045 "forbidden PLMN.\n");
2046 gsm_subscr_add_forbidden_plmn(subscr, cs->sel_mcc,
2047 cs->sel_mnc, GSM48_REJECT_PLMN_NOT_ALLOWED);
2048 msg_type = GSM322_EVENT_ROAMING_NA;
2052 /* if LAI is forbidden, don't start */
2053 if (gsm_subscr_is_forbidden_plmn(subscr, cs->sel_mcc, cs->sel_mnc)) {
2054 LOGP(DMM, LOGL_INFO, "Loc. upd. not allowed PLMN.\n");
2055 msg_type = GSM322_EVENT_ROAMING_NA;
2058 if (gsm322_is_forbidden_la(ms, cs->sel_mcc,
2059 cs->sel_mnc, cs->sel_lac)) {
2060 LOGP(DMM, LOGL_INFO, "Loc. upd. not allowed LA.\n");
2061 msg_type = GSM322_EVENT_ROAMING_NA;
2065 /* 4.4.4.9 if cell is barred, don't start */
2066 if ((!subscr->acc_barr && s->cell_barr)
2067 || (!subscr->acc_barr && !((subscr->acc_class & 0xfbff) &
2068 (s->class_barr ^ 0xffff)))) {
2069 LOGP(DMM, LOGL_INFO, "Loc. upd. no access.\n");
2070 msg_type = GSM322_EVENT_ROAMING_NA;
2074 mm->lupd_mcc = cs->sel_mcc;
2075 mm->lupd_mnc = cs->sel_mnc;
2076 mm->lupd_lac = cs->sel_lac;
2078 LOGP(DSUM, LOGL_INFO, "Perform location update (MCC %s, MNC %s "
2079 "LAC 0x%04x)\n", gsm_print_mcc(mm->lupd_mcc),
2080 gsm_print_mnc(mm->lupd_mnc), mm->lupd_lac);
2082 return gsm48_mm_tx_loc_upd_req(ms);
2085 /* initiate a normal location update / imsi attach */
2086 static int gsm48_mm_loc_upd_normal(struct osmocom_ms *ms, struct msgb *msg)
2088 struct gsm48_mmlayer *mm = &ms->mmlayer;
2089 struct gsm_subscriber *subscr = &ms->subscr;
2090 struct gsm322_cellsel *cs = &ms->cellsel;
2091 struct gsm48_sysinfo *s = &cs->sel_si;
2094 /* in case we already have a location update going on */
2095 if (mm->lupd_pending) {
2096 LOGP(DMM, LOGL_INFO, "Loc. upd. already pending.\n");
2101 /* no location update, if limited service */
2102 if (cs->state != GSM322_C3_CAMPED_NORMALLY) {
2103 LOGP(DMM, LOGL_INFO, "Loc. upd. not allowed.\n");
2105 /* send message to PLMN search process */
2106 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_FAILED);
2109 gsm322_plmn_sendmsg(ms, nmsg);
2114 /* if location update is not required */
2115 if (subscr->ustate == GSM_SIM_U1_UPDATED
2117 && cs->sel_mcc == subscr->lai_mcc
2118 && cs->sel_mnc == subscr->lai_mnc
2119 && cs->sel_lac == subscr->lai_lac
2120 && (subscr->imsi_attached
2121 || !s->att_allowed)) {
2122 LOGP(DMM, LOGL_INFO, "Loc. upd. not required.\n");
2123 subscr->imsi_attached = 1;
2125 /* send message to PLMN search process */
2126 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_SUCCESS);
2129 gsm322_plmn_sendmsg(ms, nmsg);
2134 /* 4.4.3 is attachment required? */
2135 if (subscr->ustate == GSM_SIM_U1_UPDATED
2137 && cs->sel_mcc == subscr->lai_mcc
2138 && cs->sel_mnc == subscr->lai_mnc
2139 && cs->sel_lac == subscr->lai_lac
2140 && !subscr->imsi_attached
2141 && s->att_allowed) {
2142 /* do location update for IMSI attach */
2143 LOGP(DMM, LOGL_INFO, "Do Loc. upd. for IMSI attach.\n");
2146 /* do normal location update */
2147 LOGP(DMM, LOGL_INFO, "Do normal Loc. upd.\n");
2151 /* start location update */
2152 mm->lupd_attempt = 0;
2153 mm->lupd_pending = 1;
2154 mm->lupd_ra_failure = 0;
2156 return gsm48_mm_loc_upd(ms, msg);
2159 /* initiate a periodic location update */
2160 static int gsm48_mm_loc_upd_periodic(struct osmocom_ms *ms, struct msgb *msg)
2162 struct gsm48_mmlayer *mm = &ms->mmlayer;
2164 /* in case we already have a location update going on */
2165 if (mm->lupd_pending) {
2166 LOGP(DMM, LOGL_INFO, "Loc. upd. already pending.\n");
2170 /* start periodic location update */
2172 mm->lupd_pending = 1;
2173 mm->lupd_ra_failure = 0;
2175 return gsm48_mm_loc_upd(ms, msg);
2178 /* ignore location update */
2179 static int gsm48_mm_loc_upd_ignore(struct osmocom_ms *ms, struct msgb *msg)
2184 /* 9.2.15 send LOCATION UPDATING REQUEST message */
2185 static int gsm48_mm_tx_loc_upd_req(struct osmocom_ms *ms)
2187 struct gsm48_mmlayer *mm = &ms->mmlayer;
2188 struct gsm_support *sup = &ms->support;
2189 struct gsm_subscriber *subscr = &ms->subscr;
2190 struct gsm48_rrlayer *rr = &ms->rrlayer;
2192 struct gsm48_hdr *ngh;
2193 struct gsm48_loc_upd_req *nlu; /* NOTE: mi_len is part of struct */
2197 LOGP(DMM, LOGL_INFO, "LOCATION UPDATING REQUEST\n");
2199 nmsg = gsm48_l3_msgb_alloc();
2202 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
2203 nlu = (struct gsm48_loc_upd_req *)msgb_put(nmsg, sizeof(*nlu));
2205 ngh->proto_discr = GSM48_PDISC_MM;
2206 ngh->msg_type = GSM48_MT_MM_LOC_UPD_REQUEST;
2208 /* location updating type */
2209 nlu->type = mm->lupd_type;
2211 nlu->key_seq = subscr->key_seq;
2212 /* LAI (use last SIM stored LAI) */
2213 gsm48_generate_lai(&nlu->lai,
2214 subscr->lai_mcc, subscr->lai_mnc, subscr->lai_lac);
2216 if (rr->cd_now.arfcn >= 512 && rr->cd_now.arfcn <= 885)
2217 pwr_lev = sup->pwr_lev_1800;
2219 pwr_lev = sup->pwr_lev_900;
2220 gsm48_encode_classmark1(&nlu->classmark1, sup->rev_lev, sup->es_ind,
2221 sup->a5_1, pwr_lev);
2223 if (subscr->tmsi_valid) /* have TMSI ? */
2224 gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_TMSI);
2226 gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_IMSI);
2227 msgb_put(nmsg, buf[1]); /* length is part of nlu */
2228 memcpy(&nlu->mi_len, buf + 1, 1 + buf[1]);
2230 new_mm_state(mm, GSM48_MM_ST_WAIT_RR_CONN_LUPD, 0);
2232 /* push RR header and send down */
2233 return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_EST_REQ, RR_EST_CAUSE_LOC_UPD);
2236 /* 4.4.4.1 RR is esablised during location update */
2237 static int gsm48_mm_est_loc_upd(struct osmocom_ms *ms, struct msgb *msg)
2239 struct gsm48_mmlayer *mm = &ms->mmlayer;
2241 /* start location update timer */
2244 new_mm_state(mm, GSM48_MM_ST_LOC_UPD_INIT, 0);
2249 /* 4.4.4.6 LOCATION UPDATING ACCEPT is received */
2250 static int gsm48_mm_rx_loc_upd_acc(struct osmocom_ms *ms, struct msgb *msg)
2252 struct gsm48_mmlayer *mm = &ms->mmlayer;
2253 struct gsm_subscriber *subscr = &ms->subscr;
2254 struct gsm48_hdr *gh = msgb_l3(msg);
2255 struct gsm48_loc_area_id *lai = (struct gsm48_loc_area_id *) gh->data;
2256 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2257 struct tlv_parsed tp;
2260 if (payload_len < sizeof(struct gsm48_loc_area_id)) {
2262 LOGP(DMM, LOGL_NOTICE, "Short read of LOCATION UPDATING ACCEPT "
2263 "message error.\n");
2266 tlv_parse(&tp, &gsm48_mm_att_tlvdef,
2267 gh->data + sizeof(struct gsm48_loc_area_id),
2268 payload_len - sizeof(struct gsm48_loc_area_id), 0, 0);
2270 /* update has finished */
2271 mm->lupd_pending = 0;
2273 /* RA was successfull */
2274 mm->lupd_ra_failure = 0;
2276 /* stop periodic location updating timer */
2277 stop_mm_t3212(mm); /* 4.4.2 */
2280 subscr->lai_valid = 1;
2281 gsm48_decode_lai(lai, &subscr->lai_mcc, &subscr->lai_mnc,
2284 /* stop location update timer */
2287 /* reset attempt counter */
2288 mm->lupd_attempt = 0;
2290 /* mark SIM as attached */
2291 subscr->imsi_attached = 1;
2293 /* set the status in the sim to updated */
2294 new_sim_ustate(subscr, GSM_SIM_U1_UPDATED);
2296 sim: apply update state
2299 /* set last registered PLMN */
2300 subscr->plmn_valid = 1;
2301 subscr->plmn_mcc = subscr->lai_mcc;
2302 subscr->plmn_mnc = subscr->lai_mnc;
2307 LOGP(DSUM, LOGL_INFO, "Location update accepted\n");
2308 LOGP(DMM, LOGL_INFO, "LOCATION UPDATING ACCEPT (mcc %s mnc %s "
2309 "lac 0x%04x)\n", gsm_print_mcc(subscr->lai_mcc),
2310 gsm_print_mnc(subscr->lai_mnc), subscr->lai_lac);
2312 /* remove LA from forbidden list */
2313 gsm322_del_forbidden_la(ms, subscr->lai_mcc, subscr->lai_mnc,
2317 if (TLVP_PRESENT(&tp, GSM48_IE_MOBILE_ID)) {
2322 mi = TLVP_VAL(&tp, GSM48_IE_MOBILE_ID)-1;
2325 mi_type = mi[1] & GSM_MI_TYPE_MASK;
2327 case GSM_MI_TYPE_TMSI:
2328 if (payload_len + sizeof(struct gsm48_loc_area_id) < 6
2331 memcpy(&tmsi, mi+2, 4);
2332 subscr->tmsi = ntohl(tmsi);
2333 subscr->tmsi_valid = 1;
2334 LOGP(DMM, LOGL_INFO, "got TMSI 0x%08x (%u)\n",
2335 subscr->tmsi, subscr->tmsi);
2340 case GSM_MI_TYPE_IMSI:
2341 LOGP(DMM, LOGL_INFO, "TMSI removed\n");
2342 subscr->tmsi_valid = 0;
2346 /* send TMSI REALLOCATION COMPLETE */
2347 gsm48_mm_tx_tmsi_reall_cpl(ms);
2350 LOGP(DMM, LOGL_NOTICE, "TMSI reallocation with unknown "
2351 "MI type %d.\n", mi_type);
2355 /* send message to PLMN search process */
2356 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_SUCCESS);
2359 gsm322_plmn_sendmsg(ms, nmsg);
2361 /* follow on proceed */
2362 if (TLVP_PRESENT(&tp, GSM48_IE_MOBILE_ID))
2363 LOGP(DMM, LOGL_NOTICE, "follow-on proceed not supported.\n");
2365 /* start RR release timer */
2368 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
2373 /* 4.4.4.7 LOCATION UPDATING REJECT is received */
2374 static int gsm48_mm_rx_loc_upd_rej(struct osmocom_ms *ms, struct msgb *msg)
2376 struct gsm48_mmlayer *mm = &ms->mmlayer;
2377 struct gsm48_hdr *gh = msgb_l3(msg);
2378 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2380 if (payload_len < 1) {
2381 LOGP(DMM, LOGL_NOTICE, "Short read of LOCATION UPDATING REJECT "
2382 "message error.\n");
2386 /* RA was successfull */
2387 mm->lupd_ra_failure = 0;
2389 /* stop periodic location updating timer */
2390 stop_mm_t3212(mm); /* 4.4.2 */
2392 /* stop location update timer */
2395 /* store until RR is released */
2396 mm->lupd_rej_cause = *gh->data;
2398 /* start RR release timer */
2401 new_mm_state(mm, GSM48_MM_ST_LOC_UPD_REJ, 0);
2406 /* 4.4.4.7 RR is released after location update reject */
2407 static int gsm48_mm_rel_loc_upd_rej(struct osmocom_ms *ms, struct msgb *msg)
2409 struct gsm48_mmlayer *mm = &ms->mmlayer;
2410 struct gsm_subscriber *subscr = &ms->subscr;
2412 struct gsm322_msg *ngm;
2414 LOGP(DMM, LOGL_INFO, "Loc. upd. rejected (cause %d)\n",
2415 mm->lupd_rej_cause);
2417 /* stop RR release timer */
2421 switch (mm->lupd_rej_cause) {
2422 case GSM48_REJECT_IMSI_UNKNOWN_IN_HLR:
2423 case GSM48_REJECT_ILLEGAL_MS:
2424 case GSM48_REJECT_ILLEGAL_ME:
2425 /* reset attempt counter */
2426 mm->lupd_attempt = 0;
2429 subscr->sim_valid = 0;
2432 case GSM48_REJECT_PLMN_NOT_ALLOWED:
2433 case GSM48_REJECT_LOC_NOT_ALLOWED:
2434 case GSM48_REJECT_ROAMING_NOT_ALLOWED:
2435 /* TMSI and LAI invalid */
2436 subscr->lai_valid = 0;
2437 subscr->tmsi_valid = 0;
2439 /* key is invalid */
2440 subscr->key_seq = 7;
2443 new_sim_ustate(subscr, GSM_SIM_U3_ROAMING_NA);
2445 sim: delete tmsi, lai
2446 sim: delete key seq number
2447 sim: apply update state
2449 /* update has finished */
2450 mm->lupd_pending = 0;
2453 /* send event to PLMN search process */
2454 switch(mm->lupd_rej_cause) {
2455 case GSM48_REJECT_ROAMING_NOT_ALLOWED:
2456 nmsg = gsm322_msgb_alloc(GSM322_EVENT_ROAMING_NA);
2458 case GSM48_REJECT_IMSI_UNKNOWN_IN_HLR:
2459 case GSM48_REJECT_ILLEGAL_MS:
2460 case GSM48_REJECT_ILLEGAL_ME:
2461 nmsg = gsm322_msgb_alloc(GSM322_EVENT_INVALID_SIM);
2464 nmsg = gsm322_msgb_alloc(GSM322_EVENT_REG_FAILED);
2468 ngm = (struct gsm322_msg *)nmsg->data;
2469 ngm->reject = mm->lupd_rej_cause;
2470 gsm322_plmn_sendmsg(ms, nmsg);
2472 /* forbidden list */
2473 switch (mm->lupd_rej_cause) {
2474 case GSM48_REJECT_IMSI_UNKNOWN_IN_HLR:
2475 LOGP(DSUM, LOGL_INFO, "Location update failed (IMSI unknown "
2478 case GSM48_REJECT_ILLEGAL_MS:
2479 LOGP(DSUM, LOGL_INFO, "Location update failed (Illegal MS)\n");
2481 case GSM48_REJECT_ILLEGAL_ME:
2482 LOGP(DSUM, LOGL_INFO, "Location update failed (Illegal ME)\n");
2484 case GSM48_REJECT_PLMN_NOT_ALLOWED:
2485 gsm_subscr_add_forbidden_plmn(subscr, mm->lupd_mcc,
2486 mm->lupd_mnc, mm->lupd_rej_cause);
2487 LOGP(DSUM, LOGL_INFO, "Location update failed (PLMN not "
2490 case GSM48_REJECT_LOC_NOT_ALLOWED:
2491 case GSM48_REJECT_ROAMING_NOT_ALLOWED:
2492 gsm322_add_forbidden_la(ms, mm->lupd_mcc, mm->lupd_mnc,
2493 mm->lupd_lac, mm->lupd_rej_cause);
2494 LOGP(DSUM, LOGL_INFO, "Location update failed (LAI not "
2498 /* 4.4.4.9 continue with failure handling */
2499 return gsm48_mm_loc_upd_failed(ms, NULL);
2502 /* CS proc triggers: return to IDLE, case 13 is also handled there */
2506 /* 4.2.2 delay a location update */
2507 static int gsm48_mm_loc_upd_delay_per(struct osmocom_ms *ms, struct msgb *msg)
2509 struct gsm48_mmlayer *mm = &ms->mmlayer;
2511 LOGP(DMM, LOGL_INFO, "Schedule a pending periodic loc. upd.\n");
2512 mm->lupd_periodic = 1;
2517 /* delay a location update retry */
2518 static int gsm48_mm_loc_upd_delay_retry(struct osmocom_ms *ms, struct msgb *msg)
2520 struct gsm48_mmlayer *mm = &ms->mmlayer;
2522 LOGP(DMM, LOGL_INFO, "Schedule a pending periodic loc. upd.\n");
2528 /* process failues as described in the lower part of 4.4.4.9 */
2529 static int gsm48_mm_loc_upd_failed(struct osmocom_ms *ms, struct msgb *msg)
2531 struct gsm48_mmlayer *mm = &ms->mmlayer;
2532 struct gsm_subscriber *subscr = &ms->subscr;
2534 LOGP(DSUM, LOGL_INFO, "Location update failed\n");
2536 /* stop location update timer, if running */
2539 if (subscr->ustate == GSM_SIM_U1_UPDATED
2540 && mm->lupd_mcc == subscr->lai_mcc
2541 && mm->lupd_mnc == subscr->lai_mnc
2542 && mm->lupd_lac == subscr->lai_lac) {
2543 if (mm->lupd_attempt < 4) {
2544 LOGP(DSUM, LOGL_INFO, "Try location update later\n");
2545 LOGP(DMM, LOGL_INFO, "Loc. upd. failed, retry #%d\n",
2548 /* start update retry timer */
2551 /* CS process will trigger: return to MM IDLE */
2554 LOGP(DMM, LOGL_INFO, "Loc. upd. failed too often.\n");
2557 /* TMSI and LAI invalid */
2558 subscr->lai_valid = 0;
2559 subscr->tmsi_valid = 0;
2561 /* key is invalid */
2562 subscr->key_seq = 7;
2565 new_sim_ustate(subscr, GSM_SIM_U2_NOT_UPDATED);
2568 sim: delete tmsi, lai
2569 sim: delete key seq number
2570 sim: set update status
2573 /* start update retry timer (RR connection is released) */
2574 if (mm->lupd_attempt < 4) {
2575 mm->start_t3211 = 1;
2576 LOGP(DSUM, LOGL_INFO, "Try location update later\n");
2579 /* CS process will trigger: return to MM IDLE */
2583 /* abort a location update due to radio failure or release */
2584 static int gsm48_mm_rel_loc_upd_abort(struct osmocom_ms *ms, struct msgb *msg)
2586 struct gsm48_mmlayer *mm = &ms->mmlayer;
2587 struct gsm48_rr_hdr *rrh = (struct gsm48_rr_hdr *)msg->data;
2589 /* stop RR release timer */
2592 if (rrh->msg_type == GSM48_RR_REL_IND) {
2593 LOGP(DMM, LOGL_INFO, "RR link released after loc. upd.\n");
2595 /* continue with failure handling */
2596 return gsm48_mm_loc_upd_failed(ms, NULL);
2599 LOGP(DMM, LOGL_INFO, "Loc. upd. aborted by radio (cause #%d)\n",
2602 /* random access failure, but not two successive failures */
2603 if (rrh->cause == RR_REL_CAUSE_RA_FAILURE && !mm->lupd_ra_failure) {
2604 mm->lupd_ra_failure = 1;
2606 /* start RA failure timer */
2612 /* RA was successfull or sent twice */
2613 mm->lupd_ra_failure = 0;
2615 /* continue with failure handling */
2616 return gsm48_mm_loc_upd_failed(ms, NULL);
2619 /* location update has timed out */
2620 static int gsm48_mm_loc_upd_timeout(struct osmocom_ms *ms, struct msgb *msg)
2623 struct gsm48_rr_hdr *nrrh;
2625 /* abort RR connection */
2626 nmsg = gsm48_rr_msgb_alloc(GSM48_RR_ABORT_REQ);
2629 nrrh = (struct gsm48_rr_hdr *) msgb_put(nmsg, sizeof(*nrrh));
2630 nrrh->cause = GSM48_RR_CAUSE_ABNORMAL_TIMER;
2631 gsm48_rr_downmsg(ms, nmsg);
2633 /* continue with failure handling */
2634 return gsm48_mm_loc_upd_failed(ms, NULL);
2638 * process handlers for MM connections
2641 /* cm reestablish request message from upper layer */
2642 static int gsm48_mm_tx_cm_serv_req(struct osmocom_ms *ms, int rr_prim,
2643 uint8_t cause, uint8_t cm_serv)
2645 struct gsm_subscriber *subscr = &ms->subscr;
2646 struct gsm_settings *set = &ms->settings;
2648 struct gsm48_hdr *ngh;
2649 struct gsm48_service_request *nsr; /* NOTE: includes MI length */
2653 LOGP(DMM, LOGL_INFO, "CM SERVICE REQUEST (cause %d)\n", cause);
2655 nmsg = gsm48_l3_msgb_alloc();
2658 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
2659 nsr = (struct gsm48_service_request *)msgb_put(nmsg, sizeof(*nsr));
2660 cm2lv = (uint8_t *)&nsr->classmark;
2662 ngh->proto_discr = GSM48_PDISC_MM;
2663 ngh->msg_type = GSM48_MT_MM_CM_SERV_REQ;
2666 nsr->cm_service_type = cm_serv;
2667 nsr->cipher_key_seq = subscr->key_seq;
2669 cm2lv[0] = sizeof(struct gsm48_classmark2);
2670 gsm48_rr_enc_cm2(ms, (struct gsm48_classmark2 *)(cm2lv + 1));
2672 if (!subscr->sim_valid) { /* have no SIM ? */
2673 if (set->emergency_imsi[0])
2674 gsm48_generate_mid_from_imsi(buf,
2675 set->emergency_imsi);
2677 gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_IMEI);
2678 } else if (subscr->tmsi_valid) /* have TMSI ? */
2679 gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_TMSI);
2681 gsm48_encode_mi(buf, NULL, ms, GSM_MI_TYPE_IMSI);
2682 msgb_put(nmsg, buf[1]); /* length is part of nsr */
2683 memcpy(&nsr->mi_len, buf + 1, 1 + buf[1]);
2684 /* prio is optional for eMLPP */
2686 /* push RR header and send down */
2687 return gsm48_mm_to_rr(ms, nmsg, rr_prim, cause);
2690 /* cm service abort message from upper layer
2691 * NOTE: T3240 is started by the calling function
2693 static int gsm48_mm_tx_cm_service_abort(struct osmocom_ms *ms)
2696 struct gsm48_hdr *ngh;
2698 LOGP(DMM, LOGL_INFO, "CM SERVICE ABORT\n");
2700 nmsg = gsm48_l3_msgb_alloc();
2703 ngh = (struct gsm48_hdr *)msgb_put(nmsg, sizeof(*ngh));
2705 ngh->proto_discr = GSM48_PDISC_MM;
2706 ngh->msg_type = GSM48_MT_MM_CM_SERV_ABORT;
2708 /* push RR header and send down */
2709 return gsm48_mm_to_rr(ms, nmsg, GSM48_RR_DATA_REQ, 0);
2712 /* cm service acknowledge is received from lower layer */
2713 static int gsm48_mm_rx_cm_service_acc(struct osmocom_ms *ms, struct msgb *msg)
2715 struct gsm48_mmlayer *mm = &ms->mmlayer;
2717 /* stop MM connection timer */
2720 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
2722 return gsm48_mm_conn_go_dedic(ms);
2725 /* 9.2.6 CM SERVICE REJECT message received */
2726 static int gsm48_mm_rx_cm_service_rej(struct osmocom_ms *ms, struct msgb *msg)
2728 struct gsm48_mmlayer *mm = &ms->mmlayer;
2729 struct gsm_subscriber *subscr = &ms->subscr;
2730 struct gsm48_hdr *gh = msgb_l3(msg);
2731 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2732 uint8_t abort_any = 0;
2733 uint8_t reject_cause;
2735 if (payload_len < 1) {
2736 LOGP(DMM, LOGL_NOTICE, "Short read of cm service reject "
2737 "message error.\n");
2742 reject_cause = *gh->data;
2744 LOGP(DMM, LOGL_INFO, "CM SERVICE REJECT (cause %d)\n", reject_cause);
2746 /* stop MM connection timer */
2749 /* selection action on cause value */
2750 switch (reject_cause) {
2751 case GSM48_REJECT_IMSI_UNKNOWN_IN_VLR:
2752 case GSM48_REJECT_ILLEGAL_ME:
2755 /* TMSI and LAI invalid */
2756 subscr->lai_valid = 0;
2757 subscr->tmsi_valid = 0;
2759 /* key is invalid */
2760 subscr->key_seq = 7;
2763 new_sim_ustate(subscr, GSM_SIM_U2_NOT_UPDATED);
2766 sim: delete tmsi, lai
2767 sim: delete key seq number
2768 sim: set update status
2771 /* change to WAIT_NETWORK_CMD state impied by abort_any == 1 */
2773 if (reject_cause == GSM48_REJECT_ILLEGAL_ME)
2774 subscr->sim_valid = 0;
2778 /* state implied by the number of remaining connections */
2782 /* release MM connection(s) */
2783 gsm48_mm_release_mm_conn(ms, abort_any, 16, 0);
2785 /* state depends on the existance of remaining MM connections */
2786 if (llist_empty(&mm->mm_conn))
2787 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
2789 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
2794 /* initiate an MM connection 4.5.1.1
2796 * this function is called when:
2797 * - no RR connection exists
2798 * - an RR connection exists, but this is the first MM connection
2799 * - an RR connection exists, and there are already MM connection(s)
2801 static int gsm48_mm_init_mm(struct osmocom_ms *ms, struct msgb *msg,
2804 struct gsm48_mmlayer *mm = &ms->mmlayer;
2805 struct gsm_subscriber *subscr = &ms->subscr;
2806 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
2807 int msg_type = mmh->msg_type;
2809 uint8_t cause = 0, cm_serv = 0, proto = 0;
2811 struct gsm48_mmxx_hdr *nmmh;
2812 struct gsm48_mm_conn *conn, *conn_found = NULL;
2814 /* reset loc. upd. counter on CM service request */
2815 mm->lupd_attempt = 0;
2817 /* find if there is already a pending connection */
2818 llist_for_each_entry(conn, &mm->mm_conn, list) {
2819 if (conn->state == GSM48_MMXX_ST_CONN_PEND) {
2825 /* if pending connection */
2827 LOGP(DMM, LOGL_INFO, "Init MM Connection, but already have "
2828 "pending MM Connection.\n");
2833 case GSM48_MMCC_EST_REQ:
2834 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_REL_IND,
2835 mmh->ref, mmh->transaction_id);
2837 case GSM48_MMSS_EST_REQ:
2838 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSS_REL_IND,
2839 mmh->ref, mmh->transaction_id);
2841 case GSM48_MMSMS_EST_REQ:
2842 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSMS_REL_IND,
2843 mmh->ref, mmh->transaction_id);
2848 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
2849 nmmh->cause = cause;
2850 gsm48_mmxx_upmsg(ms, nmsg);
2854 /* in case of an emergency setup */
2855 if (msg_type == GSM48_MMCC_EST_REQ && mmh->emergency)
2858 /* if sim is not updated */
2859 if (!emergency && subscr->ustate != GSM_SIM_U1_UPDATED) {
2860 LOGP(DMM, LOGL_INFO, "Init MM Connection, but SIM not "
2866 if (mm->state == GSM48_MM_ST_MM_IDLE) {
2867 /* current MM idle state */
2868 switch (mm->substate) {
2869 case GSM48_MM_SST_NORMAL_SERVICE:
2870 case GSM48_MM_SST_PLMN_SEARCH_NORMAL:
2871 LOGP(DMM, LOGL_INFO, "Init MM Connection.\n");
2872 break; /* allow when normal */
2873 case GSM48_MM_SST_LOC_UPD_NEEDED:
2874 case GSM48_MM_SST_ATTEMPT_UPDATE:
2875 /* store mm request if attempting to update */
2877 LOGP(DMM, LOGL_INFO, "Init MM Connection, but "
2878 "attempting to update.\n");
2881 /* TODO: implement delay and start loc upd. */
2885 /* reject if not emergency */
2887 LOGP(DMM, LOGL_INFO, "Init MM Connection, not "
2888 "in normal state.\n");
2895 LOGP(DMM, LOGL_INFO, "Init another MM Connection.\n");
2897 /* set cause, service, proto */
2899 case GSM48_MMCC_EST_REQ:
2901 cause = RR_EST_CAUSE_EMERGENCY;
2902 cm_serv = GSM48_CMSERV_EMERGENCY;
2904 cause = RR_EST_CAUSE_ORIG_TCHF;
2905 cm_serv = GSM48_CMSERV_MO_CALL_PACKET;
2907 proto = GSM48_PDISC_CC;
2909 case GSM48_MMSS_EST_REQ:
2910 cause = RR_EST_CAUSE_OTHER_SDCCH;
2911 cm_serv = GSM48_CMSERV_SUP_SERV;
2912 proto = GSM48_PDISC_NC_SS;
2914 case GSM48_MMSMS_EST_REQ:
2915 cause = RR_EST_CAUSE_OTHER_SDCCH;
2916 cm_serv = GSM48_CMSERV_SMS;
2917 proto = GSM48_PDISC_SMS;
2921 /* create MM connection instance */
2922 conn = mm_conn_new(mm, proto, mmh->transaction_id, mmh->ref);
2926 new_conn_state(conn, GSM48_MMXX_ST_CONN_PEND);
2928 /* send CM SERVICE REQUEST */
2930 return gsm48_mm_tx_cm_serv_req(ms, rr_prim, cause, cm_serv);
2935 /* 4.5.1.1 a) MM connection request triggers RR connection */
2936 static int gsm48_mm_init_mm_no_rr(struct osmocom_ms *ms, struct msgb *msg)
2938 struct gsm48_mmlayer *mm = &ms->mmlayer;
2941 /* start MM connection by requesting RR connection */
2942 rc = gsm48_mm_init_mm(ms, msg, GSM48_RR_EST_REQ);
2946 new_mm_state(mm, GSM48_MM_ST_WAIT_RR_CONN_MM_CON, 0);
2951 /* 4.5.1.1 a) RR is esablised during mm connection, wait for CM accepted */
2952 static int gsm48_mm_est_mm_con(struct osmocom_ms *ms, struct msgb *msg)
2954 struct gsm48_mmlayer *mm = &ms->mmlayer;
2956 /* 4.5.1.7 if there is no more MM connection */
2957 if (llist_empty(&mm->mm_conn)) {
2958 LOGP(DMM, LOGL_INFO, "MM Connection, are already gone.\n");
2960 /* start RR release timer */
2963 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
2966 return gsm48_mm_tx_cm_service_abort(ms);
2969 /* start MM connection timer */
2972 new_mm_state(mm, GSM48_MM_ST_WAIT_OUT_MM_CONN, 0);
2977 /* 4.5.1.1 b) MM connection request on existing RR connection */
2978 static int gsm48_mm_init_mm_first(struct osmocom_ms *ms, struct msgb *msg)
2980 struct gsm48_mmlayer *mm = &ms->mmlayer;
2983 /* start MM connection by sending data */
2984 rc = gsm48_mm_init_mm(ms, msg, GSM48_RR_DATA_REQ);
2988 /* stop "RR connection release not allowed" timer */
2991 /* start MM connection timer */
2994 new_mm_state(mm, GSM48_MM_ST_WAIT_OUT_MM_CONN, 0);
2999 /* 4.5.1.1 b) another MM connection request on existing RR connection */
3000 static int gsm48_mm_init_mm_more(struct osmocom_ms *ms, struct msgb *msg)
3002 struct gsm48_mmlayer *mm = &ms->mmlayer;
3005 /* start MM connection by sending data */
3006 rc = gsm48_mm_init_mm(ms, msg, GSM48_RR_DATA_REQ);
3010 /* start MM connection timer */
3013 new_mm_state(mm, GSM48_MM_ST_WAIT_ADD_OUT_MM_CON, 0);
3018 /* 4.5.1.1 b) delay on WAIT FOR NETWORK COMMAND state */
3019 static int gsm48_mm_init_mm_wait(struct osmocom_ms *ms, struct msgb *msg)
3022 gsm48_mm_init_mm_reject(ms, msg);
3024 this requires handling when leaving this state...
3026 struct gsm48_mmlayer *mm = &ms->mmlayer;
3029 /* just create the MM connection in pending state */
3030 rc = gsm48_mm_init_mm(ms, msg, 0);
3034 /* start MM connection timer */
3037 new_mm_state(mm, GSM48_MM_ST_WAIT_ADD_OUT_MM_CON, 0);
3043 /* initiate an mm connection other cases */
3044 static int gsm48_mm_init_mm_reject(struct osmocom_ms *ms, struct msgb *msg)
3046 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3047 int msg_type = mmh->msg_type;
3049 struct gsm48_mmxx_hdr *nmmh;
3054 case GSM48_MMCC_EST_REQ:
3055 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_REL_IND, mmh->ref,
3056 mmh->transaction_id);
3058 case GSM48_MMSS_EST_REQ:
3059 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSS_REL_IND, mmh->ref,
3060 mmh->transaction_id);
3062 case GSM48_MMSMS_EST_REQ:
3063 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSMS_REL_IND, mmh->ref,
3064 mmh->transaction_id);
3069 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
3071 gsm48_mmxx_upmsg(ms, nmsg);
3076 /* accepting pending connection, got dedicated mode
3078 * this function is called:
3079 * - when ciphering command is received
3080 * - when cm service is accepted
3082 static int gsm48_mm_conn_go_dedic(struct osmocom_ms *ms)
3084 struct gsm48_mmlayer *mm = &ms->mmlayer;
3085 struct gsm48_mm_conn *conn, *conn_found = NULL;
3087 struct gsm48_mmxx_hdr *nmmh;
3089 /* the first and only pending connection is the recent requested */
3090 llist_for_each_entry(conn, &mm->mm_conn, list) {
3091 if (conn->state == GSM48_MMXX_ST_CONN_PEND) {
3097 /* if no pending connection (anymore) */
3099 LOGP(DMM, LOGL_INFO, "No pending MM Connection.\n");
3104 new_conn_state(conn, GSM48_MMXX_ST_DEDICATED);
3106 /* send establishment confirm */
3108 switch(conn_found->protocol) {
3109 case GSM48_PDISC_CC:
3110 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_EST_CNF, conn->ref,
3111 conn->transaction_id);
3113 case GSM48_PDISC_NC_SS:
3114 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSS_EST_CNF, conn->ref,
3115 conn->transaction_id);
3117 case GSM48_PDISC_SMS:
3118 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMSMS_EST_CNF, conn->ref,
3119 conn->transaction_id);
3124 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
3126 gsm48_mmxx_upmsg(ms, nmsg);
3131 /* a RR-SYNC-IND is received during MM connection establishment */
3132 static int gsm48_mm_sync_ind_wait(struct osmocom_ms *ms, struct msgb *msg)
3134 struct gsm48_mmlayer *mm = &ms->mmlayer;
3136 /* stop MM connection timer */
3139 return gsm48_mm_conn_go_dedic(ms);
3142 /* a RR-SYNC-IND is received during MM connection active */
3143 static int gsm48_mm_sync_ind_active(struct osmocom_ms *ms, struct msgb *msg)
3145 struct gsm48_mmlayer *mm = &ms->mmlayer;
3146 struct gsm48_mm_conn *conn;
3148 struct gsm48_mmxx_hdr *nmmh;
3150 /* stop MM connection timer */
3153 /* broadcast all MMCC connection(s) */
3154 llist_for_each_entry(conn, &mm->mm_conn, list) {
3155 /* send MMCC-SYNC-IND */
3157 switch(conn->protocol) {
3158 case GSM48_PDISC_CC:
3159 nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_SYNC_IND,
3160 conn->ref, conn->transaction_id);
3164 continue; /* skip if not of CC type */
3165 nmmh = (struct gsm48_mmxx_hdr *)nmsg->data;
3167 /* copy L3 message */
3168 nmsg->l3h = msgb_put(nmsg, msgb_l3len(msg));
3169 memcpy(nmsg->l3h, msg->l3h, msgb_l3len(msg));
3170 gsm48_mmxx_upmsg(ms, nmsg);
3176 /* 4.5.1.2 RR abort/release is received during MM connection establishment */
3177 static int gsm48_mm_abort_mm_con(struct osmocom_ms *ms, struct msgb *msg)
3179 struct gsm48_mmlayer *mm = &ms->mmlayer;
3180 struct gsm48_rr_hdr *rrh = (struct gsm48_rr_hdr *)msg->data;
3183 /* stop RR release timer */
3186 /* this conversion is not of any standard */
3187 switch(rrh->cause) {
3188 case RR_REL_CAUSE_NOT_AUTHORIZED:
3189 case RR_REL_CAUSE_EMERGENCY_ONLY:
3190 case RR_REL_CAUSE_TRY_LATER:
3193 case RR_REL_CAUSE_NORMAL:
3200 /* stop MM connection timer */
3203 /* release all connections */
3204 gsm48_mm_release_mm_conn(ms, 1, cause, 1);
3206 /* no RR connection, so we return to MM IDLE */
3207 if (mm->state == GSM48_MM_ST_WAIT_RR_CONN_MM_CON)
3208 return gsm48_mm_return_idle(ms, NULL);
3210 /* CS process will trigger: return to MM IDLE */
3214 /* 4.5.1.2 timeout is received during MM connection establishment */
3215 static int gsm48_mm_timeout_mm_con(struct osmocom_ms *ms, struct msgb *msg)
3217 struct gsm48_mmlayer *mm = &ms->mmlayer;
3219 /* release pending connection */
3220 gsm48_mm_release_mm_conn(ms, 0, 102, 0);
3222 /* state depends on the existance of remaining MM connections */
3223 if (llist_empty(&mm->mm_conn)) {
3224 /* start RR release timer */
3227 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
3229 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
3234 /* respond to paging */
3235 static int gsm48_mm_est(struct osmocom_ms *ms, struct msgb *msg)
3237 struct gsm48_mmlayer *mm = &ms->mmlayer;
3239 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
3245 static int gsm48_mm_data(struct osmocom_ms *ms, struct msgb *msg)
3247 struct gsm48_mmlayer *mm = &ms->mmlayer;
3248 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3249 struct gsm48_mm_conn *conn;
3250 int msg_type = mmh->msg_type;
3252 /* get connection, if not exist (anymore), release */
3253 conn = mm_conn_by_ref(mm, mmh->ref);
3255 LOGP(DMM, LOGL_INFO, "MMXX_DATA_REQ with unknown (already "
3256 "released) ref=%x, sending MMXX_REL_IND\n", mmh->ref);
3257 switch(msg_type & GSM48_MMXX_MASK) {
3258 case GSM48_MMCC_CLASS:
3259 mmh->msg_type = GSM48_MMCC_REL_IND;
3261 case GSM48_MMSS_CLASS:
3262 mmh->msg_type = GSM48_MMSS_REL_IND;
3264 case GSM48_MMSMS_CLASS:
3265 mmh->msg_type = GSM48_MMSMS_REL_IND;
3270 /* mirror message with REL_IND + cause */
3271 return gsm48_mmxx_upmsg(ms, msg);
3274 /* pull MM header */
3275 msgb_pull(msg, sizeof(struct gsm48_mmxx_hdr));
3277 /* push RR header and send down */
3278 return gsm48_mm_to_rr(ms, msg, GSM48_RR_DATA_REQ, 0);
3281 /* release of MM connection (active state) */
3282 static int gsm48_mm_release_active(struct osmocom_ms *ms, struct msgb *msg)
3284 struct gsm48_mmlayer *mm = &ms->mmlayer;
3285 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3286 struct gsm48_mm_conn *conn;
3288 /* get connection, if not exist (anymore), release */
3289 conn = mm_conn_by_ref(mm, mmh->ref);
3293 /* state depends on the existance of remaining MM connections */
3294 if (llist_empty(&mm->mm_conn)) {
3295 /* start RR release timer */
3298 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
3300 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
3305 /* release of MM connection (wait for additional state) */
3306 static int gsm48_mm_release_wait_add(struct osmocom_ms *ms, struct msgb *msg)
3308 struct gsm48_mmlayer *mm = &ms->mmlayer;
3309 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3310 struct gsm48_mm_conn *conn;
3312 /* get connection, if not exist (anymore), release */
3313 conn = mm_conn_by_ref(mm, mmh->ref);
3320 /* release of MM connection (wait for active state) */
3321 static int gsm48_mm_release_wait_active(struct osmocom_ms *ms, struct msgb *msg)
3323 struct gsm48_mmlayer *mm = &ms->mmlayer;
3324 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3325 struct gsm48_mm_conn *conn;
3327 /* get connection, if not exist (anymore), release */
3328 conn = mm_conn_by_ref(mm, mmh->ref);
3332 /* 4.5.1.7 if there is no MM connection during wait for active state */
3333 if (llist_empty(&mm->mm_conn)) {
3334 LOGP(DMM, LOGL_INFO, "No MM Connection during 'wait for "
3335 "active' state.\n");
3337 /* start RR release timer */
3340 new_mm_state(mm, GSM48_MM_ST_WAIT_NETWORK_CMD, 0);
3343 return gsm48_mm_tx_cm_service_abort(ms);
3349 /* release of MM connection (wait for RR state) */
3350 static int gsm48_mm_release_wait_rr(struct osmocom_ms *ms, struct msgb *msg)
3352 struct gsm48_mmlayer *mm = &ms->mmlayer;
3353 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3354 struct gsm48_mm_conn *conn;
3356 /* get connection, if not exist (anymore), release */
3357 conn = mm_conn_by_ref(mm, mmh->ref);
3361 /* later, if RR connection is established, the CM SERIVE ABORT
3362 * message will be sent
3367 /* abort RR connection (due to T3240) */
3368 static int gsm48_mm_abort_rr(struct osmocom_ms *ms, struct msgb *msg)
3371 struct gsm48_rr_hdr *nrrh;
3373 /* send abort to RR */
3374 nmsg = gsm48_rr_msgb_alloc(GSM48_RR_ABORT_REQ);
3377 nrrh = (struct gsm48_rr_hdr *) msgb_put(nmsg, sizeof(*nrrh));
3378 nrrh->cause = GSM48_RR_CAUSE_ABNORMAL_TIMER;
3379 gsm48_rr_downmsg(ms, nmsg);
3381 /* CS process will trigger: return to MM IDLE / No SIM */
3389 /* RR is released in other states */
3390 static int gsm48_mm_rel_other(struct osmocom_ms *ms, struct msgb *msg)
3392 struct gsm48_mmlayer *mm = &ms->mmlayer;
3394 /* stop RR release timer (if running) */
3397 /* CS process will trigger: return to MM IDLE */
3405 /* state trasitions for MMxx-SAP messages from upper layers */
3406 static struct downstate {
3410 int (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3411 } downstatelist[] = {
3412 /* 4.2.2.1 Normal service */
3413 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3414 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3416 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3417 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_no_rr},
3419 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3420 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_no_rr},
3422 /* 4.2.2.2 Attempt to update / Loc. Upd. needed */
3423 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE) |
3424 SBIT(GSM48_MM_SST_LOC_UPD_NEEDED),
3425 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr}, /* emergency only */
3427 /* 4.2.2.3 Limited service */
3428 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3429 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3431 /* 4.2.2.4 No IMSI */
3432 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NO_IMSI),
3433 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3435 /* 4.2.2.5 PLMN search, normal service */
3436 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3437 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3439 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3440 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_no_rr},
3442 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3443 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_no_rr},
3445 /* 4.2.2.6 PLMN search */
3446 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH),
3447 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_no_rr},
3449 /* 4.5.1.1 MM Connection (EST) */
3450 {SBIT(GSM48_MM_ST_RR_CONN_RELEASE_NA), ALL_STATES,
3451 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_first},
3453 {SBIT(GSM48_MM_ST_RR_CONN_RELEASE_NA), ALL_STATES,
3454 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_first},
3456 {SBIT(GSM48_MM_ST_RR_CONN_RELEASE_NA), ALL_STATES,
3457 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_first},
3459 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3460 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_more},
3462 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3463 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_more},
3465 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3466 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_more},
3468 {SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD), ALL_STATES,
3469 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_wait},
3471 {SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD), ALL_STATES,
3472 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_wait},
3474 {SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD), ALL_STATES,
3475 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_wait},
3477 {ALL_STATES, ALL_STATES,
3478 GSM48_MMCC_EST_REQ, gsm48_mm_init_mm_reject},
3480 {ALL_STATES, ALL_STATES,
3481 GSM48_MMSS_EST_REQ, gsm48_mm_init_mm_reject},
3483 {ALL_STATES, ALL_STATES,
3484 GSM48_MMSMS_EST_REQ, gsm48_mm_init_mm_reject},
3486 /* 4.5.2.1 MM Connection (DATA) */
3487 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3488 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3489 GSM48_MMCC_DATA_REQ, gsm48_mm_data},
3491 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3492 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3493 GSM48_MMSS_DATA_REQ, gsm48_mm_data},
3495 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3496 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3497 GSM48_MMSMS_DATA_REQ, gsm48_mm_data},
3499 /* 4.5.2.1 MM Connection (REL) */
3500 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3501 GSM48_MMCC_REL_REQ, gsm48_mm_release_active},
3503 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3504 GSM48_MMSS_REL_REQ, gsm48_mm_release_active},
3506 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), ALL_STATES,
3507 GSM48_MMSMS_REL_REQ, gsm48_mm_release_active},
3509 {SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3510 GSM48_MMCC_REL_REQ, gsm48_mm_release_wait_add},
3512 {SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3513 GSM48_MMSS_REL_REQ, gsm48_mm_release_wait_add},
3515 {SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), ALL_STATES,
3516 GSM48_MMSMS_REL_REQ, gsm48_mm_release_wait_add},
3518 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN), ALL_STATES,
3519 GSM48_MMCC_REL_REQ, gsm48_mm_release_wait_active},
3521 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN), ALL_STATES,
3522 GSM48_MMSS_REL_REQ, gsm48_mm_release_wait_active},
3524 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN), ALL_STATES,
3525 GSM48_MMSMS_REL_REQ, gsm48_mm_release_wait_active},
3527 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON), ALL_STATES,
3528 GSM48_MMCC_REL_REQ, gsm48_mm_release_wait_rr},
3530 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON), ALL_STATES,
3531 GSM48_MMSS_REL_REQ, gsm48_mm_release_wait_rr},
3533 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON), ALL_STATES,
3534 GSM48_MMSMS_REL_REQ, gsm48_mm_release_wait_rr},
3538 (sizeof(downstatelist) / sizeof(struct downstate))
3540 int gsm48_mmxx_downmsg(struct osmocom_ms *ms, struct msgb *msg)
3542 struct gsm48_mmlayer *mm = &ms->mmlayer;
3543 struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
3544 int msg_type = mmh->msg_type;
3545 struct gsm48_mm_conn *conn;
3548 /* keep up to date with the transaction ID */
3549 conn = mm_conn_by_ref(mm, mmh->ref);
3551 conn->transaction_id = mmh->transaction_id;
3553 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' event in state %s\n",
3554 ms->name, get_mmxx_name(msg_type),
3555 gsm48_mm_state_names[mm->state]);
3556 if (mm->state == GSM48_MM_ST_MM_IDLE)
3557 LOGP(DMM, LOGL_INFO, "-> substate %s\n",
3558 gsm48_mm_substate_names[mm->substate]);
3559 LOGP(DMM, LOGL_INFO, "-> callref %x, transaction_id %d\n",
3560 mmh->ref, mmh->transaction_id);
3562 /* Find function for current state and message */
3563 for (i = 0; i < DOWNSLLEN; i++)
3564 if ((msg_type == downstatelist[i].type)
3565 && ((1 << mm->state) & downstatelist[i].states)
3566 && ((1 << mm->substate) & downstatelist[i].substates))
3568 if (i == DOWNSLLEN) {
3569 LOGP(DMM, LOGL_NOTICE, "Message unhandled at this state.\n");
3574 rc = downstatelist[i].rout(ms, msg);
3576 if (downstatelist[i].rout != gsm48_mm_data)
3582 /* state trasitions for radio ressource messages (lower layer) */
3583 static struct rrdatastate {
3586 int (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3587 } rrdatastatelist[] = {
3589 {SBIT(GSM48_MM_ST_MM_IDLE),
3590 GSM48_RR_EST_IND, gsm48_mm_est},
3593 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_IMSI_D), /* 4.3.4.4 */
3594 GSM48_RR_EST_CNF, gsm48_mm_imsi_detach_sent},
3596 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_IMSI_D), /* 4.3.4.4 (unsuc.) */
3597 GSM48_RR_REL_IND, gsm48_mm_imsi_detach_end},
3598 /* also this may happen if SABM is ackwnowledged with DISC */
3600 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_IMSI_D), /* 4.3.4.4 (lost) */
3601 GSM48_RR_ABORT_IND, gsm48_mm_imsi_detach_end},
3603 {SBIT(GSM48_MM_ST_IMSI_DETACH_INIT), /* 4.3.4.4 (unsuc.) */
3604 GSM48_RR_REL_IND, gsm48_mm_imsi_detach_end},
3606 {SBIT(GSM48_MM_ST_IMSI_DETACH_INIT), /* 4.3.4.4 (lost) */
3607 GSM48_RR_ABORT_IND, gsm48_mm_imsi_detach_end},
3609 /* location update */
3610 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_LUPD), /* 4.4.4.1 */
3611 GSM48_RR_EST_CNF, gsm48_mm_est_loc_upd},
3613 {SBIT(GSM48_MM_ST_LOC_UPD_INIT) |
3614 SBIT(GSM48_MM_ST_WAIT_RR_CONN_LUPD), /* 4.4.4.9 */
3615 GSM48_RR_REL_IND, gsm48_mm_rel_loc_upd_abort},
3617 {SBIT(GSM48_MM_ST_LOC_UPD_INIT) |
3618 SBIT(GSM48_MM_ST_WAIT_RR_CONN_LUPD), /* 4.4.4.9 */
3619 GSM48_RR_ABORT_IND, gsm48_mm_rel_loc_upd_abort},
3621 {SBIT(GSM48_MM_ST_LOC_UPD_REJ), /* 4.4.4.7 */
3622 GSM48_RR_REL_IND, gsm48_mm_rel_loc_upd_rej},
3624 {SBIT(GSM48_MM_ST_LOC_UPD_REJ), /* 4.4.4.7 */
3625 GSM48_RR_ABORT_IND, gsm48_mm_rel_loc_upd_rej},
3627 /* MM connection (EST) */
3628 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON), /* 4.5.1.1 */
3629 GSM48_RR_EST_CNF, gsm48_mm_est_mm_con},
3631 /* MM connection (DATA) */
3633 GSM48_RR_DATA_IND, gsm48_mm_data_ind},
3635 /* MM connection (SYNC) */
3636 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN) |
3637 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), /* 4.5.1.1 */
3638 GSM48_RR_SYNC_IND, gsm48_mm_sync_ind_wait},
3640 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE),
3641 GSM48_RR_SYNC_IND, gsm48_mm_sync_ind_active},
3643 /* MM connection (REL/ABORT) */
3644 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON) |
3645 SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN) |
3646 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), /* 4.5.1.2 */
3647 GSM48_RR_REL_IND, gsm48_mm_abort_mm_con},
3649 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_MM_CON) |
3650 SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN) |
3651 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), /* 4.5.1.2 */
3652 GSM48_RR_ABORT_IND, gsm48_mm_abort_mm_con},
3654 /* MM connection (REL/ABORT with re-establishment possibility) */
3655 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE), /* not supported */
3656 GSM48_RR_REL_IND, gsm48_mm_abort_mm_con},
3658 {SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
3659 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON), /* not supported */
3660 GSM48_RR_ABORT_IND, gsm48_mm_abort_mm_con},
3662 /* other (also wait for network command) */
3664 GSM48_RR_REL_IND, gsm48_mm_rel_other},
3667 GSM48_RR_ABORT_IND, gsm48_mm_rel_other},
3670 #define RRDATASLLEN \
3671 (sizeof(rrdatastatelist) / sizeof(struct rrdatastate))
3673 static int gsm48_rcv_rr(struct osmocom_ms *ms, struct msgb *msg)
3675 struct gsm48_mmlayer *mm = &ms->mmlayer;
3676 struct gsm48_rr_hdr *rrh = (struct gsm48_rr_hdr *)msg->data;
3677 int msg_type = rrh->msg_type;
3680 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' from RR in state %s\n",
3681 ms->name, get_rr_name(msg_type),
3682 gsm48_mm_state_names[mm->state]);
3684 /* find function for current state and message */
3685 for (i = 0; i < RRDATASLLEN; i++)
3686 if ((msg_type == rrdatastatelist[i].type)
3687 && ((1 << mm->state) & rrdatastatelist[i].states))
3689 if (i == RRDATASLLEN) {
3690 LOGP(DMM, LOGL_NOTICE, "Message unhandled at this state.\n");
3695 rc = rrdatastatelist[i].rout(ms, msg);
3697 if (rrdatastatelist[i].rout != gsm48_mm_data_ind)
3703 /* state trasitions for mobile managemnt messages (lower layer) */
3704 static struct mmdatastate {
3707 int (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3708 } mmdatastatelist[] = {
3709 {ALL_STATES, /* 4.3.1.2 */
3710 GSM48_MT_MM_TMSI_REALL_CMD, gsm48_mm_rx_tmsi_realloc_cmd},
3712 {ALL_STATES, /* 4.3.2.2 */
3713 GSM48_MT_MM_AUTH_REQ, gsm48_mm_rx_auth_req},
3715 {ALL_STATES, /* 4.3.2.5 */
3716 GSM48_MT_MM_AUTH_REJ, gsm48_mm_rx_auth_rej},
3718 {ALL_STATES, /* 4.3.3.2 */
3719 GSM48_MT_MM_ID_REQ, gsm48_mm_rx_id_req},
3721 {ALL_STATES, /* 4.3.5.2 */
3722 GSM48_MT_MM_ABORT, gsm48_mm_rx_abort},
3724 {ALL_STATES, /* 4.3.6.2 */
3725 GSM48_MT_MM_INFO, gsm48_mm_rx_info},
3727 {SBIT(GSM48_MM_ST_LOC_UPD_INIT), /* 4.4.4.6 */
3728 GSM48_MT_MM_LOC_UPD_ACCEPT, gsm48_mm_rx_loc_upd_acc},
3730 {SBIT(GSM48_MM_ST_LOC_UPD_INIT), /* 4.4.4.7 */
3731 GSM48_MT_MM_LOC_UPD_REJECT, gsm48_mm_rx_loc_upd_rej},
3733 {ALL_STATES, /* 4.5.1.1 */
3734 GSM48_MT_MM_CM_SERV_ACC, gsm48_mm_rx_cm_service_acc},
3736 {ALL_STATES, /* 4.5.1.1 */
3737 GSM48_MT_MM_CM_SERV_REJ, gsm48_mm_rx_cm_service_rej},
3740 #define MMDATASLLEN \
3741 (sizeof(mmdatastatelist) / sizeof(struct mmdatastate))
3743 static int gsm48_mm_data_ind(struct osmocom_ms *ms, struct msgb *msg)
3745 struct gsm48_mmlayer *mm = &ms->mmlayer;
3746 struct gsm48_hdr *gh = msgb_l3(msg);
3747 uint8_t pdisc = gh->proto_discr & 0x0f;
3748 uint8_t msg_type = gh->msg_type & 0xbf;
3749 struct gsm48_mmxx_hdr *mmh;
3750 int msg_supported = 0; /* determine, if message is supported at all */
3751 int rr_prim = -1, rr_est = -1; /* no prim set */
3756 if (msg_type == GSM48_MT_MM_NULL)
3759 if (mm->state == GSM48_MM_ST_IMSI_DETACH_INIT) {
3760 LOGP(DMM, LOGL_NOTICE, "DATA IND ignored during IMSI "
3764 /* pull the RR header */
3765 msgb_pull(msg, sizeof(struct gsm48_rr_hdr));
3767 /* create transaction (if not exists) and push message */
3769 case GSM48_PDISC_CC:
3770 rr_prim = GSM48_MMCC_DATA_IND;
3771 rr_est = GSM48_MMCC_EST_IND;
3774 case GSM48_PDISC_NC_SS:
3775 rr_prim = GSM48_MMSS_DATA_IND;
3776 rr_est = GSM48_MMSS_EST_IND;
3778 case GSM48_PDISC_SMS:
3779 rr_prim = GSM48_MMSMS_DATA_IND;
3780 rr_est = GSM48_MMSMS_EST_IND;
3784 if (rr_prim != -1) {
3785 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4;
3787 struct gsm48_mm_conn *conn;
3789 /* find transaction, if any */
3790 conn = mm_conn_by_id(mm, pdisc, transaction_id);
3792 /* create MM connection instance */
3794 conn = mm_conn_new(mm, pdisc, transaction_id,
3801 /* push new header */
3802 msgb_push(msg, sizeof(struct gsm48_mmxx_hdr));
3803 mmh = (struct gsm48_mmxx_hdr *)msg->data;
3804 mmh->msg_type = rr_prim;
3805 mmh->ref = conn->ref;
3807 /* go MM CONN ACTIVE state */
3808 if (mm->state == GSM48_MM_ST_WAIT_NETWORK_CMD
3809 || mm->state == GSM48_MM_ST_RR_CONN_RELEASE_NA) {
3810 /* stop RR release timer */
3813 /* stop "RR connection release not allowed" timer */
3816 new_mm_state(mm, GSM48_MM_ST_MM_CONN_ACTIVE, 0);
3820 /* forward message */
3822 case GSM48_PDISC_MM:
3823 skip_ind = (gh->proto_discr & 0xf0) >> 4;
3825 /* ignore if skip indicator is not B'0000' */
3828 break; /* follow the selection proceedure below */
3830 case GSM48_PDISC_CC:
3831 return gsm48_rcv_cc(ms, msg);
3834 case GSM48_PDISC_NC_SS:
3835 return gsm48_rcv_ss(ms, msg);
3837 case GSM48_PDISC_SMS:
3838 return gsm48_rcv_sms(ms, msg);
3842 LOGP(DMM, LOGL_NOTICE, "Protocol type 0x%02x unsupported.\n",
3845 return gsm48_mm_tx_mm_status(ms,
3846 GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED);
3849 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' in MM state %s\n", ms->name,
3850 get_mm_name(msg_type), gsm48_mm_state_names[mm->state]);
3852 stop_mm_t3212(mm); /* 4.4.2 */
3854 /* 11.2 re-start pending RR release timer */
3855 if (bsc_timer_pending(&mm->t3240)) {
3860 /* find function for current state and message */
3861 for (i = 0; i < MMDATASLLEN; i++) {
3862 if (msg_type == mmdatastatelist[i].type)
3864 if ((msg_type == mmdatastatelist[i].type)
3865 && ((1 << mm->state) & mmdatastatelist[i].states))
3868 if (i == MMDATASLLEN) {
3870 if (msg_supported) {
3871 LOGP(DMM, LOGL_NOTICE, "Message unhandled at this "
3873 return gsm48_mm_tx_mm_status(ms,
3874 GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE);
3876 LOGP(DMM, LOGL_NOTICE, "Message not supported.\n");
3877 return gsm48_mm_tx_mm_status(ms,
3878 GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED);
3882 rc = mmdatastatelist[i].rout(ms, msg);
3889 /* state trasitions for mobile management events */
3890 static struct eventstate {
3894 int (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3895 } eventstatelist[] = {
3896 /* 4.2.3 return to MM IDLE */
3897 {ALL_STATES - SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES,
3898 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_no_cell_found},
3900 {ALL_STATES - SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES,
3901 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_return_idle},
3903 /* 4.2.2.1 Normal service */
3904 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3905 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3907 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3908 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_loc_upd_normal}, /* change */
3910 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3911 GSM48_MM_EVENT_TIMEOUT_T3211, gsm48_mm_loc_upd},
3913 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3914 GSM48_MM_EVENT_TIMEOUT_T3213, gsm48_mm_loc_upd},
3916 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3917 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_periodic},
3919 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
3920 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_start},
3922 /* 4.2.2.2 Attempt to update / Loc. upd. needed */
3923 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE) |
3924 SBIT(GSM48_MM_SST_LOC_UPD_NEEDED),
3925 GSM48_MM_EVENT_USER_PLMN_SEL, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3927 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE) |
3928 SBIT(GSM48_MM_SST_LOC_UPD_NEEDED),
3929 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3931 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE) |
3932 SBIT(GSM48_MM_SST_LOC_UPD_NEEDED),
3933 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_loc_upd_normal}, /* change */
3935 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE),
3936 GSM48_MM_EVENT_TIMEOUT_T3211, gsm48_mm_loc_upd},
3938 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE),
3939 GSM48_MM_EVENT_TIMEOUT_T3213, gsm48_mm_loc_upd},
3941 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_ATTEMPT_UPDATE),
3942 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_periodic},
3944 /* 4.2.2.3 Limited service */
3945 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3946 GSM48_MM_EVENT_USER_PLMN_SEL, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3948 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3949 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_plmn_search}, /* 4.2.1.2 */
3951 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3952 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_loc_upd_normal}, /* if allow. */
3954 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_LIMITED_SERVICE),
3955 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_delay_per}, /* 4.4.2 */
3957 /* 4.2.2.4 No IMSI */
3958 /* 4.2.2.5 PLMN search, normal service */
3959 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3960 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_no_cell_found}, /* 4.2.1.1 */
3962 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3963 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_cell_selected}, /* 4.2.1.1 */
3965 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3966 GSM48_MM_EVENT_TIMEOUT_T3211, gsm48_mm_loc_upd_delay_retry},
3968 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3969 GSM48_MM_EVENT_TIMEOUT_T3213, gsm48_mm_loc_upd_delay_retry},
3971 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3972 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_delay_per}, /* 4.4.2 */
3974 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH_NORMAL),
3975 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_start},
3977 /* 4.2.2.6 PLMN search */
3978 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH),
3979 GSM48_MM_EVENT_NO_CELL_FOUND, gsm48_mm_no_cell_found}, /* 4.2.1.1 */
3981 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH),
3982 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_cell_selected}, /* 4.2.1.1 */
3984 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_PLMN_SEARCH),
3985 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_delay_per}, /* 4.4.2 */
3987 /* No cell available */
3988 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NO_CELL_AVAIL),
3989 GSM48_MM_EVENT_CELL_SELECTED, gsm48_mm_cell_selected}, /* 4.2.1.1 */
3991 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NO_CELL_AVAIL),
3992 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_delay_per}, /* 4.4.2 */
3994 /* IMSI detach in other cases */
3995 {SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES, /* silently detach */
3996 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_end},
3998 {SBIT(GSM48_MM_ST_WAIT_OUT_MM_CONN) |
3999 SBIT(GSM48_MM_ST_MM_CONN_ACTIVE) |
4000 SBIT(GSM48_MM_ST_PROCESS_CM_SERV_P) |
4001 SBIT(GSM48_MM_ST_WAIT_REEST) |
4002 SBIT(GSM48_MM_ST_WAIT_ADD_OUT_MM_CON) |
4003 SBIT(GSM48_MM_ST_MM_CONN_ACTIVE_VGCS) |
4004 SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD), ALL_STATES, /* we can release */
4005 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_release},
4007 {SBIT(GSM48_MM_ST_WAIT_RR_CONN_IMSI_D) |
4008 SBIT(GSM48_MM_ST_IMSI_DETACH_INIT) |
4009 SBIT(GSM48_MM_ST_IMSI_DETACH_PEND), ALL_STATES, /* ignore */
4010 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_ignore},
4012 {ALL_STATES, ALL_STATES,
4013 GSM48_MM_EVENT_IMSI_DETACH, gsm48_mm_imsi_detach_delay},
4015 {GSM48_MM_ST_IMSI_DETACH_INIT, ALL_STATES,
4016 GSM48_MM_EVENT_TIMEOUT_T3220, gsm48_mm_imsi_detach_end},
4018 /* location update in other cases */
4019 {ALL_STATES, ALL_STATES,
4020 GSM48_MM_EVENT_TIMEOUT_T3212, gsm48_mm_loc_upd_ignore},
4022 {ALL_STATES - SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES,
4023 GSM48_MM_EVENT_TIMEOUT_T3210, gsm48_mm_loc_upd_timeout},
4025 {ALL_STATES - SBIT(GSM48_MM_ST_MM_IDLE), ALL_STATES,
4026 GSM48_MM_EVENT_TIMEOUT_T3213, gsm48_mm_loc_upd_failed},
4027 /* 4.4.4.9 c) (but without retry) */
4030 {ALL_STATES, ALL_STATES,
4031 GSM48_MM_EVENT_SYSINFO, gsm48_mm_sysinfo},
4033 /* T3240 timed out */
4034 {SBIT(GSM48_MM_ST_WAIT_NETWORK_CMD) |
4035 SBIT(GSM48_MM_ST_LOC_UPD_REJ), ALL_STATES, /* 4.4.4.8 */
4036 GSM48_MM_EVENT_TIMEOUT_T3240, gsm48_mm_abort_rr},
4038 /* T3230 timed out */
4039 {SBIT(GSM48_MM_ST_MM_IDLE), SBIT(GSM48_MM_SST_NORMAL_SERVICE),
4040 GSM48_MM_EVENT_TIMEOUT_T3230, gsm48_mm_timeout_mm_con},
4042 /* SIM reports SRES */
4043 {ALL_STATES, ALL_STATES, /* 4.3.2.2 */
4044 GSM48_MM_EVENT_AUTH_RESPONSE, gsm48_mm_tx_auth_rsp},
4047 /* change in classmark is reported */
4048 {ALL_STATES, ALL_STATES,
4049 GSM48_MM_EVENT_CLASSMARK_CHG, gsm48_mm_classm_chg},
4053 #define EVENTSLLEN \
4054 (sizeof(eventstatelist) / sizeof(struct eventstate))
4056 static int gsm48_mm_ev(struct osmocom_ms *ms, int msg_type, struct msgb *msg)
4058 struct gsm48_mmlayer *mm = &ms->mmlayer;
4061 if (mm->state == GSM48_MM_ST_MM_IDLE)
4062 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' event in state "
4063 "MM IDLE, %s\n", ms->name, get_mmevent_name(msg_type),
4064 gsm48_mm_substate_names[mm->substate]);
4066 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' event in state "
4067 "%s\n", ms->name, get_mmevent_name(msg_type),
4068 gsm48_mm_state_names[mm->state]);
4070 /* Find function for current state and message */
4071 for (i = 0; i < EVENTSLLEN; i++)
4072 if ((msg_type == eventstatelist[i].type)
4073 && ((1 << mm->state) & eventstatelist[i].states)
4074 && ((1 << mm->substate) & eventstatelist[i].substates))
4076 if (i == EVENTSLLEN) {
4077 LOGP(DMM, LOGL_NOTICE, "Message unhandled at this state.\n");
4081 rc = eventstatelist[i].rout(ms, msg);
4087 * MM Register (SIM insert and remove)
4090 /* register new SIM card and trigger attach */
4091 static int gsm48_mmr_reg_req(struct osmocom_ms *ms)
4093 struct gsm48_mmlayer *mm = &ms->mmlayer;
4096 /* schedule insertion of SIM */
4097 nmsg = gsm322_msgb_alloc(GSM322_EVENT_SIM_INSERT);
4100 gsm322_plmn_sendmsg(ms, nmsg);
4102 /* 4.2.1.2 SIM is inserted in state NO IMSI */
4103 if (mm->state == GSM48_MM_ST_MM_IDLE
4104 && mm->substate == GSM48_MM_SST_NO_IMSI)
4105 new_mm_state(mm, GSM48_MM_ST_MM_IDLE,
4106 gsm48_mm_set_plmn_search(ms));
4111 /* trigger detach of sim card */
4112 static int gsm48_mmr_nreg_req(struct osmocom_ms *ms)
4114 struct gsm48_mmlayer *mm = &ms->mmlayer;
4117 nmsg = gsm322_msgb_alloc(GSM48_MM_EVENT_IMSI_DETACH);
4120 gsm48_mmevent_msg(mm->ms, nmsg);
4125 static int gsm48_rcv_mmr(struct osmocom_ms *ms, struct msgb *msg)
4127 struct gsm48_mmr *mmr = (struct gsm48_mmr *)msg->data;
4128 int msg_type = mmr->msg_type;
4131 LOGP(DMM, LOGL_INFO, "(ms %s) Received '%s' event\n", ms->name,
4132 get_mmr_name(msg_type));
4134 case GSM48_MMR_REG_REQ:
4135 rc = gsm48_mmr_reg_req(ms);
4137 case GSM48_MMR_NREG_REQ:
4138 rc = gsm48_mmr_nreg_req(ms);
4141 LOGP(DMM, LOGL_NOTICE, "Message unhandled.\n");