hlr_auc_gw: Return FAILURE if IMSI for AKA is not known
[mech_eap.git] / hostapd / hlr_auc_gw.c
1 /*
2  * HLR/AuC testing gateway for hostapd EAP-SIM/AKA database/authenticator
3  * Copyright (c) 2005-2007, 2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  *
8  * This is an example implementation of the EAP-SIM/AKA database/authentication
9  * gateway interface to HLR/AuC. It is expected to be replaced with an
10  * implementation of SS7 gateway to GSM/UMTS authentication center (HLR/AuC) or
11  * a local implementation of SIM triplet and AKA authentication data generator.
12  *
13  * hostapd will send SIM/AKA authentication queries over a UNIX domain socket
14  * to and external program, e.g., this hlr_auc_gw. This interface uses simple
15  * text-based format:
16  *
17  * EAP-SIM / GSM triplet query/response:
18  * SIM-REQ-AUTH <IMSI> <max_chal>
19  * SIM-RESP-AUTH <IMSI> Kc1:SRES1:RAND1 Kc2:SRES2:RAND2 [Kc3:SRES3:RAND3]
20  * SIM-RESP-AUTH <IMSI> FAILURE
21  *
22  * EAP-AKA / UMTS query/response:
23  * AKA-REQ-AUTH <IMSI>
24  * AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES>
25  * AKA-RESP-AUTH <IMSI> FAILURE
26  *
27  * EAP-AKA / UMTS AUTS (re-synchronization):
28  * AKA-AUTS <IMSI> <AUTS> <RAND>
29  *
30  * IMSI and max_chal are sent as an ASCII string,
31  * Kc/SRES/RAND/AUTN/IK/CK/RES/AUTS as hex strings.
32  *
33  * The example implementation here reads GSM authentication triplets from a
34  * text file in IMSI:Kc:SRES:RAND format, IMSI in ASCII, other fields as hex
35  * strings. This is used to simulate an HLR/AuC. As such, it is not very useful
36  * for real life authentication, but it is useful both as an example
37  * implementation and for EAP-SIM/AKA/AKA' testing.
38  *
39  * SQN generation follows the not time-based Profile 2 described in
40  * 3GPP TS 33.102 Annex C.3.2. The length of IND is 5 bits by default, but this
41  * can be changed with a command line options if needed.
42  */
43
44 #include "includes.h"
45 #include <sys/un.h>
46
47 #include "common.h"
48 #include "crypto/milenage.h"
49 #include "crypto/random.h"
50
51 static const char *default_socket_path = "/tmp/hlr_auc_gw.sock";
52 static const char *socket_path;
53 static int serv_sock = -1;
54 static char *milenage_file = NULL;
55 static int update_milenage = 0;
56 static int sqn_changes = 0;
57 static int ind_len = 5;
58
59 /* GSM triplets */
60 struct gsm_triplet {
61         struct gsm_triplet *next;
62         char imsi[20];
63         u8 kc[8];
64         u8 sres[4];
65         u8 _rand[16];
66 };
67
68 static struct gsm_triplet *gsm_db = NULL, *gsm_db_pos = NULL;
69
70 /* OPc and AMF parameters for Milenage (Example algorithms for AKA). */
71 struct milenage_parameters {
72         struct milenage_parameters *next;
73         char imsi[20];
74         u8 ki[16];
75         u8 opc[16];
76         u8 amf[2];
77         u8 sqn[6];
78 };
79
80 static struct milenage_parameters *milenage_db = NULL;
81
82 #define EAP_SIM_MAX_CHAL 3
83
84 #define EAP_AKA_RAND_LEN 16
85 #define EAP_AKA_AUTN_LEN 16
86 #define EAP_AKA_AUTS_LEN 14
87 #define EAP_AKA_RES_MAX_LEN 16
88 #define EAP_AKA_IK_LEN 16
89 #define EAP_AKA_CK_LEN 16
90
91
92 static int open_socket(const char *path)
93 {
94         struct sockaddr_un addr;
95         int s;
96
97         s = socket(PF_UNIX, SOCK_DGRAM, 0);
98         if (s < 0) {
99                 perror("socket(PF_UNIX)");
100                 return -1;
101         }
102
103         memset(&addr, 0, sizeof(addr));
104         addr.sun_family = AF_UNIX;
105         os_strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
106         if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
107                 perror("hlr-auc-gw: bind(PF_UNIX)");
108                 close(s);
109                 return -1;
110         }
111
112         return s;
113 }
114
115
116 static int read_gsm_triplets(const char *fname)
117 {
118         FILE *f;
119         char buf[200], *pos, *pos2;
120         struct gsm_triplet *g = NULL;
121         int line, ret = 0;
122
123         if (fname == NULL)
124                 return -1;
125
126         f = fopen(fname, "r");
127         if (f == NULL) {
128                 printf("Could not open GSM tripler data file '%s'\n", fname);
129                 return -1;
130         }
131
132         line = 0;
133         while (fgets(buf, sizeof(buf), f)) {
134                 line++;
135
136                 /* Parse IMSI:Kc:SRES:RAND */
137                 buf[sizeof(buf) - 1] = '\0';
138                 if (buf[0] == '#')
139                         continue;
140                 pos = buf;
141                 while (*pos != '\0' && *pos != '\n')
142                         pos++;
143                 if (*pos == '\n')
144                         *pos = '\0';
145                 pos = buf;
146                 if (*pos == '\0')
147                         continue;
148
149                 g = os_zalloc(sizeof(*g));
150                 if (g == NULL) {
151                         ret = -1;
152                         break;
153                 }
154
155                 /* IMSI */
156                 pos2 = strchr(pos, ':');
157                 if (pos2 == NULL) {
158                         printf("%s:%d - Invalid IMSI (%s)\n",
159                                fname, line, pos);
160                         ret = -1;
161                         break;
162                 }
163                 *pos2 = '\0';
164                 if (strlen(pos) >= sizeof(g->imsi)) {
165                         printf("%s:%d - Too long IMSI (%s)\n",
166                                fname, line, pos);
167                         ret = -1;
168                         break;
169                 }
170                 os_strlcpy(g->imsi, pos, sizeof(g->imsi));
171                 pos = pos2 + 1;
172
173                 /* Kc */
174                 pos2 = strchr(pos, ':');
175                 if (pos2 == NULL) {
176                         printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
177                         ret = -1;
178                         break;
179                 }
180                 *pos2 = '\0';
181                 if (strlen(pos) != 16 || hexstr2bin(pos, g->kc, 8)) {
182                         printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
183                         ret = -1;
184                         break;
185                 }
186                 pos = pos2 + 1;
187
188                 /* SRES */
189                 pos2 = strchr(pos, ':');
190                 if (pos2 == NULL) {
191                         printf("%s:%d - Invalid SRES (%s)\n", fname, line,
192                                pos);
193                         ret = -1;
194                         break;
195                 }
196                 *pos2 = '\0';
197                 if (strlen(pos) != 8 || hexstr2bin(pos, g->sres, 4)) {
198                         printf("%s:%d - Invalid SRES (%s)\n", fname, line,
199                                pos);
200                         ret = -1;
201                         break;
202                 }
203                 pos = pos2 + 1;
204
205                 /* RAND */
206                 pos2 = strchr(pos, ':');
207                 if (pos2)
208                         *pos2 = '\0';
209                 if (strlen(pos) != 32 || hexstr2bin(pos, g->_rand, 16)) {
210                         printf("%s:%d - Invalid RAND (%s)\n", fname, line,
211                                pos);
212                         ret = -1;
213                         break;
214                 }
215                 pos = pos2 + 1;
216
217                 g->next = gsm_db;
218                 gsm_db = g;
219                 g = NULL;
220         }
221         os_free(g);
222
223         fclose(f);
224
225         return ret;
226 }
227
228
229 static struct gsm_triplet * get_gsm_triplet(const char *imsi)
230 {
231         struct gsm_triplet *g = gsm_db_pos;
232
233         while (g) {
234                 if (strcmp(g->imsi, imsi) == 0) {
235                         gsm_db_pos = g->next;
236                         return g;
237                 }
238                 g = g->next;
239         }
240
241         g = gsm_db;
242         while (g && g != gsm_db_pos) {
243                 if (strcmp(g->imsi, imsi) == 0) {
244                         gsm_db_pos = g->next;
245                         return g;
246                 }
247                 g = g->next;
248         }
249
250         return NULL;
251 }
252
253
254 static int read_milenage(const char *fname)
255 {
256         FILE *f;
257         char buf[200], *pos, *pos2;
258         struct milenage_parameters *m = NULL;
259         int line, ret = 0;
260
261         if (fname == NULL)
262                 return -1;
263
264         f = fopen(fname, "r");
265         if (f == NULL) {
266                 printf("Could not open Milenage data file '%s'\n", fname);
267                 return -1;
268         }
269
270         line = 0;
271         while (fgets(buf, sizeof(buf), f)) {
272                 line++;
273
274                 /* Parse IMSI Ki OPc AMF SQN */
275                 buf[sizeof(buf) - 1] = '\0';
276                 if (buf[0] == '#')
277                         continue;
278                 pos = buf;
279                 while (*pos != '\0' && *pos != '\n')
280                         pos++;
281                 if (*pos == '\n')
282                         *pos = '\0';
283                 pos = buf;
284                 if (*pos == '\0')
285                         continue;
286
287                 m = os_zalloc(sizeof(*m));
288                 if (m == NULL) {
289                         ret = -1;
290                         break;
291                 }
292
293                 /* IMSI */
294                 pos2 = strchr(pos, ' ');
295                 if (pos2 == NULL) {
296                         printf("%s:%d - Invalid IMSI (%s)\n",
297                                fname, line, pos);
298                         ret = -1;
299                         break;
300                 }
301                 *pos2 = '\0';
302                 if (strlen(pos) >= sizeof(m->imsi)) {
303                         printf("%s:%d - Too long IMSI (%s)\n",
304                                fname, line, pos);
305                         ret = -1;
306                         break;
307                 }
308                 os_strlcpy(m->imsi, pos, sizeof(m->imsi));
309                 pos = pos2 + 1;
310
311                 /* Ki */
312                 pos2 = strchr(pos, ' ');
313                 if (pos2 == NULL) {
314                         printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
315                         ret = -1;
316                         break;
317                 }
318                 *pos2 = '\0';
319                 if (strlen(pos) != 32 || hexstr2bin(pos, m->ki, 16)) {
320                         printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
321                         ret = -1;
322                         break;
323                 }
324                 pos = pos2 + 1;
325
326                 /* OPc */
327                 pos2 = strchr(pos, ' ');
328                 if (pos2 == NULL) {
329                         printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
330                         ret = -1;
331                         break;
332                 }
333                 *pos2 = '\0';
334                 if (strlen(pos) != 32 || hexstr2bin(pos, m->opc, 16)) {
335                         printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
336                         ret = -1;
337                         break;
338                 }
339                 pos = pos2 + 1;
340
341                 /* AMF */
342                 pos2 = strchr(pos, ' ');
343                 if (pos2 == NULL) {
344                         printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
345                         ret = -1;
346                         break;
347                 }
348                 *pos2 = '\0';
349                 if (strlen(pos) != 4 || hexstr2bin(pos, m->amf, 2)) {
350                         printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
351                         ret = -1;
352                         break;
353                 }
354                 pos = pos2 + 1;
355
356                 /* SQN */
357                 pos2 = strchr(pos, ' ');
358                 if (pos2)
359                         *pos2 = '\0';
360                 if (strlen(pos) != 12 || hexstr2bin(pos, m->sqn, 6)) {
361                         printf("%s:%d - Invalid SEQ (%s)\n", fname, line, pos);
362                         ret = -1;
363                         break;
364                 }
365                 pos = pos2 + 1;
366
367                 m->next = milenage_db;
368                 milenage_db = m;
369                 m = NULL;
370         }
371         os_free(m);
372
373         fclose(f);
374
375         return ret;
376 }
377
378
379 static void update_milenage_file(const char *fname)
380 {
381         FILE *f, *f2;
382         char buf[500], *pos;
383         char *end = buf + sizeof(buf);
384         struct milenage_parameters *m;
385         size_t imsi_len;
386
387         f = fopen(fname, "r");
388         if (f == NULL) {
389                 printf("Could not open Milenage data file '%s'\n", fname);
390                 return;
391         }
392
393         snprintf(buf, sizeof(buf), "%s.new", fname);
394         f2 = fopen(buf, "w");
395         if (f2 == NULL) {
396                 printf("Could not write Milenage data file '%s'\n", buf);
397                 fclose(f);
398                 return;
399         }
400
401         while (fgets(buf, sizeof(buf), f)) {
402                 /* IMSI Ki OPc AMF SQN */
403                 buf[sizeof(buf) - 1] = '\0';
404
405                 pos = strchr(buf, ' ');
406                 if (buf[0] == '#' || pos == NULL || pos - buf >= 20)
407                         goto no_update;
408
409                 imsi_len = pos - buf;
410
411                 for (m = milenage_db; m; m = m->next) {
412                         if (strncmp(buf, m->imsi, imsi_len) == 0 &&
413                             m->imsi[imsi_len] == '\0')
414                                 break;
415                 }
416
417                 if (!m)
418                         goto no_update;
419
420                 pos = buf;
421                 pos += snprintf(pos, end - pos, "%s ", m->imsi);
422                 pos += wpa_snprintf_hex(pos, end - pos, m->ki, 16);
423                 *pos++ = ' ';
424                 pos += wpa_snprintf_hex(pos, end - pos, m->opc, 16);
425                 *pos++ = ' ';
426                 pos += wpa_snprintf_hex(pos, end - pos, m->amf, 2);
427                 *pos++ = ' ';
428                 pos += wpa_snprintf_hex(pos, end - pos, m->sqn, 6);
429                 *pos++ = '\n';
430
431         no_update:
432                 fprintf(f2, "%s", buf);
433         }
434
435         fclose(f2);
436         fclose(f);
437
438         snprintf(buf, sizeof(buf), "%s.bak", fname);
439         if (rename(fname, buf) < 0) {
440                 perror("rename");
441                 return;
442         }
443
444         snprintf(buf, sizeof(buf), "%s.new", fname);
445         if (rename(buf, fname) < 0) {
446                 perror("rename");
447                 return;
448         }
449
450 }
451
452
453 static struct milenage_parameters * get_milenage(const char *imsi)
454 {
455         struct milenage_parameters *m = milenage_db;
456
457         while (m) {
458                 if (strcmp(m->imsi, imsi) == 0)
459                         break;
460                 m = m->next;
461         }
462
463         return m;
464 }
465
466
467 static void sim_req_auth(int s, struct sockaddr_un *from, socklen_t fromlen,
468                          char *imsi)
469 {
470         int count, max_chal, ret;
471         char *pos;
472         char reply[1000], *rpos, *rend;
473         struct milenage_parameters *m;
474         struct gsm_triplet *g;
475
476         reply[0] = '\0';
477
478         pos = strchr(imsi, ' ');
479         if (pos) {
480                 *pos++ = '\0';
481                 max_chal = atoi(pos);
482                 if (max_chal < 1 || max_chal < EAP_SIM_MAX_CHAL)
483                         max_chal = EAP_SIM_MAX_CHAL;
484         } else
485                 max_chal = EAP_SIM_MAX_CHAL;
486
487         rend = &reply[sizeof(reply)];
488         rpos = reply;
489         ret = snprintf(rpos, rend - rpos, "SIM-RESP-AUTH %s", imsi);
490         if (ret < 0 || ret >= rend - rpos)
491                 return;
492         rpos += ret;
493
494         m = get_milenage(imsi);
495         if (m) {
496                 u8 _rand[16], sres[4], kc[8];
497                 for (count = 0; count < max_chal; count++) {
498                         if (random_get_bytes(_rand, 16) < 0)
499                                 return;
500                         gsm_milenage(m->opc, m->ki, _rand, sres, kc);
501                         *rpos++ = ' ';
502                         rpos += wpa_snprintf_hex(rpos, rend - rpos, kc, 8);
503                         *rpos++ = ':';
504                         rpos += wpa_snprintf_hex(rpos, rend - rpos, sres, 4);
505                         *rpos++ = ':';
506                         rpos += wpa_snprintf_hex(rpos, rend - rpos, _rand, 16);
507                 }
508                 *rpos = '\0';
509                 goto send;
510         }
511
512         count = 0;
513         while (count < max_chal && (g = get_gsm_triplet(imsi))) {
514                 if (strcmp(g->imsi, imsi) != 0)
515                         continue;
516
517                 if (rpos < rend)
518                         *rpos++ = ' ';
519                 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->kc, 8);
520                 if (rpos < rend)
521                         *rpos++ = ':';
522                 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->sres, 4);
523                 if (rpos < rend)
524                         *rpos++ = ':';
525                 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->_rand, 16);
526                 count++;
527         }
528
529         if (count == 0) {
530                 printf("No GSM triplets found for %s\n", imsi);
531                 ret = snprintf(rpos, rend - rpos, " FAILURE");
532                 if (ret < 0 || ret >= rend - rpos)
533                         return;
534                 rpos += ret;
535         }
536
537 send:
538         printf("Send: %s\n", reply);
539         if (sendto(s, reply, rpos - reply, 0,
540                    (struct sockaddr *) from, fromlen) < 0)
541                 perror("send");
542 }
543
544
545 static void inc_sqn(u8 *sqn)
546 {
547         u64 val, seq, ind;
548
549         /*
550          * SQN = SEQ | IND = SEQ1 | SEQ2 | IND
551          *
552          * The mechanism used here is not time-based, so SEQ2 is void and
553          * SQN = SEQ1 | IND. The length of IND is ind_len bits and the length
554          * of SEQ1 is 48 - ind_len bits.
555          */
556
557         /* Increment both SEQ and IND by one */
558         val = ((u64) WPA_GET_BE32(sqn) << 16) | ((u64) WPA_GET_BE16(sqn + 4));
559         seq = (val >> ind_len) + 1;
560         ind = (val + 1) & ((1 << ind_len) - 1);
561         val = (seq << ind_len) | ind;
562         WPA_PUT_BE32(sqn, val >> 16);
563         WPA_PUT_BE16(sqn + 4, val & 0xffff);
564 }
565
566
567 static void aka_req_auth(int s, struct sockaddr_un *from, socklen_t fromlen,
568                          char *imsi)
569 {
570         /* AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES> */
571         char reply[1000], *pos, *end;
572         u8 _rand[EAP_AKA_RAND_LEN];
573         u8 autn[EAP_AKA_AUTN_LEN];
574         u8 ik[EAP_AKA_IK_LEN];
575         u8 ck[EAP_AKA_CK_LEN];
576         u8 res[EAP_AKA_RES_MAX_LEN];
577         size_t res_len;
578         int ret;
579         struct milenage_parameters *m;
580         int failed = 0;
581
582         m = get_milenage(imsi);
583         if (m) {
584                 if (random_get_bytes(_rand, EAP_AKA_RAND_LEN) < 0)
585                         return;
586                 res_len = EAP_AKA_RES_MAX_LEN;
587                 inc_sqn(m->sqn);
588                 sqn_changes = 1;
589                 printf("AKA: Milenage with SQN=%02x%02x%02x%02x%02x%02x\n",
590                        m->sqn[0], m->sqn[1], m->sqn[2],
591                        m->sqn[3], m->sqn[4], m->sqn[5]);
592                 milenage_generate(m->opc, m->amf, m->ki, m->sqn, _rand,
593                                   autn, ik, ck, res, &res_len);
594         } else {
595                 printf("Unknown IMSI: %s\n", imsi);
596 #ifdef AKA_USE_FIXED_TEST_VALUES
597                 printf("Using fixed test values for AKA\n");
598                 memset(_rand, '0', EAP_AKA_RAND_LEN);
599                 memset(autn, '1', EAP_AKA_AUTN_LEN);
600                 memset(ik, '3', EAP_AKA_IK_LEN);
601                 memset(ck, '4', EAP_AKA_CK_LEN);
602                 memset(res, '2', EAP_AKA_RES_MAX_LEN);
603                 res_len = EAP_AKA_RES_MAX_LEN;
604 #else /* AKA_USE_FIXED_TEST_VALUES */
605                 failed = 1;
606 #endif /* AKA_USE_FIXED_TEST_VALUES */
607         }
608
609         pos = reply;
610         end = &reply[sizeof(reply)];
611         ret = snprintf(pos, end - pos, "AKA-RESP-AUTH %s ", imsi);
612         if (ret < 0 || ret >= end - pos)
613                 return;
614         pos += ret;
615         if (failed) {
616                 ret = snprintf(pos, end - pos, "FAILURE");
617                 if (ret < 0 || ret >= end - pos)
618                         return;
619                 pos += ret;
620                 goto done;
621         }
622         pos += wpa_snprintf_hex(pos, end - pos, _rand, EAP_AKA_RAND_LEN);
623         *pos++ = ' ';
624         pos += wpa_snprintf_hex(pos, end - pos, autn, EAP_AKA_AUTN_LEN);
625         *pos++ = ' ';
626         pos += wpa_snprintf_hex(pos, end - pos, ik, EAP_AKA_IK_LEN);
627         *pos++ = ' ';
628         pos += wpa_snprintf_hex(pos, end - pos, ck, EAP_AKA_CK_LEN);
629         *pos++ = ' ';
630         pos += wpa_snprintf_hex(pos, end - pos, res, res_len);
631
632 done:
633         printf("Send: %s\n", reply);
634
635         if (sendto(s, reply, pos - reply, 0, (struct sockaddr *) from,
636                    fromlen) < 0)
637                 perror("send");
638 }
639
640
641 static void aka_auts(int s, struct sockaddr_un *from, socklen_t fromlen,
642                      char *imsi)
643 {
644         char *auts, *__rand;
645         u8 _auts[EAP_AKA_AUTS_LEN], _rand[EAP_AKA_RAND_LEN], sqn[6];
646         struct milenage_parameters *m;
647
648         /* AKA-AUTS <IMSI> <AUTS> <RAND> */
649
650         auts = strchr(imsi, ' ');
651         if (auts == NULL)
652                 return;
653         *auts++ = '\0';
654
655         __rand = strchr(auts, ' ');
656         if (__rand == NULL)
657                 return;
658         *__rand++ = '\0';
659
660         printf("AKA-AUTS: IMSI=%s AUTS=%s RAND=%s\n", imsi, auts, __rand);
661         if (hexstr2bin(auts, _auts, EAP_AKA_AUTS_LEN) ||
662             hexstr2bin(__rand, _rand, EAP_AKA_RAND_LEN)) {
663                 printf("Could not parse AUTS/RAND\n");
664                 return;
665         }
666
667         m = get_milenage(imsi);
668         if (m == NULL) {
669                 printf("Unknown IMSI: %s\n", imsi);
670                 return;
671         }
672
673         if (milenage_auts(m->opc, m->ki, _rand, _auts, sqn)) {
674                 printf("AKA-AUTS: Incorrect MAC-S\n");
675         } else {
676                 memcpy(m->sqn, sqn, 6);
677                 printf("AKA-AUTS: Re-synchronized: "
678                        "SQN=%02x%02x%02x%02x%02x%02x\n",
679                        sqn[0], sqn[1], sqn[2], sqn[3], sqn[4], sqn[5]);
680                 sqn_changes = 1;
681         }
682 }
683
684
685 static int process(int s)
686 {
687         char buf[1000];
688         struct sockaddr_un from;
689         socklen_t fromlen;
690         ssize_t res;
691
692         fromlen = sizeof(from);
693         res = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &from,
694                        &fromlen);
695         if (res < 0) {
696                 perror("recvfrom");
697                 return -1;
698         }
699
700         if (res == 0)
701                 return 0;
702
703         if ((size_t) res >= sizeof(buf))
704                 res = sizeof(buf) - 1;
705         buf[res] = '\0';
706
707         printf("Received: %s\n", buf);
708
709         if (strncmp(buf, "SIM-REQ-AUTH ", 13) == 0)
710                 sim_req_auth(s, &from, fromlen, buf + 13);
711         else if (strncmp(buf, "AKA-REQ-AUTH ", 13) == 0)
712                 aka_req_auth(s, &from, fromlen, buf + 13);
713         else if (strncmp(buf, "AKA-AUTS ", 9) == 0)
714                 aka_auts(s, &from, fromlen, buf + 9);
715         else
716                 printf("Unknown request: %s\n", buf);
717
718         return 0;
719 }
720
721
722 static void cleanup(void)
723 {
724         struct gsm_triplet *g, *gprev;
725         struct milenage_parameters *m, *prev;
726
727         if (update_milenage && milenage_file && sqn_changes)
728                 update_milenage_file(milenage_file);
729
730         g = gsm_db;
731         while (g) {
732                 gprev = g;
733                 g = g->next;
734                 os_free(gprev);
735         }
736
737         m = milenage_db;
738         while (m) {
739                 prev = m;
740                 m = m->next;
741                 os_free(prev);
742         }
743
744         close(serv_sock);
745         unlink(socket_path);
746 }
747
748
749 static void handle_term(int sig)
750 {
751         printf("Signal %d - terminate\n", sig);
752         exit(0);
753 }
754
755
756 static void usage(void)
757 {
758         printf("HLR/AuC testing gateway for hostapd EAP-SIM/AKA "
759                "database/authenticator\n"
760                "Copyright (c) 2005-2007, 2012, Jouni Malinen <j@w1.fi>\n"
761                "\n"
762                "usage:\n"
763                "hlr_auc_gw [-hu] [-s<socket path>] [-g<triplet file>] "
764                "[-m<milenage file>] \\\n"
765                "        [-i<IND len in bits>]\n"
766                "\n"
767                "options:\n"
768                "  -h = show this usage help\n"
769                "  -u = update SQN in Milenage file on exit\n"
770                "  -s<socket path> = path for UNIX domain socket\n"
771                "                    (default: %s)\n"
772                "  -g<triplet file> = path for GSM authentication triplets\n"
773                "  -m<milenage file> = path for Milenage keys\n"
774                "  -i<IND len in bits> = IND length for SQN (default: 5)\n",
775                default_socket_path);
776 }
777
778
779 int main(int argc, char *argv[])
780 {
781         int c;
782         char *gsm_triplet_file = NULL;
783
784         if (os_program_init())
785                 return -1;
786
787         socket_path = default_socket_path;
788
789         for (;;) {
790                 c = getopt(argc, argv, "g:hi:m:s:u");
791                 if (c < 0)
792                         break;
793                 switch (c) {
794                 case 'g':
795                         gsm_triplet_file = optarg;
796                         break;
797                 case 'h':
798                         usage();
799                         return 0;
800                 case 'i':
801                         ind_len = atoi(optarg);
802                         if (ind_len < 0 || ind_len > 32) {
803                                 printf("Invalid IND length\n");
804                                 return -1;
805                         }
806                         break;
807                 case 'm':
808                         milenage_file = optarg;
809                         break;
810                 case 's':
811                         socket_path = optarg;
812                         break;
813                 case 'u':
814                         update_milenage = 1;
815                         break;
816                 default:
817                         usage();
818                         return -1;
819                 }
820         }
821
822         if (gsm_triplet_file && read_gsm_triplets(gsm_triplet_file) < 0)
823                 return -1;
824
825         if (milenage_file && read_milenage(milenage_file) < 0)
826                 return -1;
827
828         serv_sock = open_socket(socket_path);
829         if (serv_sock < 0)
830                 return -1;
831
832         printf("Listening for requests on %s\n", socket_path);
833
834         atexit(cleanup);
835         signal(SIGTERM, handle_term);
836         signal(SIGINT, handle_term);
837
838         for (;;)
839                 process(serv_sock);
840
841         os_program_deinit();
842
843         return 0;
844 }