[layer23] Fixed and improved subscriber (SIM) dump of VTY
[osmocom-bb.git] / src / host / layer23 / src / mobile / subscriber.c
1 /*
2  * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
3  *
4  * All Rights Reserved
5  *
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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License 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.
19  *
20  */
21
22 #include <stdint.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <arpa/inet.h>
26 #include <osmocore/talloc.h>
27 #include <osmocore/comp128.h>
28
29 #include <osmocom/bb/common/logging.h>
30 #include <osmocom/bb/common/osmocom_data.h>
31 #include <osmocom/bb/common/networks.h>
32 #include <osmocom/bb/mobile/vty.h>
33
34 void *l23_ctx;
35
36 static void subscr_sim_query_cb(struct osmocom_ms *ms, struct msgb *msg);
37 static void subscr_sim_update_cb(struct osmocom_ms *ms, struct msgb *msg);
38 static void subscr_sim_key_cb(struct osmocom_ms *ms, struct msgb *msg);
39
40 /*
41  * support
42  */
43
44 char *gsm_check_imsi(const char *imsi)
45 {
46         int i;
47
48         if (!imsi || strlen(imsi) != 15)
49                 return "IMSI must have 15 digits!";
50
51         for (i = 0; i < strlen(imsi); i++) {
52                 if (imsi[i] < '0' || imsi[i] > '9')
53                         return "IMSI must have digits 0 to 9 only!";
54         }
55
56         return NULL;
57 }
58
59 static char *sim_decode_bcd(uint8_t *data, uint8_t length)
60 {
61         int i, j = 0;
62         static char result[32], c;
63
64         for (i = 0; i < (length << 1); i++) {
65                 if ((i & 1))
66                         c = (data[i >> 1] >> 4);
67                 else
68                         c = (data[i >> 1] & 0xf);
69                 if (c == 0xf)
70                         break;
71                 result[j++] = c + '0';
72                 if (j == sizeof(result) - 1)
73                         break;
74         }
75         result[j] = '\0';
76
77         return result;
78 }
79
80 static void xor96(uint8_t *ki, uint8_t *rand, uint8_t *sres, uint8_t *kc)
81 {
82         int i;
83
84         for (i=0; i < 4; i++)
85                 sres[i] = rand[i] ^ ki[i];
86         for (i=0; i < 8; i++)
87                 kc[i] = rand[i] ^ ki[i+4];
88 }
89
90 /*
91  * init/exit
92  */
93
94 int gsm_subscr_init(struct osmocom_ms *ms)
95 {
96         struct gsm_subscriber *subscr = &ms->subscr;
97
98         memset(subscr, 0, sizeof(*subscr));
99         subscr->ms = ms;
100
101         /* set TMSI / LAC invalid */
102         subscr->tmsi = 0xffffffff;
103         subscr->lac = 0x0000;
104
105         /* set key invalid */
106         subscr->key_seq = 7;
107
108         /* init lists */
109         INIT_LLIST_HEAD(&subscr->plmn_list);
110         INIT_LLIST_HEAD(&subscr->plmn_na);
111
112         /* open SIM */
113         subscr->sim_handle_query = sim_open(ms, subscr_sim_query_cb);
114         subscr->sim_handle_update = sim_open(ms, subscr_sim_update_cb);
115         subscr->sim_handle_key = sim_open(ms, subscr_sim_key_cb);
116
117         return 0;
118 }
119
120 int gsm_subscr_exit(struct osmocom_ms *ms)
121 {
122         struct gsm_subscriber *subscr = &ms->subscr;
123         struct llist_head *lh, *lh2;
124
125         if (subscr->sim_handle_query) {
126                 sim_close(ms, subscr->sim_handle_query);
127                 subscr->sim_handle_query = 0;
128         }
129         if (subscr->sim_handle_update) {
130                 sim_close(ms, subscr->sim_handle_update);
131                 subscr->sim_handle_update = 0;
132         }
133         if (subscr->sim_handle_key) {
134                 sim_close(ms, subscr->sim_handle_key);
135                 subscr->sim_handle_key = 0;
136         }
137
138         /* flush lists */
139         llist_for_each_safe(lh, lh2, &subscr->plmn_list) {
140                 llist_del(lh);
141                 talloc_free(lh);
142         }
143         llist_for_each_safe(lh, lh2, &subscr->plmn_na) {
144                 llist_del(lh);
145                 talloc_free(lh);
146         }
147
148         return 0;
149 }
150
151 /*
152  * test card
153  */
154
155 /* Attach test card, no SIM must be currently attached */
156 int gsm_subscr_testcard(struct osmocom_ms *ms, uint16_t mcc, uint16_t mnc)
157 {
158         struct gsm_settings *set = &ms->settings;
159         struct gsm_subscriber *subscr = &ms->subscr;
160         struct msgb *nmsg;
161         char *error;
162
163         if (subscr->sim_valid) {
164                 LOGP(DMM, LOGL_ERROR, "Cannot insert card, until current card "
165                         "is detached.\n");
166                 return -EBUSY;
167         }
168
169         error = gsm_check_imsi(set->test_imsi);
170         if (error) {
171                 LOGP(DMM, LOGL_ERROR, "%s\n", error);
172                 return -EINVAL;
173         }
174
175         /* reset subscriber */
176         gsm_subscr_exit(ms);
177         gsm_subscr_init(ms);
178
179         subscr->sim_type = GSM_SIM_TYPE_TEST;
180         sprintf(subscr->sim_name, "test");
181         subscr->sim_valid = 1;
182         subscr->ustate = GSM_SIM_U2_NOT_UPDATED;
183         subscr->acc_barr = set->test_barr; /* we may access barred cell */
184         subscr->acc_class = 0xffff; /* we have any access class */
185         subscr->plmn_valid = set->test_rplmn_valid;
186         subscr->plmn_mcc = mcc;
187         subscr->plmn_mnc = mnc;
188         subscr->always_search_hplmn = set->test_always;
189         subscr->t6m_hplmn = 1; /* try to find home network every 6 min */
190         strcpy(subscr->imsi, set->test_imsi);
191
192         LOGP(DMM, LOGL_INFO, "(ms %s) Inserting test card (IMSI=%s %s, %s)\n",
193                 ms->name, subscr->imsi, gsm_imsi_mcc(subscr->imsi),
194                 gsm_imsi_mnc(subscr->imsi));
195
196         if (subscr->plmn_valid)
197                 LOGP(DMM, LOGL_INFO, "-> Test card registered to %s %s "
198                         "(%s, %s)\n", gsm_print_mcc(mcc), gsm_print_mnc(mnc),
199                         gsm_get_mcc(mcc), gsm_get_mnc(mcc, mnc));
200         else
201                 LOGP(DMM, LOGL_INFO, "-> Test card not registered\n");
202
203         /* insert card */
204         nmsg = gsm48_mmr_msgb_alloc(GSM48_MMR_REG_REQ);
205         if (!nmsg)
206                 return -ENOMEM;
207         gsm48_mmr_downmsg(ms, nmsg);
208
209         return 0;
210 }
211
212 /*
213  * sim card
214  */
215
216 static int subscr_sim_iccid(struct osmocom_ms *ms, uint8_t *data,
217         uint8_t length)
218 {
219         struct gsm_subscriber *subscr = &ms->subscr;
220
221         strcpy(subscr->iccid, sim_decode_bcd(data, length));
222         sprintf(subscr->sim_name, "sim-%s", subscr->iccid);
223         LOGP(DMM, LOGL_INFO, "received ICCID %s from SIM\n", subscr->iccid);
224
225         return 0;
226 }
227
228 static int subscr_sim_imsi(struct osmocom_ms *ms, uint8_t *data,
229         uint8_t length)
230 {
231         struct gsm_subscriber *subscr = &ms->subscr;
232         char *imsi;
233
234         /* get actual length */
235         if (length < 1)
236                 return -EINVAL;
237         if (data[0] + 1 < length) {
238                 LOGP(DMM, LOGL_NOTICE, "invalid length = %d\n", length);
239                 return -EINVAL;
240         }
241         length = data[0];
242
243         /* decode IMSI, skip first digit (parity) */
244         imsi = sim_decode_bcd(data + 1, length);
245         if (strlen(imsi) - 1 > GSM_IMSI_LENGTH - 1 || strlen(imsi) - 1 < 6) {
246                 LOGP(DMM, LOGL_NOTICE, "IMSI invalid length = %d\n",
247                         strlen(imsi) - 1);
248                 return -EINVAL;
249         }
250
251         strncpy(subscr->imsi, imsi + 1, sizeof(subscr->imsi) - 1);
252
253         LOGP(DMM, LOGL_INFO, "received IMSI %s from SIM\n", subscr->imsi);
254
255         return 0;
256 }
257
258 static int subscr_sim_loci(struct osmocom_ms *ms, uint8_t *data,
259         uint8_t length)
260 {
261         struct gsm_subscriber *subscr = &ms->subscr;
262         struct gsm1111_ef_loci *loci;
263
264         if (length < 11)
265                 return -EINVAL;
266         loci = (struct gsm1111_ef_loci *) data;
267
268         /* TMSI */
269         subscr->tmsi = ntohl(loci->tmsi);
270
271         /* LAI */
272         gsm48_decode_lai(&loci->lai, &subscr->mcc, &subscr->mnc, &subscr->lac);
273
274         /* location update status */
275         switch (loci->lupd_status & 0x07) {
276         case 0x00:
277                 subscr->ustate = GSM_SIM_U1_UPDATED;
278                 break;
279         case 0x02:
280         case 0x03:
281                 subscr->ustate = GSM_SIM_U3_ROAMING_NA;
282                 break;
283         default:
284                 subscr->ustate = GSM_SIM_U2_NOT_UPDATED;
285         }
286
287         LOGP(DMM, LOGL_INFO, "received LOCI from SIM (mcc=%s mnc=%s lac=0x%04x "
288                 "U%d)\n", gsm_print_mcc(subscr->mcc),
289                 gsm_print_mnc(subscr->mnc), subscr->lac, subscr->ustate);
290
291         return 0;
292 }
293
294 static int subscr_sim_msisdn(struct osmocom_ms *ms, uint8_t *data,
295         uint8_t length)
296 {
297         struct gsm_subscriber *subscr = &ms->subscr;
298         struct gsm1111_ef_adn *adn;
299
300         if (length < sizeof(*adn))
301                 return -EINVAL;
302         adn = (struct gsm1111_ef_adn *) (data + length - sizeof(*adn));
303
304         /* empty */
305         subscr->msisdn[0] = '\0';
306         if (adn->len_bcd <= 1)
307                 return 0;
308
309         /* number */
310         if (adn->ton_npi == 1)
311                 strcpy(subscr->msisdn, "+");
312         if (adn->ton_npi == 2)
313                 strcpy(subscr->msisdn, "0");
314         strncat(subscr->msisdn, sim_decode_bcd(adn->number, adn->len_bcd - 1),
315                 sizeof(subscr->msisdn) - 2);
316
317         LOGP(DMM, LOGL_INFO, "received MSISDN %s from SIM\n", subscr->msisdn);
318
319         return 0;
320 }
321
322 static int subscr_sim_kc(struct osmocom_ms *ms, uint8_t *data,
323         uint8_t length)
324 {
325         struct gsm_subscriber *subscr = &ms->subscr;
326
327         if (length < 9)
328                 return -EINVAL;
329
330         /* key */
331         memcpy(subscr->key, data, 8);
332
333         /* key sequence */
334         subscr->key_seq = data[8] & 0x07;
335
336         LOGP(DMM, LOGL_INFO, "received KEY from SIM\n");
337
338         return 0;
339 }
340
341 static int subscr_sim_plmnsel(struct osmocom_ms *ms, uint8_t *data,
342         uint8_t length)
343 {
344         struct gsm_subscriber *subscr = &ms->subscr;
345         struct gsm_sub_plmn_list *plmn;
346         struct llist_head *lh, *lh2;
347         uint8_t lai[5];
348         uint16_t dummy_lac;
349
350         /* flush list */
351         llist_for_each_safe(lh, lh2, &subscr->plmn_list) {
352                 llist_del(lh);
353                 talloc_free(lh);
354         }
355
356         while(length >= 3) {
357                 /* end of list inside mandatory fields */
358                 if (data[0] == 0xff && data[1] == 0xff && data[2] == 0x0ff)
359                         break;
360
361                 /* add to list */
362                 plmn = talloc_zero(l23_ctx, struct gsm_sub_plmn_list);
363                 if (!plmn)
364                         return -ENOMEM;
365                 lai[0] = data[0];
366                 lai[1] = data[1];
367                 lai[2] = data[2];
368                 gsm48_decode_lai((struct gsm48_loc_area_id *)lai, &plmn->mcc,
369                         &plmn->mnc, &dummy_lac);
370                 llist_add_tail(&plmn->entry, &subscr->plmn_list);
371
372                 LOGP(DMM, LOGL_INFO, "received PLMN selector (mcc=%s mnc=%s) "
373                         "from SIM\n",
374                         gsm_print_mcc(plmn->mcc), gsm_print_mnc(plmn->mnc));
375
376                 data += 3;
377                 length -= 3;
378         }
379
380         return 0;
381 }
382
383 static int subscr_sim_hpplmn(struct osmocom_ms *ms, uint8_t *data,
384         uint8_t length)
385 {
386         struct gsm_subscriber *subscr = &ms->subscr;
387
388         if (length < 1)
389                 return -EINVAL;
390
391         /* HPLMN search interval */
392         subscr->t6m_hplmn = *data; /* multiple of 6 minutes */
393
394         LOGP(DMM, LOGL_INFO, "received HPPLMN %d (%d mins) from SIM\n",
395                 subscr->t6m_hplmn, subscr->t6m_hplmn * 6);
396
397         return 0;
398 }
399
400 static int subscr_sim_spn(struct osmocom_ms *ms, uint8_t *data,
401         uint8_t length)
402 {
403         struct gsm_subscriber *subscr = &ms->subscr;
404         int i;
405
406         /* UCS2 code not supported */
407         if (length < 17 || data[1] >= 0x80)
408                 return -ENOTSUP;
409
410         data++;
411         for (i = 0; i < 16; i++) {
412                 if (*data == 0xff)
413                         break;
414                 subscr->sim_spn[i] = *data++;
415         }
416         subscr->sim_spn[i] = '\0';
417
418         LOGP(DMM, LOGL_INFO, "received SPN %s from SIM\n", subscr->sim_spn);
419
420         return 0;
421 }
422
423 static int subscr_sim_acc(struct osmocom_ms *ms, uint8_t *data,
424         uint8_t length)
425 {
426         struct gsm_subscriber *subscr = &ms->subscr;
427         uint16_t ac;
428
429         if (length < 2)
430                 return -EINVAL;
431
432         /* cell access */
433         memcpy(&ac, data, sizeof(ac));
434         subscr->acc_class = ntohs(ac);
435
436         LOGP(DMM, LOGL_INFO, "received ACC %04x from SIM\n", subscr->acc_class);
437
438         return 0;
439 }
440
441 static int subscr_sim_fplmn(struct osmocom_ms *ms, uint8_t *data,
442         uint8_t length)
443 {
444         struct gsm_subscriber *subscr = &ms->subscr;
445         struct gsm_sub_plmn_na *na;
446         struct llist_head *lh, *lh2;
447         uint8_t lai[5];
448         uint16_t dummy_lac;
449
450         /* flush list */
451         llist_for_each_safe(lh, lh2, &subscr->plmn_na) {
452                 llist_del(lh);
453                 talloc_free(lh);
454         }
455
456         while (length >= 3) {
457                 /* end of list inside mandatory fields */
458                 if (data[0] == 0xff && data[1] == 0xff && data[2] == 0x0ff)
459                         break;
460
461                 /* add to list */
462                 na = talloc_zero(l23_ctx, struct gsm_sub_plmn_na);
463                 if (!na)
464                         return -ENOMEM;
465                 lai[0] = data[0];
466                 lai[1] = data[1];
467                 lai[2] = data[2];
468                 gsm48_decode_lai((struct gsm48_loc_area_id *)lai, &na->mcc,
469                         &na->mnc, &dummy_lac);
470                 na->cause = 0;
471                 llist_add_tail(&na->entry, &subscr->plmn_na);
472
473                 data += 3;
474                 length -= 3;
475         }
476         return 0;
477 }
478
479 static struct subscr_sim_file {
480         uint8_t         mandatory;
481         uint16_t        path[MAX_SIM_PATH_LENGTH];
482         uint16_t        file;
483         int             (*func)(struct osmocom_ms *ms, uint8_t *data,
484                                 uint8_t length);
485 } subscr_sim_files[] = {
486         { 1, { 0 },         0x2fe2, subscr_sim_iccid },
487         { 1, { 0x7f20, 0 }, 0x6f07, subscr_sim_imsi },
488         { 1, { 0x7f20, 0 }, 0x6f7e, subscr_sim_loci },
489         { 0, { 0x7f20, 0 }, 0x6f40, subscr_sim_msisdn },
490         { 0, { 0x7f20, 0 }, 0x6f20, subscr_sim_kc },
491         { 0, { 0x7f20, 0 }, 0x6f30, subscr_sim_plmnsel },
492         { 0, { 0x7f20, 0 }, 0x6f31, subscr_sim_hpplmn },
493         { 0, { 0x7f20, 0 }, 0x6f46, subscr_sim_spn },
494         { 0, { 0x7f20, 0 }, 0x6f78, subscr_sim_acc },
495         { 0, { 0x7f20, 0 }, 0x6f7b, subscr_sim_fplmn },
496         { 0, { 0 },         0,      NULL }
497 };
498
499 /* request file from SIM */
500 static int subscr_sim_request(struct osmocom_ms *ms)
501 {
502         struct gsm_subscriber *subscr = &ms->subscr;
503         struct subscr_sim_file *sf = &subscr_sim_files[subscr->sim_file_index];
504         struct msgb *nmsg;
505         struct sim_hdr *nsh;
506         int i;
507
508         /* we are done, fire up PLMN and cell selection process */
509         if (!sf->func) {
510                 LOGP(DMM, LOGL_INFO, "(ms %s) Done reading SIM card "
511                         "(IMSI=%s %s, %s)\n", ms->name, subscr->imsi,
512                         gsm_imsi_mcc(subscr->imsi), gsm_imsi_mnc(subscr->imsi));
513
514                 /* if LAI is valid, set RPLMN */
515                 if (subscr->lac > 0x0000 && subscr->lac < 0xfffe) {
516                         subscr->plmn_valid = 1;
517                         subscr->plmn_mcc = subscr->mcc;
518                         subscr->plmn_mnc = subscr->mnc;
519                         LOGP(DMM, LOGL_INFO, "-> SIM card registered to %s %s "
520                                 "(%s, %s)\n", gsm_print_mcc(subscr->plmn_mcc),
521                                 gsm_print_mnc(subscr->plmn_mnc),
522                                 gsm_get_mcc(subscr->plmn_mcc),
523                                 gsm_get_mnc(subscr->plmn_mcc,
524                                         subscr->plmn_mnc));
525                 } else
526                         LOGP(DMM, LOGL_INFO, "-> SIM card not registered\n");
527
528                 /* insert card */
529                 nmsg = gsm48_mmr_msgb_alloc(GSM48_MMR_REG_REQ);
530                 if (!nmsg)
531                         return -ENOMEM;
532                 gsm48_mmr_downmsg(ms, nmsg);
533
534                 return 0;
535         }
536
537         /* trigger SIM reading */
538         nmsg = gsm_sim_msgb_alloc(subscr->sim_handle_query,
539                 SIM_JOB_READ_BINARY);
540         if (!nmsg)
541                 return -ENOMEM;
542         nsh = (struct sim_hdr *) nmsg->data;
543         i = 0;
544         while (sf->path[i]) {
545                 nsh->path[i] = sf->path[i];
546                 i++;
547         }
548         nsh->path[i] = 0; /* end of path */
549         nsh->file = sf->file;
550         LOGP(DMM, LOGL_INFO, "Requesting SIM file 0x%04x\n", nsh->file);
551         sim_job(ms, nmsg);
552
553         return 0;
554 }
555
556 static void subscr_sim_query_cb(struct osmocom_ms *ms, struct msgb *msg)
557 {
558         struct gsm_subscriber *subscr = &ms->subscr;
559         struct sim_hdr *sh = (struct sim_hdr *) msg->data;
560         uint8_t *payload = msg->data + sizeof(*sh);
561         uint16_t payload_len = msg->len - sizeof(*sh);
562         int rc;
563         struct subscr_sim_file *sf = &subscr_sim_files[subscr->sim_file_index];
564
565         /* error handling */
566         if (sh->job_type == SIM_JOB_ERROR) {
567                 uint8_t cause = payload[0];
568
569                 switch (cause) {
570                         /* unlocking required */
571                 case SIM_CAUSE_PIN1_REQUIRED:
572                         LOGP(DMM, LOGL_INFO, "PIN is required, %d tries left\n",
573                                 payload[1]);
574
575                         vty_notify(ms, NULL);
576                         vty_notify(ms, "Please give PIN for ICCID %s (you have "
577                                 "%d tries left)\n", subscr->iccid, payload[1]);
578                         subscr->sim_pin_required = 1;
579                         break;
580                 case SIM_CAUSE_PIN1_BLOCKED:
581                         LOGP(DMM, LOGL_NOTICE, "PIN is blocked\n");
582
583                         vty_notify(ms, NULL);
584                         vty_notify(ms, "PIN is blocked\n");
585                         if (payload[1]) {
586                                 vty_notify(ms, "Please give PUC for ICCID %s "
587                                         "(you have %d tries left)\n",
588                                         subscr->iccid, payload[1]);
589                         }
590                         subscr->sim_pin_required = 1;
591                         break;
592                 case SIM_CAUSE_PUC_BLOCKED:
593                         LOGP(DMM, LOGL_NOTICE, "PUC is blocked\n");
594
595                         vty_notify(ms, NULL);
596                         vty_notify(ms, "PUC is blocked\n");
597                         subscr->sim_pin_required = 1;
598                         break;
599                 default:
600                         if (sf->func && !sf->mandatory) {
601                                 LOGP(DMM, LOGL_NOTICE, "SIM reading failed, "
602                                         "ignoring!\n");
603                                 goto ignore;
604                         }
605                         LOGP(DMM, LOGL_NOTICE, "SIM reading failed\n");
606
607                         vty_notify(ms, NULL);
608                         vty_notify(ms, "SIM failed, replace SIM!\n");
609                 }
610                 msgb_free(msg);
611
612                 return;
613         }
614
615         /* if pin was successfully unlocked, then resend request */
616         if (subscr->sim_pin_required) {
617                 subscr->sim_pin_required = 0;
618                 subscr_sim_request(ms);
619                 return;
620         }
621
622         /* done when nothing more to read. this happens on PIN requests */
623         if (!sf->func)
624                 return;
625
626         /* call function do decode SIM reply */
627         rc = sf->func(ms, payload, payload_len);
628         if (rc) {
629                 LOGP(DMM, LOGL_NOTICE, "SIM reading failed, file invalid\n");
630                 if (subscr_sim_files[subscr->sim_file_index].mandatory) {
631                         vty_notify(ms, NULL);
632                         vty_notify(ms, "SIM failed, data invalid, replace "
633                                 "SIM!\n");
634                         msgb_free(msg);
635
636                         return;
637                 }
638         }
639
640 ignore:
641         msgb_free(msg);
642
643         /* trigger next file */
644         subscr->sim_file_index++;
645         subscr_sim_request(ms);
646 }
647
648 /* enter PIN */
649 void gsm_subscr_sim_pin(struct osmocom_ms *ms, char *pin1, char *pin2,
650         int8_t mode)
651 {
652         struct gsm_subscriber *subscr = &ms->subscr;
653         struct msgb *nmsg;
654         uint8_t job;
655
656         switch (mode) {
657         case -1:
658                 job = SIM_JOB_PIN1_DISABLE;
659                 LOGP(DMM, LOGL_INFO, "disabling PIN %s\n", pin1);
660                 break;
661         case 1:
662                 job = SIM_JOB_PIN1_ENABLE;
663                 LOGP(DMM, LOGL_INFO, "enabling PIN %s\n", pin1);
664                 break;
665         case 2:
666                 job = SIM_JOB_PIN1_CHANGE;
667                 LOGP(DMM, LOGL_INFO, "changing PIN %s to %s\n", pin1, pin2);
668                 break;
669         case 99:
670                 job = SIM_JOB_PIN1_UNBLOCK;
671                 LOGP(DMM, LOGL_INFO, "unblocking PIN %s with PUC %s\n", pin1,
672                         pin2);
673                 break;
674         default:
675                 if (!subscr->sim_pin_required) {
676                         LOGP(DMM, LOGL_ERROR, "No PIN required now\n");
677                         return;
678                 }
679                 LOGP(DMM, LOGL_INFO, "entering PIN %s\n", pin1);
680                 job = SIM_JOB_PIN1_UNLOCK;
681         }
682
683         nmsg = gsm_sim_msgb_alloc(subscr->sim_handle_query, job);
684         if (!nmsg)
685                 return;
686         memcpy(msgb_put(nmsg, strlen(pin1) + 1), pin1, strlen(pin1) + 1);
687         memcpy(msgb_put(nmsg, strlen(pin2) + 1), pin2, strlen(pin2) + 1);
688         sim_job(ms, nmsg);
689 }
690
691 /* Attach SIM reader, no SIM must be currently attached */
692 int gsm_subscr_simcard(struct osmocom_ms *ms)
693 {
694         struct gsm_subscriber *subscr = &ms->subscr;
695
696         if (subscr->sim_valid) {
697                 LOGP(DMM, LOGL_ERROR, "Cannot attach card, until current card "
698                         "is detached.\n");
699                 return -EBUSY;
700         }
701
702         /* reset subscriber */
703         gsm_subscr_exit(ms);
704         gsm_subscr_init(ms);
705
706         subscr->sim_type = GSM_SIM_TYPE_READER;
707         sprintf(subscr->sim_name, "sim");
708         subscr->sim_valid = 1;
709         subscr->ustate = GSM_SIM_U2_NOT_UPDATED;
710
711         /* start with first index */
712         subscr->sim_file_index = 0;
713         return subscr_sim_request(ms);
714 }
715
716 /* update plmn not allowed list on SIM */
717 static int subscr_write_plmn_na(struct osmocom_ms *ms)
718 {
719         struct gsm_subscriber *subscr = &ms->subscr;
720         struct msgb *nmsg;
721         struct sim_hdr *nsh;
722         struct gsm_sub_plmn_na *na, *nas[4] = { NULL, NULL, NULL, NULL };
723         int count = 0, i;
724         uint8_t *data;
725         uint8_t lai[5];
726
727         /* skip, if no real valid SIM */
728         if (subscr->sim_type != GSM_SIM_TYPE_READER || !subscr->sim_valid)
729                 return 0;
730
731         /* get tail list from "PLMN not allowed" */
732         llist_for_each_entry(na, &subscr->plmn_na, entry) {
733                 if (count < 4)
734                         nas[count] = na;
735                 else {
736                         nas[0] = nas[1];
737                         nas[1] = nas[2];
738                         nas[2] = nas[3];
739                         nas[3] = na;
740                 }
741                 count++;
742         }
743
744         /* write to SIM */
745         LOGP(DMM, LOGL_INFO, "Updating FPLMN on SIM\n");
746         nmsg = gsm_sim_msgb_alloc(subscr->sim_handle_update,
747                 SIM_JOB_UPDATE_BINARY);
748         if (!nmsg)
749                 return -ENOMEM;
750         nsh = (struct sim_hdr *) nmsg->data;
751         data = msgb_put(nmsg, 12);
752         nsh->path[0] = 0x7f20;
753         nsh->path[1] = 0;
754         nsh->file = 0x6f7b;
755         for (i = 0; i < 4; i++) {
756                 if (nas[i]) {
757                         gsm48_encode_lai((struct gsm48_loc_area_id *)lai,
758                         nas[i]->mcc, nas[i]->mnc, 0);
759                         *data++ = lai[0];
760                         *data++ = lai[1];
761                         *data++ = lai[2];
762                 } else {
763                         *data++ = 0xff;
764                         *data++ = 0xff;
765                         *data++ = 0xff;
766                 }
767         }
768         sim_job(ms, nmsg);
769
770         return 0;
771 }
772
773 /* update LOCI on SIM */
774 int gsm_subscr_write_loci(struct osmocom_ms *ms)
775 {
776         struct gsm_subscriber *subscr = &ms->subscr;
777         struct msgb *nmsg;
778         struct sim_hdr *nsh;
779         struct gsm1111_ef_loci *loci;
780
781         /* skip, if no real valid SIM */
782         if (subscr->sim_type != GSM_SIM_TYPE_READER || !subscr->sim_valid)
783                 return 0;
784
785         LOGP(DMM, LOGL_INFO, "Updating LOCI on SIM\n");
786
787         /* write to SIM */
788         nmsg = gsm_sim_msgb_alloc(subscr->sim_handle_update,
789                 SIM_JOB_UPDATE_BINARY);
790         if (!nmsg)
791                 return -ENOMEM;
792         nsh = (struct sim_hdr *) nmsg->data;
793         nsh->path[0] = 0x7f20;
794         nsh->path[1] = 0;
795         nsh->file = 0x6f7e;
796         loci = (struct gsm1111_ef_loci *)msgb_put(nmsg, sizeof(*loci));
797
798         /* TMSI */
799         loci->tmsi = htonl(subscr->tmsi);
800
801         /* LAI */
802         gsm48_encode_lai(&loci->lai, subscr->mcc, subscr->mnc, subscr->lac);
803
804         /* TMSI time */
805         loci->tmsi_time = 0xff;
806
807         /* location update status */
808         switch (subscr->ustate) {
809         case GSM_SIM_U1_UPDATED:
810                 loci->lupd_status = 0x00;
811                 break;
812         case GSM_SIM_U3_ROAMING_NA:
813                 loci->lupd_status = 0x03;
814                 break;
815         default:
816                 loci->lupd_status = 0x01;
817         }
818
819         sim_job(ms, nmsg);
820
821         return 0;
822 }
823
824 static void subscr_sim_update_cb(struct osmocom_ms *ms, struct msgb *msg)
825 {
826         struct sim_hdr *sh = (struct sim_hdr *) msg->data;
827         uint8_t *payload = msg->data + sizeof(*sh);
828
829         /* error handling */
830         if (sh->job_type == SIM_JOB_ERROR)
831                 LOGP(DMM, LOGL_NOTICE, "SIM update failed (cause %d)\n",
832                         *payload);
833
834         msgb_free(msg);
835 }
836
837 int gsm_subscr_generate_kc(struct osmocom_ms *ms, uint8_t key_seq,
838         uint8_t *rand, uint8_t no_sim)
839 {
840         struct gsm_subscriber *subscr = &ms->subscr;
841         struct msgb *nmsg;
842         struct sim_hdr *nsh;
843
844         /* not a SIM */
845         if ((subscr->sim_type != GSM_SIM_TYPE_READER
846           && subscr->sim_type != GSM_SIM_TYPE_TEST)
847          || !subscr->sim_valid || no_sim) {
848                 struct gsm48_mm_event *nmme;
849
850                 LOGP(DMM, LOGL_INFO, "Sending dummy authentication response\n");
851                 nmsg = gsm48_mmevent_msgb_alloc(GSM48_MM_EVENT_AUTH_RESPONSE);
852                 if (!nmsg)
853                         return -ENOMEM;
854                 nmme = (struct gsm48_mm_event *) nmsg->data;
855                 nmme->sres[0] = 0x12;
856                 nmme->sres[1] = 0x34;
857                 nmme->sres[2] = 0x56;
858                 nmme->sres[3] = 0x78;
859                 gsm48_mmevent_msg(ms, nmsg);
860
861                 return 0;
862         }
863
864         /* test SIM */
865         if (subscr->sim_type == GSM_SIM_TYPE_TEST) {
866                 struct gsm48_mm_event *nmme;
867                 uint8_t sres[4];
868                 struct gsm_settings *set = &ms->settings;
869
870                 if (set->test_ki_type == GSM_SIM_KEY_COMP128)
871                         comp128(set->test_ki, rand, sres, subscr->key);
872                 else
873                         xor96(set->test_ki, rand, sres, subscr->key);
874                 /* store sequence */
875                 subscr->key_seq = key_seq;
876
877                 LOGP(DMM, LOGL_INFO, "Sending authentication response\n");
878                 nmsg = gsm48_mmevent_msgb_alloc(GSM48_MM_EVENT_AUTH_RESPONSE);
879                 if (!nmsg)
880                         return -ENOMEM;
881                 nmme = (struct gsm48_mm_event *) nmsg->data;
882                 memcpy(nmme->sres, sres, 4);
883                 gsm48_mmevent_msg(ms, nmsg);
884
885                 return 0;
886         }
887
888         LOGP(DMM, LOGL_INFO, "Generating KEY at SIM\n");
889
890         /* command to SIM */
891         nmsg = gsm_sim_msgb_alloc(subscr->sim_handle_key, SIM_JOB_RUN_GSM_ALGO);
892         if (!nmsg)
893                 return -ENOMEM;
894         nsh = (struct sim_hdr *) nmsg->data;
895         nsh->path[0] = 0x7f20;
896         nsh->path[1] = 0;
897
898         /* random */
899         memcpy(msgb_put(nmsg, 16), rand, 16);
900
901         /* store sequence */
902         subscr->key_seq = key_seq;
903
904         sim_job(ms, nmsg);
905
906         return 0;
907 }
908
909 static void subscr_sim_key_cb(struct osmocom_ms *ms, struct msgb *msg)
910 {
911         struct gsm_subscriber *subscr = &ms->subscr;
912         struct sim_hdr *sh = (struct sim_hdr *) msg->data;
913         uint8_t *payload = msg->data + sizeof(*sh);
914         uint16_t payload_len = msg->len - sizeof(*sh);
915         struct msgb *nmsg;
916         struct sim_hdr *nsh;
917         struct gsm48_mm_event *nmme;
918         uint8_t *data;
919
920         /* error handling */
921         if (sh->job_type == SIM_JOB_ERROR) {
922                 LOGP(DMM, LOGL_NOTICE, "key generation on SIM failed "
923                         "(cause %d)\n", *payload);
924
925                 msgb_free(msg);
926
927                 return;
928         }
929
930         if (payload_len < 12) {
931                 LOGP(DMM, LOGL_NOTICE, "response from SIM too short\n");
932                 return;
933         }
934
935         /* store key */
936         memcpy(subscr->key, payload + 4, 8);
937
938         /* write to SIM */
939         LOGP(DMM, LOGL_INFO, "Updating KC on SIM\n");
940         nmsg = gsm_sim_msgb_alloc(subscr->sim_handle_update,
941                 SIM_JOB_UPDATE_BINARY);
942         if (!nmsg)
943                 return;
944         nsh = (struct sim_hdr *) nmsg->data;
945         nsh->path[0] = 0x7f20;
946         nsh->path[1] = 0;
947         nsh->file = 0x6f20;
948         data = msgb_put(nmsg, 9);
949         memcpy(data, subscr->key, 8);
950         data[8] = subscr->key_seq;
951         sim_job(ms, nmsg);
952
953         /* return signed response */
954         nmsg = gsm48_mmevent_msgb_alloc(GSM48_MM_EVENT_AUTH_RESPONSE);
955         if (!nmsg)
956                 return;
957         nmme = (struct gsm48_mm_event *) nmsg->data;
958         memcpy(nmme->sres, payload, 4);
959         gsm48_mmevent_msg(ms, nmsg);
960
961         msgb_free(msg);
962 }
963
964 /*
965  * detach
966  */
967
968 /* Detach card */
969 int gsm_subscr_remove(struct osmocom_ms *ms)
970 {
971         struct gsm_subscriber *subscr = &ms->subscr;
972         struct msgb *nmsg;
973
974         if (!subscr->sim_valid) {
975                 LOGP(DMM, LOGL_ERROR, "Cannot remove card, no card present\n");
976                 return -EINVAL;
977         }
978
979         /* remove card */
980         nmsg = gsm48_mmr_msgb_alloc(GSM48_MMR_NREG_REQ);
981         if (!nmsg)
982                 return -ENOMEM;
983         gsm48_mmr_downmsg(ms, nmsg);
984
985         return 0;
986 }
987
988 /*
989  * state and lists
990  */
991
992 static const char *subscr_ustate_names[] = {
993         "U0_NULL",
994         "U1_UPDATED",
995         "U2_NOT_UPDATED",
996         "U3_ROAMING_NA"
997 };
998
999 /* change to new U state */
1000 void new_sim_ustate(struct gsm_subscriber *subscr, int state)
1001 {
1002         LOGP(DMM, LOGL_INFO, "(ms %s) new state %s -> %s\n", subscr->ms->name,
1003                 subscr_ustate_names[subscr->ustate],
1004                 subscr_ustate_names[state]);
1005
1006         subscr->ustate = state;
1007 }
1008
1009 /* del forbidden PLMN */
1010 int gsm_subscr_del_forbidden_plmn(struct gsm_subscriber *subscr, uint16_t mcc,
1011         uint16_t mnc)
1012 {
1013         struct gsm_sub_plmn_na *na;
1014
1015         llist_for_each_entry(na, &subscr->plmn_na, entry) {
1016                 if (na->mcc == mcc && na->mnc == mnc) {
1017                         LOGP(DPLMN, LOGL_INFO, "Delete from list of forbidden "
1018                                 "PLMNs (mcc=%s, mnc=%s)\n",
1019                                 gsm_print_mcc(mcc), gsm_print_mnc(mnc));
1020                         llist_del(&na->entry);
1021                         talloc_free(na);
1022                         /* update plmn not allowed list on SIM */
1023                         subscr_write_plmn_na(subscr->ms);
1024                         return 0;
1025                 }
1026         }
1027
1028         return -EINVAL;
1029 }
1030
1031 /* add forbidden PLMN */
1032 int gsm_subscr_add_forbidden_plmn(struct gsm_subscriber *subscr, uint16_t mcc,
1033                                         uint16_t mnc, uint8_t cause)
1034 {
1035         struct gsm_sub_plmn_na *na;
1036
1037         /* if already in the list, remove and add to tail */
1038         gsm_subscr_del_forbidden_plmn(subscr, mcc, mnc);
1039
1040         LOGP(DPLMN, LOGL_INFO, "Add to list of forbidden PLMNs "
1041                 "(mcc=%s, mnc=%s)\n", gsm_print_mcc(mcc), gsm_print_mnc(mnc));
1042         na = talloc_zero(l23_ctx, struct gsm_sub_plmn_na);
1043         if (!na)
1044                 return -ENOMEM;
1045         na->mcc = mcc;
1046         na->mnc = mnc;
1047         na->cause = cause;
1048         llist_add_tail(&na->entry, &subscr->plmn_na);
1049
1050         /* don't add Home PLMN to SIM */
1051         if (subscr->sim_valid && gsm_match_mnc(mcc, mnc, subscr->imsi))
1052                 return -EINVAL;
1053
1054         /* update plmn not allowed list on SIM */
1055         subscr_write_plmn_na(subscr->ms);
1056
1057         return 0;
1058 }
1059
1060 /* search forbidden PLMN */
1061 int gsm_subscr_is_forbidden_plmn(struct gsm_subscriber *subscr, uint16_t mcc,
1062                                         uint16_t mnc)
1063 {
1064         struct gsm_sub_plmn_na *na;
1065
1066         llist_for_each_entry(na, &subscr->plmn_na, entry) {
1067                 if (na->mcc == mcc && na->mnc == mnc)
1068                         return 1;
1069         }
1070
1071         return 0;
1072 }
1073
1074 int gsm_subscr_dump_forbidden_plmn(struct osmocom_ms *ms,
1075                         void (*print)(void *, const char *, ...), void *priv)
1076 {
1077         struct gsm_subscriber *subscr = &ms->subscr;
1078         struct gsm_sub_plmn_na *temp;
1079
1080         print(priv, "MCC    |MNC    |cause\n");
1081         print(priv, "-------+-------+-------\n");
1082         llist_for_each_entry(temp, &subscr->plmn_na, entry)
1083                 print(priv, "%s    |%s%s    |#%d\n",
1084                         gsm_print_mcc(temp->mcc), gsm_print_mnc(temp->mnc),
1085                         ((temp->mnc & 0x00f) == 0x00f) ? " ":"", temp->cause);
1086
1087         return 0;
1088 }
1089
1090 /* dump subscriber */
1091 void gsm_subscr_dump(struct gsm_subscriber *subscr,
1092                         void (*print)(void *, const char *, ...), void *priv)
1093 {
1094         int i;
1095         struct gsm_sub_plmn_list *plmn_list;
1096         struct gsm_sub_plmn_na *plmn_na;
1097
1098         print(priv, "Mobile Subscriber of MS '%s':\n", subscr->ms->name);
1099
1100         if (!subscr->sim_valid) {
1101                 print(priv, " No SIM present.\n");
1102                 return;
1103         }
1104
1105         print(priv, " IMSI: %s\n", subscr->imsi);
1106         if (subscr->iccid[0])
1107                 print(priv, " ICCID: %s\n", subscr->iccid);
1108         if (subscr->sim_spn[0])
1109                 print(priv, " Service Provider Name: %s\n", subscr->sim_spn);
1110         if (subscr->msisdn[0])
1111                 print(priv, " MSISDN: %s\n", subscr->msisdn);
1112         print(priv, " Status: %s  IMSI %s", subscr_ustate_names[subscr->ustate],
1113                 (subscr->imsi_attached) ? "attached" : "detached");
1114         if (subscr->tmsi != 0xffffffff)
1115                 print(priv, "  TSMI 0x%08x", subscr->tmsi);
1116         if (subscr->lac > 0x0000 && subscr->lac < 0xfffe) {
1117                 print(priv, "\n");
1118                 print(priv, "         LAI: MCC %s  MNC %s  LAC 0x%04x  "
1119                         "(%s, %s)\n", gsm_print_mcc(subscr->mcc),
1120                         gsm_print_mnc(subscr->mnc), subscr->lac,
1121                         gsm_get_mcc(subscr->mcc),
1122                         gsm_get_mnc(subscr->mcc, subscr->mnc));
1123         } else
1124                 print(priv, "  LAI: invalid\n");
1125         if (subscr->key_seq != 7) {
1126                 print(priv, " Key: sequence %d ", subscr->key_seq);
1127                 for (i = 0; i < sizeof(subscr->key); i++)
1128                         print(priv, " %02x", subscr->key[i]);
1129                 print(priv, "\n");
1130         }
1131         if (subscr->plmn_valid)
1132                 print(priv, " Registered PLMN: MCC %s  MNC %s  (%s, %s)\n",
1133                         gsm_print_mcc(subscr->plmn_mcc),
1134                         gsm_print_mnc(subscr->plmn_mnc),
1135                         gsm_get_mcc(subscr->plmn_mcc),
1136                         gsm_get_mnc(subscr->plmn_mcc, subscr->plmn_mnc));
1137         print(priv, " Access barred cells: %s\n",
1138                 (subscr->acc_barr) ? "yes" : "no");
1139         print(priv, " Access classes:");
1140         for (i = 0; i < 16; i++)
1141                 if ((subscr->acc_class & (1 << i)))
1142                         print(priv, " C%d", i);
1143         print(priv, "\n");
1144         if (!llist_empty(&subscr->plmn_list)) {
1145                 print(priv, " List of preferred PLMNs:\n");
1146                 print(priv, "        MCC    |MNC\n");
1147                 print(priv, "        -------+-------\n");
1148                 llist_for_each_entry(plmn_list, &subscr->plmn_list, entry)
1149                         print(priv, "        %s    |%s        (%s, %s)\n",
1150                         gsm_print_mcc(plmn_list->mcc),
1151                         gsm_print_mnc(plmn_list->mnc),
1152                         gsm_get_mcc(plmn_list->mcc),
1153                         gsm_get_mnc(plmn_list->mcc, plmn_list->mnc));
1154         }
1155         if (!llist_empty(&subscr->plmn_na)) {
1156                 print(priv, " List of forbidden PLMNs:\n");
1157                 print(priv, "        MCC    |MNC    |cause\n");
1158                 print(priv, "        -------+-------+-------\n");
1159                 llist_for_each_entry(plmn_na, &subscr->plmn_na, entry)
1160                         print(priv, "        %s    |%s%s    |#%d        "
1161                                 "(%s, %s)\n", gsm_print_mcc(plmn_na->mcc),
1162                                 gsm_print_mnc(plmn_na->mnc),
1163                                 ((plmn_na->mnc & 0x00f) == 0x00f) ? " ":"",
1164                                 plmn_na->cause, gsm_get_mcc(plmn_na->mcc),
1165                                 gsm_get_mnc(plmn_na->mcc, plmn_na->mnc));
1166         }
1167 }
1168