e6c41db243d51d30920b08f22f370d8a8d90e3b6
[mech_eap.git] / src / pae / ieee802_1x_kay.c
1 /*
2  * IEEE 802.1X-2010 Key Agree Protocol of PAE state machine
3  * Copyright (c) 2013, Qualcomm Atheros, Inc.
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include <time.h>
10 #include "includes.h"
11 #include "common.h"
12 #include "list.h"
13 #include "eloop.h"
14 #include "wpabuf.h"
15 #include "state_machine.h"
16 #include "l2_packet/l2_packet.h"
17 #include "common/eapol_common.h"
18 #include "crypto/aes_wrap.h"
19 #include "ieee802_1x_cp.h"
20 #include "ieee802_1x_key.h"
21 #include "ieee802_1x_kay.h"
22 #include "ieee802_1x_kay_i.h"
23 #include "ieee802_1x_secy_ops.h"
24
25
26 #define DEFAULT_SA_KEY_LEN      16
27 #define DEFAULT_ICV_LEN         16
28 #define MAX_ICV_LEN             32  /* 32 bytes, 256 bits */
29
30 #define PENDING_PN_EXHAUSTION 0xC0000000
31
32 /* IEEE Std 802.1X-2010, Table 9-1 - MKA Algorithm Agility */
33 #define MKA_ALGO_AGILITY_2009 { 0x00, 0x80, 0xC2, 0x01 }
34 static u8 mka_algo_agility[4] = MKA_ALGO_AGILITY_2009;
35
36 /* IEEE802.1AE-2006 Table 14-1 MACsec Cipher Suites */
37 static struct macsec_ciphersuite cipher_suite_tbl[] = {
38         /* GCM-AES-128 */
39         {
40                 CS_ID_GCM_AES_128,
41                 CS_NAME_GCM_AES_128,
42                 MACSEC_CAP_INTEG_AND_CONF_0_30_50,
43                 16,
44
45                 0 /* index */
46         },
47 };
48 #define CS_TABLE_SIZE (ARRAY_SIZE(cipher_suite_tbl))
49 #define DEFAULT_CS_INDEX  0
50
51 static struct mka_alg mka_alg_tbl[] = {
52         {
53                 MKA_ALGO_AGILITY_2009,
54                 /* 128-bit CAK, KEK, ICK, ICV */
55                 16, 16, 16, 16,
56                 ieee802_1x_cak_128bits_aes_cmac,
57                 ieee802_1x_ckn_128bits_aes_cmac,
58                 ieee802_1x_kek_128bits_aes_cmac,
59                 ieee802_1x_ick_128bits_aes_cmac,
60                 ieee802_1x_icv_128bits_aes_cmac,
61
62                 1, /* index */
63         },
64 };
65 #define MKA_ALG_TABLE_SIZE (ARRAY_SIZE(mka_alg_tbl))
66
67
68 static int is_ki_equal(struct ieee802_1x_mka_ki *ki1,
69                        struct ieee802_1x_mka_ki *ki2)
70 {
71         return os_memcmp(ki1->mi, ki2->mi, MI_LEN) == 0 &&
72                 ki1->kn == ki2->kn;
73 }
74
75
76 struct mka_param_body_handler {
77         int (*body_tx)(struct ieee802_1x_mka_participant *participant,
78                        struct wpabuf *buf);
79         int (*body_rx)(struct ieee802_1x_mka_participant *participant,
80                        const u8 *mka_msg, size_t msg_len);
81         int (*body_length)(struct ieee802_1x_mka_participant *participant);
82         Boolean (*body_present)(struct ieee802_1x_mka_participant *participant);
83 };
84
85
86 static void set_mka_param_body_len(void *body, unsigned int len)
87 {
88         struct ieee802_1x_mka_hdr *hdr = body;
89         hdr->length = (len >> 8) & 0x0f;
90         hdr->length1 = len & 0xff;
91 }
92
93
94 static unsigned int get_mka_param_body_len(const void *body)
95 {
96         const struct ieee802_1x_mka_hdr *hdr = body;
97         return (hdr->length << 8) | hdr->length1;
98 }
99
100
101 static u8 get_mka_param_body_type(const void *body)
102 {
103         const struct ieee802_1x_mka_hdr *hdr = body;
104         return hdr->type;
105 }
106
107
108 /**
109  * ieee802_1x_mka_dump_basic_body -
110  */
111 static void
112 ieee802_1x_mka_dump_basic_body(struct ieee802_1x_mka_basic_body *body)
113 {
114         size_t body_len;
115
116         if (!body)
117                 return;
118
119         body_len = get_mka_param_body_len(body);
120         wpa_printf(MSG_DEBUG, "*** MKA Basic Parameter set ***");
121         wpa_printf(MSG_DEBUG, "\tVersion.......: %d", body->version);
122         wpa_printf(MSG_DEBUG, "\tPriority......: %d", body->priority);
123         wpa_printf(MSG_DEBUG, "\tKeySvr........: %d", body->key_server);
124         wpa_printf(MSG_DEBUG, "\tMACSecDesired.: %d", body->macsec_desired);
125         wpa_printf(MSG_DEBUG, "\tMACSecCapable.: %d", body->macsec_capbility);
126         wpa_printf(MSG_DEBUG, "\tBody Length...: %d", (int) body_len);
127         wpa_printf(MSG_DEBUG, "\tSCI MAC.......: " MACSTR,
128                    MAC2STR(body->actor_sci.addr));
129         wpa_printf(MSG_DEBUG, "\tSCI Port .....: %d",
130                    be_to_host16(body->actor_sci.port));
131         wpa_hexdump(MSG_DEBUG, "\tMember Id.....:",
132                     body->actor_mi, sizeof(body->actor_mi));
133         wpa_printf(MSG_DEBUG, "\tMessage Number: %d",
134                    be_to_host32(body->actor_mn));
135         wpa_hexdump(MSG_DEBUG, "\tAlgo Agility..:",
136                     body->algo_agility, sizeof(body->algo_agility));
137         wpa_hexdump_ascii(MSG_DEBUG, "\tCAK Name......:", body->ckn,
138                           body_len + MKA_HDR_LEN - sizeof(*body));
139 }
140
141
142 /**
143  * ieee802_1x_mka_dump_peer_body -
144  */
145 static void
146 ieee802_1x_mka_dump_peer_body(struct ieee802_1x_mka_peer_body *body)
147 {
148         size_t body_len;
149         size_t i;
150         u8 *mi;
151         be32 mn;
152
153         if (body == NULL)
154                 return;
155
156         body_len = get_mka_param_body_len(body);
157         if (body->type == MKA_LIVE_PEER_LIST) {
158                 wpa_printf(MSG_DEBUG, "*** Live Peer List ***");
159                 wpa_printf(MSG_DEBUG, "\tBody Length...: %d", (int) body_len);
160         } else if (body->type == MKA_POTENTIAL_PEER_LIST) {
161                 wpa_printf(MSG_DEBUG, "*** Potential Live Peer List ***");
162                 wpa_printf(MSG_DEBUG, "\tBody Length...: %d", (int) body_len);
163         }
164
165         for (i = 0; i < body_len; i += MI_LEN + sizeof(mn)) {
166                 mi = body->peer + i;
167                 os_memcpy(&mn, mi + MI_LEN, sizeof(mn));
168                 wpa_hexdump_ascii(MSG_DEBUG, "\tMember Id.....:", mi, MI_LEN);
169                 wpa_printf(MSG_DEBUG, "\tMessage Number: %d", be_to_host32(mn));
170         }
171 }
172
173
174 /**
175  * ieee802_1x_mka_dump_dist_sak_body -
176  */
177 static void
178 ieee802_1x_mka_dump_dist_sak_body(struct ieee802_1x_mka_dist_sak_body *body)
179 {
180         size_t body_len;
181
182         if (body == NULL)
183                 return;
184
185         body_len = get_mka_param_body_len(body);
186         wpa_printf(MSG_INFO, "*** Distributed SAK ***");
187         wpa_printf(MSG_INFO, "\tDistributed AN........: %d", body->dan);
188         wpa_printf(MSG_INFO, "\tConfidentiality Offset: %d",
189                    body->confid_offset);
190         wpa_printf(MSG_INFO, "\tBody Length...........: %d", (int) body_len);
191         if (!body_len)
192                 return;
193
194         wpa_printf(MSG_INFO, "\tKey Number............: %d",
195                    be_to_host32(body->kn));
196         wpa_hexdump(MSG_INFO, "\tAES Key Wrap of SAK...:", body->sak, 24);
197 }
198
199
200 static const char * yes_no(int val)
201 {
202         return val ? "Yes" : "No";
203 }
204
205
206 /**
207  * ieee802_1x_mka_dump_sak_use_body -
208  */
209 static void
210 ieee802_1x_mka_dump_sak_use_body(struct ieee802_1x_mka_sak_use_body *body)
211 {
212         int body_len;
213
214         if (body == NULL)
215                 return;
216
217         body_len = get_mka_param_body_len(body);
218         wpa_printf(MSG_DEBUG, "*** MACsec SAK Use ***");
219         wpa_printf(MSG_DEBUG, "\tLatest Key AN....: %d", body->lan);
220         wpa_printf(MSG_DEBUG, "\tLatest Key Tx....: %s", yes_no(body->ltx));
221         wpa_printf(MSG_DEBUG, "\tLatest Key Rx....: %s", yes_no(body->lrx));
222         wpa_printf(MSG_DEBUG, "\tOld Key AN....: %d", body->oan);
223         wpa_printf(MSG_DEBUG, "\tOld Key Tx....: %s", yes_no(body->otx));
224         wpa_printf(MSG_DEBUG, "\tOld Key Rx....: %s", yes_no(body->orx));
225         wpa_printf(MSG_DEBUG, "\tPlain Key Tx....: %s", yes_no(body->ptx));
226         wpa_printf(MSG_DEBUG, "\tPlain Key Rx....: %s", yes_no(body->prx));
227         wpa_printf(MSG_DEBUG, "\tDelay Protect....: %s",
228                    yes_no(body->delay_protect));
229         wpa_printf(MSG_DEBUG, "\tBody Length......: %d", body_len);
230         if (!body_len)
231                 return;
232
233         wpa_hexdump(MSG_DEBUG, "\tKey Server MI....:",
234                     body->lsrv_mi, sizeof(body->lsrv_mi));
235         wpa_printf(MSG_DEBUG, "\tKey Number.......: %u",
236                    be_to_host32(body->lkn));
237         wpa_printf(MSG_DEBUG, "\tLowest PN........: %u",
238                    be_to_host32(body->llpn));
239         wpa_hexdump_ascii(MSG_DEBUG, "\tOld Key Server MI....:",
240                           body->osrv_mi, sizeof(body->osrv_mi));
241         wpa_printf(MSG_DEBUG, "\tOld Key Number.......: %u",
242                    be_to_host32(body->okn));
243         wpa_printf(MSG_DEBUG, "\tOld Lowest PN........: %u",
244                    be_to_host32(body->olpn));
245 }
246
247
248 /**
249  * ieee802_1x_kay_get_participant -
250  */
251 static struct ieee802_1x_mka_participant *
252 ieee802_1x_kay_get_participant(struct ieee802_1x_kay *kay, const u8 *ckn)
253 {
254         struct ieee802_1x_mka_participant *participant;
255
256         dl_list_for_each(participant, &kay->participant_list,
257                          struct ieee802_1x_mka_participant, list) {
258                 if (os_memcmp(participant->ckn.name, ckn,
259                               participant->ckn.len) == 0)
260                         return participant;
261         }
262
263         wpa_printf(MSG_DEBUG, "KaY: participant is not found");
264
265         return NULL;
266 }
267
268
269 /**
270  * ieee802_1x_kay_get_principal_participant -
271  */
272 static struct ieee802_1x_mka_participant *
273 ieee802_1x_kay_get_principal_participant(struct ieee802_1x_kay *kay)
274 {
275         struct ieee802_1x_mka_participant *participant;
276
277         dl_list_for_each(participant, &kay->participant_list,
278                          struct ieee802_1x_mka_participant, list) {
279                 if (participant->principal)
280                         return participant;
281         }
282
283         wpa_printf(MSG_DEBUG, "KaY: principal participant is not founded");
284         return NULL;
285 }
286
287
288 static struct ieee802_1x_kay_peer * get_peer_mi(struct dl_list *peers,
289                                                 const u8 *mi)
290 {
291         struct ieee802_1x_kay_peer *peer;
292
293         dl_list_for_each(peer, peers, struct ieee802_1x_kay_peer, list) {
294                 if (os_memcmp(peer->mi, mi, MI_LEN) == 0)
295                         return peer;
296         }
297
298         return NULL;
299 }
300
301
302 /**
303  * ieee802_1x_kay_is_in_potential_peer
304  */
305 static Boolean
306 ieee802_1x_kay_is_in_potential_peer(
307         struct ieee802_1x_mka_participant *participant, const u8 *mi)
308 {
309         return get_peer_mi(&participant->potential_peers, mi) != NULL;
310 }
311
312
313 /**
314  * ieee802_1x_kay_is_in_live_peer
315  */
316 static Boolean
317 ieee802_1x_kay_is_in_live_peer(
318         struct ieee802_1x_mka_participant *participant, const u8 *mi)
319 {
320         return get_peer_mi(&participant->live_peers, mi) != NULL;
321 }
322
323
324 /**
325  * ieee802_1x_kay_is_in_peer
326  */
327 static Boolean
328 ieee802_1x_kay_is_in_peer(struct ieee802_1x_mka_participant *participant,
329                           const u8 *mi)
330 {
331         return ieee802_1x_kay_is_in_live_peer(participant, mi) ||
332                 ieee802_1x_kay_is_in_potential_peer(participant, mi);
333 }
334
335
336 /**
337  * ieee802_1x_kay_get_peer
338  */
339 static struct ieee802_1x_kay_peer *
340 ieee802_1x_kay_get_peer(struct ieee802_1x_mka_participant *participant,
341                         const u8 *mi)
342 {
343         struct ieee802_1x_kay_peer *peer;
344
345         peer = get_peer_mi(&participant->live_peers, mi);
346         if (peer)
347                 return peer;
348
349         return get_peer_mi(&participant->potential_peers, mi);
350 }
351
352
353 /**
354  * ieee802_1x_kay_get_live_peer
355  */
356 static struct ieee802_1x_kay_peer *
357 ieee802_1x_kay_get_live_peer(struct ieee802_1x_mka_participant *participant,
358                              const u8 *mi)
359 {
360         return get_peer_mi(&participant->live_peers, mi);
361 }
362
363
364 /**
365  * ieee802_1x_kay_get_cipher_suite
366  */
367 static struct macsec_ciphersuite *
368 ieee802_1x_kay_get_cipher_suite(struct ieee802_1x_mka_participant *participant,
369                                 u8 *cs_id)
370 {
371         unsigned int i;
372
373         for (i = 0; i < CS_TABLE_SIZE; i++) {
374                 if (os_memcmp(cipher_suite_tbl[i].id, cs_id, CS_ID_LEN) == 0)
375                         break;
376         }
377         if (i >= CS_TABLE_SIZE)
378                 return NULL;
379
380         return &cipher_suite_tbl[i];
381 }
382
383
384 /**
385  * ieee802_1x_kay_get_peer_sci
386  */
387 static struct ieee802_1x_kay_peer *
388 ieee802_1x_kay_get_peer_sci(struct ieee802_1x_mka_participant *participant,
389                             const struct ieee802_1x_mka_sci *sci)
390 {
391         struct ieee802_1x_kay_peer *peer;
392
393         dl_list_for_each(peer, &participant->live_peers,
394                          struct ieee802_1x_kay_peer, list) {
395                 if (os_memcmp(&peer->sci, sci, sizeof(peer->sci)) == 0)
396                         return peer;
397         }
398
399         dl_list_for_each(peer, &participant->potential_peers,
400                          struct ieee802_1x_kay_peer, list) {
401                 if (os_memcmp(&peer->sci, sci, sizeof(peer->sci)) == 0)
402                         return peer;
403         }
404
405         return NULL;
406 }
407
408
409 /**
410  * ieee802_1x_kay_init_receive_sa -
411  */
412 static struct receive_sa *
413 ieee802_1x_kay_init_receive_sa(struct receive_sc *psc, u8 an, u32 lowest_pn,
414                                struct data_key *key)
415 {
416         struct receive_sa *psa;
417
418         if (!psc || !key)
419                 return NULL;
420
421         psa = os_zalloc(sizeof(*psa));
422         if (!psa) {
423                 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
424                 return NULL;
425         }
426
427         psa->pkey = key;
428         psa->lowest_pn = lowest_pn;
429         psa->next_pn = lowest_pn;
430         psa->an = an;
431         psa->sc = psc;
432
433         os_get_time(&psa->created_time);
434         psa->in_use = FALSE;
435
436         dl_list_add(&psc->sa_list, &psa->list);
437         wpa_printf(MSG_DEBUG,
438                    "KaY: Create receive SA(AN: %d lowest_pn: %u of SC(channel: %d)",
439                    (int) an, lowest_pn, psc->channel);
440
441         return psa;
442 }
443
444
445 /**
446  * ieee802_1x_kay_deinit_receive_sa -
447  */
448 static void ieee802_1x_kay_deinit_receive_sa(struct receive_sa *psa)
449 {
450         psa->pkey = NULL;
451         wpa_printf(MSG_DEBUG,
452                    "KaY: Delete receive SA(an: %d) of SC(channel: %d)",
453                    psa->an, psa->sc->channel);
454         dl_list_del(&psa->list);
455         os_free(psa);
456 }
457
458
459 /**
460  * ieee802_1x_kay_init_receive_sc -
461  */
462 static struct receive_sc *
463 ieee802_1x_kay_init_receive_sc(const struct ieee802_1x_mka_sci *psci,
464                                int channel)
465 {
466         struct receive_sc *psc;
467
468         if (!psci)
469                 return NULL;
470
471         psc = os_zalloc(sizeof(*psc));
472         if (!psc) {
473                 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
474                 return NULL;
475         }
476
477         os_memcpy(&psc->sci, psci, sizeof(psc->sci));
478         psc->channel = channel;
479
480         os_get_time(&psc->created_time);
481         psc->receiving = FALSE;
482
483         dl_list_init(&psc->sa_list);
484         wpa_printf(MSG_DEBUG, "KaY: Create receive SC(channel: %d)", channel);
485         wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)psci, sizeof(*psci));
486
487         return psc;
488 }
489
490
491 /**
492  * ieee802_1x_kay_deinit_receive_sc -
493  **/
494 static void
495 ieee802_1x_kay_deinit_receive_sc(
496         struct ieee802_1x_mka_participant *participant, struct receive_sc *psc)
497 {
498         struct receive_sa *psa, *pre_sa;
499
500         wpa_printf(MSG_DEBUG, "KaY: Delete receive SC(channel: %d)",
501                    psc->channel);
502         dl_list_for_each_safe(psa, pre_sa, &psc->sa_list, struct receive_sa,
503                               list)  {
504                 secy_disable_receive_sa(participant->kay, psa);
505                 ieee802_1x_kay_deinit_receive_sa(psa);
506         }
507         dl_list_del(&psc->list);
508         os_free(psc);
509 }
510
511
512 /**
513  * ieee802_1x_kay_create_live_peer
514  */
515 static struct ieee802_1x_kay_peer *
516 ieee802_1x_kay_create_live_peer(struct ieee802_1x_mka_participant *participant,
517                                 u8 *mi, u32 mn)
518 {
519         struct ieee802_1x_kay_peer *peer;
520         struct receive_sc *rxsc;
521         u32 sc_ch = 0;
522
523         peer = os_zalloc(sizeof(*peer));
524         if (peer == NULL) {
525                 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
526                 return NULL;
527         }
528
529         os_memcpy(peer->mi, mi, MI_LEN);
530         peer->mn = mn;
531         peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
532         peer->sak_used = FALSE;
533         os_memcpy(&peer->sci, &participant->current_peer_sci,
534                   sizeof(peer->sci));
535
536         secy_get_available_receive_sc(participant->kay, &sc_ch);
537
538         rxsc = ieee802_1x_kay_init_receive_sc(&peer->sci, sc_ch);
539         if (!rxsc) {
540                 os_free(peer);
541                 return NULL;
542         }
543
544         dl_list_add(&participant->live_peers, &peer->list);
545         dl_list_add(&participant->rxsc_list, &rxsc->list);
546         secy_create_receive_sc(participant->kay, rxsc);
547
548         wpa_printf(MSG_DEBUG, "KaY: Live peer created");
549         wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
550         wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
551         wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
552         wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
553
554         return peer;
555 }
556
557
558 /**
559  * ieee802_1x_kay_create_potential_peer
560  */
561 static struct ieee802_1x_kay_peer *
562 ieee802_1x_kay_create_potential_peer(
563         struct ieee802_1x_mka_participant *participant, const u8 *mi, u32 mn)
564 {
565         struct ieee802_1x_kay_peer *peer;
566
567         peer = os_zalloc(sizeof(*peer));
568         if (peer == NULL) {
569                 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
570                 return NULL;
571         }
572
573         os_memcpy(peer->mi, mi, MI_LEN);
574         peer->mn = mn;
575         peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
576         peer->sak_used = FALSE;
577
578         dl_list_add(&participant->potential_peers, &peer->list);
579
580         wpa_printf(MSG_DEBUG, "KaY: potential peer created");
581         wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
582         wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
583         wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
584         wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
585
586         return peer;
587 }
588
589
590 /**
591  * ieee802_1x_kay_move_live_peer
592  */
593 static struct ieee802_1x_kay_peer *
594 ieee802_1x_kay_move_live_peer(struct ieee802_1x_mka_participant *participant,
595                               u8 *mi, u32 mn)
596 {
597         struct ieee802_1x_kay_peer *peer;
598         struct receive_sc *rxsc;
599         u32 sc_ch = 0;
600
601         dl_list_for_each(peer, &participant->potential_peers,
602                          struct ieee802_1x_kay_peer, list) {
603                 if (os_memcmp(peer->mi, mi, MI_LEN) == 0)
604                         break;
605         }
606
607         rxsc = ieee802_1x_kay_init_receive_sc(&participant->current_peer_sci,
608                                               sc_ch);
609         if (!rxsc)
610                 return NULL;
611
612         os_memcpy(&peer->sci, &participant->current_peer_sci,
613                   sizeof(peer->sci));
614         peer->mn = mn;
615         peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
616
617         wpa_printf(MSG_DEBUG, "KaY: move potential peer to live peer");
618         wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
619         wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
620         wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
621         wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
622
623         dl_list_del(&peer->list);
624         dl_list_add_tail(&participant->live_peers, &peer->list);
625
626         secy_get_available_receive_sc(participant->kay, &sc_ch);
627
628         dl_list_add(&participant->rxsc_list, &rxsc->list);
629         secy_create_receive_sc(participant->kay, rxsc);
630
631         return peer;
632 }
633
634
635
636 /**
637  *  ieee802_1x_mka_basic_body_present -
638  */
639 static Boolean
640 ieee802_1x_mka_basic_body_present(
641         struct ieee802_1x_mka_participant *participant)
642 {
643         return TRUE;
644 }
645
646
647 /**
648  * ieee802_1x_mka_basic_body_length -
649  */
650 static int
651 ieee802_1x_mka_basic_body_length(struct ieee802_1x_mka_participant *participant)
652 {
653         int length;
654
655         length = sizeof(struct ieee802_1x_mka_basic_body);
656         length += participant->ckn.len;
657         return (length + 0x3) & ~0x3;
658 }
659
660
661 /**
662  * ieee802_1x_mka_encode_basic_body
663  */
664 static int
665 ieee802_1x_mka_encode_basic_body(
666         struct ieee802_1x_mka_participant *participant,
667         struct wpabuf *buf)
668 {
669         struct ieee802_1x_mka_basic_body *body;
670         struct ieee802_1x_kay *kay = participant->kay;
671         unsigned int length = ieee802_1x_mka_basic_body_length(participant);
672
673         body = wpabuf_put(buf, length);
674
675         body->version = kay->mka_version;
676         body->priority = kay->actor_priority;
677         if (participant->is_elected)
678                 body->key_server = participant->is_key_server;
679         else
680                 body->key_server = participant->can_be_key_server;
681
682         body->macsec_desired = kay->macsec_desired;
683         body->macsec_capbility = kay->macsec_capable;
684         set_mka_param_body_len(body, length - MKA_HDR_LEN);
685
686         os_memcpy(body->actor_sci.addr, kay->actor_sci.addr,
687                   sizeof(kay->actor_sci.addr));
688         body->actor_sci.port = kay->actor_sci.port;
689
690         os_memcpy(body->actor_mi, participant->mi, sizeof(body->actor_mi));
691         participant->mn = participant->mn + 1;
692         body->actor_mn = host_to_be32(participant->mn);
693         os_memcpy(body->algo_agility, participant->kay->algo_agility,
694                   sizeof(body->algo_agility));
695
696         os_memcpy(body->ckn, participant->ckn.name, participant->ckn.len);
697
698         ieee802_1x_mka_dump_basic_body(body);
699
700         return 0;
701 }
702
703
704 /**
705  * ieee802_1x_mka_decode_basic_body -
706  */
707 static struct ieee802_1x_mka_participant *
708 ieee802_1x_mka_decode_basic_body(struct ieee802_1x_kay *kay, const u8 *mka_msg,
709                                  size_t msg_len)
710 {
711         struct ieee802_1x_mka_participant *participant;
712         const struct ieee802_1x_mka_basic_body *body;
713         struct ieee802_1x_kay_peer *peer;
714
715         body = (const struct ieee802_1x_mka_basic_body *) mka_msg;
716
717         if (body->version > MKA_VERSION_ID) {
718                 wpa_printf(MSG_DEBUG,
719                            "KaY: peer's version(%d) greater than mka current version(%d)",
720                            body->version, MKA_VERSION_ID);
721         }
722         if (kay->is_obliged_key_server && body->key_server) {
723                 wpa_printf(MSG_DEBUG, "I must be as key server");
724                 return NULL;
725         }
726
727         participant = ieee802_1x_kay_get_participant(kay, body->ckn);
728         if (!participant) {
729                 wpa_printf(MSG_DEBUG, "Peer is not included in my CA");
730                 return NULL;
731         }
732
733         /* If the peer's MI is my MI, I will choose new MI */
734         if (os_memcmp(body->actor_mi, participant->mi, MI_LEN) == 0) {
735                 if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
736                         return NULL;
737                 participant->mn = 0;
738         }
739
740         os_memcpy(participant->current_peer_id.mi, body->actor_mi, MI_LEN);
741         participant->current_peer_id.mn = body->actor_mn;
742         os_memcpy(participant->current_peer_sci.addr, body->actor_sci.addr,
743                   sizeof(participant->current_peer_sci.addr));
744         participant->current_peer_sci.port = body->actor_sci.port;
745
746         /* handler peer */
747         peer = ieee802_1x_kay_get_peer(participant, body->actor_mi);
748         if (!peer) {
749                 /* Check duplicated SCI */
750                 /* TODO: What policy should be applied to detect duplicated SCI
751                  * is active attacker or a valid peer whose MI is be changed?
752                  */
753                 peer = ieee802_1x_kay_get_peer_sci(participant,
754                                                    &body->actor_sci);
755                 if (peer) {
756                         wpa_printf(MSG_WARNING,
757                                    "KaY: duplicated SCI detected, Maybe active attacker");
758                         dl_list_del(&peer->list);
759                         os_free(peer);
760                 }
761
762                 peer = ieee802_1x_kay_create_potential_peer(
763                         participant, body->actor_mi,
764                         be_to_host32(body->actor_mn));
765                 if (!peer)
766                         return NULL;
767
768                 peer->macsec_desired = body->macsec_desired;
769                 peer->macsec_capbility = body->macsec_capbility;
770                 peer->is_key_server = (Boolean) body->key_server;
771                 peer->key_server_priority = body->priority;
772         } else if (peer->mn < be_to_host32(body->actor_mn)) {
773                 peer->mn = be_to_host32(body->actor_mn);
774                 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
775                 peer->macsec_desired = body->macsec_desired;
776                 peer->macsec_capbility = body->macsec_capbility;
777                 peer->is_key_server = (Boolean) body->key_server;
778                 peer->key_server_priority = body->priority;
779         } else {
780                 wpa_printf(MSG_WARNING, "KaY: The peer MN have received");
781                 return NULL;
782         }
783
784         return participant;
785 }
786
787
788 /**
789  * ieee802_1x_mka_live_peer_body_present
790  */
791 static Boolean
792 ieee802_1x_mka_live_peer_body_present(
793         struct ieee802_1x_mka_participant *participant)
794 {
795         return !dl_list_empty(&participant->live_peers);
796 }
797
798
799 /**
800  * ieee802_1x_kay_get_live_peer_length
801  */
802 static int
803 ieee802_1x_mka_get_live_peer_length(
804         struct ieee802_1x_mka_participant *participant)
805 {
806         int len = MKA_HDR_LEN;
807         struct ieee802_1x_kay_peer *peer;
808
809         dl_list_for_each(peer, &participant->live_peers,
810                          struct ieee802_1x_kay_peer, list)
811                 len += sizeof(struct ieee802_1x_mka_peer_id);
812
813         return (len + 0x3) & ~0x3;
814 }
815
816
817 /**
818  * ieee802_1x_mka_encode_live_peer_body -
819  */
820 static int
821 ieee802_1x_mka_encode_live_peer_body(
822         struct ieee802_1x_mka_participant *participant,
823         struct wpabuf *buf)
824 {
825         struct ieee802_1x_mka_peer_body *body;
826         struct ieee802_1x_kay_peer *peer;
827         unsigned int length;
828         struct ieee802_1x_mka_peer_id *body_peer;
829
830         length = ieee802_1x_mka_get_live_peer_length(participant);
831         body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
832
833         body->type = MKA_LIVE_PEER_LIST;
834         set_mka_param_body_len(body, length - MKA_HDR_LEN);
835
836         dl_list_for_each(peer, &participant->live_peers,
837                          struct ieee802_1x_kay_peer, list) {
838                 body_peer = wpabuf_put(buf,
839                                        sizeof(struct ieee802_1x_mka_peer_id));
840                 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
841                 body_peer->mn = host_to_be32(peer->mn);
842                 body_peer++;
843         }
844
845         ieee802_1x_mka_dump_peer_body(body);
846         return 0;
847 }
848
849 /**
850  * ieee802_1x_mka_potential_peer_body_present
851  */
852 static Boolean
853 ieee802_1x_mka_potential_peer_body_present(
854         struct ieee802_1x_mka_participant *participant)
855 {
856         return !dl_list_empty(&participant->potential_peers);
857 }
858
859
860 /**
861  * ieee802_1x_kay_get_potential_peer_length
862  */
863 static int
864 ieee802_1x_mka_get_potential_peer_length(
865         struct ieee802_1x_mka_participant *participant)
866 {
867         int len = MKA_HDR_LEN;
868         struct ieee802_1x_kay_peer *peer;
869
870         dl_list_for_each(peer, &participant->potential_peers,
871                          struct ieee802_1x_kay_peer, list)
872                 len += sizeof(struct ieee802_1x_mka_peer_id);
873
874         return (len + 0x3) & ~0x3;
875 }
876
877
878 /**
879  * ieee802_1x_mka_encode_potential_peer_body -
880  */
881 static int
882 ieee802_1x_mka_encode_potential_peer_body(
883         struct ieee802_1x_mka_participant *participant,
884         struct wpabuf *buf)
885 {
886         struct ieee802_1x_mka_peer_body *body;
887         struct ieee802_1x_kay_peer *peer;
888         unsigned int length;
889         struct ieee802_1x_mka_peer_id *body_peer;
890
891         length = ieee802_1x_mka_get_potential_peer_length(participant);
892         body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
893
894         body->type = MKA_POTENTIAL_PEER_LIST;
895         set_mka_param_body_len(body, length - MKA_HDR_LEN);
896
897         dl_list_for_each(peer, &participant->potential_peers,
898                          struct ieee802_1x_kay_peer, list) {
899                 body_peer = wpabuf_put(buf,
900                                        sizeof(struct ieee802_1x_mka_peer_id));
901                 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
902                 body_peer->mn = host_to_be32(peer->mn);
903                 body_peer++;
904         }
905
906         ieee802_1x_mka_dump_peer_body(body);
907         return 0;
908 }
909
910
911 /**
912  * ieee802_1x_mka_i_in_peerlist -
913  */
914 static Boolean
915 ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant *participant,
916                              const u8 *mka_msg, size_t msg_len)
917 {
918         Boolean included = FALSE;
919         struct ieee802_1x_mka_hdr *hdr;
920         size_t body_len;
921         size_t left_len;
922         u8 body_type;
923         u32 peer_mn;
924         be32 _peer_mn;
925         const u8 *peer_mi;
926         const u8 *pos;
927         size_t i;
928
929         pos = mka_msg;
930         left_len = msg_len;
931         while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
932                 hdr = (struct ieee802_1x_mka_hdr *) pos;
933                 body_len = get_mka_param_body_len(hdr);
934                 body_type = get_mka_param_body_type(hdr);
935
936                 if (body_type != MKA_LIVE_PEER_LIST &&
937                     body_type != MKA_POTENTIAL_PEER_LIST)
938                         goto SKIP_PEER;
939
940                 ieee802_1x_mka_dump_peer_body(
941                         (struct ieee802_1x_mka_peer_body *)pos);
942
943                 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
944                         wpa_printf(MSG_ERROR,
945                                    "KaY: MKA Peer Packet Body Length (%d bytes) is less than the Parameter Set Header Length (%d bytes) + the Parameter Set Body Length (%d bytes) + %d bytes of ICV",
946                                    (int) left_len, (int) MKA_HDR_LEN,
947                                    (int) body_len, DEFAULT_ICV_LEN);
948                         goto SKIP_PEER;
949                 }
950
951                 if ((body_len % 16) != 0) {
952                         wpa_printf(MSG_ERROR,
953                                    "KaY: MKA Peer Packet Body Length (%d bytes) should multiple of 16 octets",
954                                    (int) body_len);
955                         goto SKIP_PEER;
956                 }
957
958                 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
959                         peer_mi = MKA_HDR_LEN + pos + i;
960                         os_memcpy(&_peer_mn, peer_mi + MI_LEN,
961                                   sizeof(_peer_mn));
962                         peer_mn = be_to_host32(_peer_mn);
963                         if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0 &&
964                             peer_mn == participant->mn) {
965                                 included = TRUE;
966                                 break;
967                         }
968                 }
969
970                 if (included)
971                         return TRUE;
972
973 SKIP_PEER:
974                 left_len -= body_len + MKA_HDR_LEN;
975                 pos += body_len + MKA_HDR_LEN;
976         }
977
978         return FALSE;
979 }
980
981
982 /**
983  * ieee802_1x_mka_decode_live_peer_body -
984  */
985 static int ieee802_1x_mka_decode_live_peer_body(
986         struct ieee802_1x_mka_participant *participant,
987         const u8 *peer_msg, size_t msg_len)
988 {
989         const struct ieee802_1x_mka_hdr *hdr;
990         struct ieee802_1x_kay_peer *peer;
991         size_t body_len;
992         u32 peer_mn;
993         be32 _peer_mn;
994         const u8 *peer_mi;
995         size_t i;
996         Boolean is_included;
997
998         is_included = ieee802_1x_kay_is_in_live_peer(
999                 participant, participant->current_peer_id.mi);
1000
1001         hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
1002         body_len = get_mka_param_body_len(hdr);
1003         if (body_len % 16 != 0) {
1004                 wpa_printf(MSG_ERROR,
1005                            "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1006                            body_len);
1007                 return -1;
1008         }
1009
1010         for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1011                 peer_mi = MKA_HDR_LEN + peer_msg + i;
1012                 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1013                 peer_mn = be_to_host32(_peer_mn);
1014
1015                 /* it is myself */
1016                 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1017                         /* My message id is used by other participant */
1018                         if (peer_mn > participant->mn) {
1019                                 if (os_get_random(participant->mi,
1020                                                   sizeof(participant->mi)) < 0)
1021                                         wpa_printf(MSG_DEBUG,
1022                                                    "KaY: Could not update mi");
1023                                 participant->mn = 0;
1024                         }
1025                         continue;
1026                 }
1027                 if (!is_included)
1028                         continue;
1029
1030                 peer = ieee802_1x_kay_get_peer(participant, peer_mi);
1031                 if (NULL != peer) {
1032                         peer->mn = peer_mn;
1033                         peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
1034                 } else {
1035                         if (!ieee802_1x_kay_create_potential_peer(
1036                                 participant, peer_mi, peer_mn)) {
1037                                 return -1;
1038                         }
1039                 }
1040         }
1041
1042         return 0;
1043 }
1044
1045
1046 /**
1047  * ieee802_1x_mka_decode_potential_peer_body -
1048  */
1049 static int
1050 ieee802_1x_mka_decode_potential_peer_body(
1051         struct ieee802_1x_mka_participant *participant,
1052         const u8 *peer_msg, size_t msg_len)
1053 {
1054         struct ieee802_1x_mka_hdr *hdr;
1055         size_t body_len;
1056         u32 peer_mn;
1057         be32 _peer_mn;
1058         const u8 *peer_mi;
1059         size_t i;
1060
1061         hdr = (struct ieee802_1x_mka_hdr *) peer_msg;
1062         body_len = get_mka_param_body_len(hdr);
1063         if (body_len % 16 != 0) {
1064                 wpa_printf(MSG_ERROR,
1065                            "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1066                            body_len);
1067                 return -1;
1068         }
1069
1070         for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1071                 peer_mi = MKA_HDR_LEN + peer_msg + i;
1072                 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1073                 peer_mn = be_to_host32(_peer_mn);
1074
1075                 /* it is myself */
1076                 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1077                         /* My message id is used by other participant */
1078                         if (peer_mn > participant->mn) {
1079                                 if (os_get_random(participant->mi,
1080                                                   sizeof(participant->mi)) < 0)
1081                                         wpa_printf(MSG_DEBUG,
1082                                                    "KaY: Could not update mi");
1083                                 participant->mn = 0;
1084                         }
1085                         continue;
1086                 }
1087         }
1088
1089         return 0;
1090 }
1091
1092
1093 /**
1094  * ieee802_1x_mka_sak_use_body_present
1095  */
1096 static Boolean
1097 ieee802_1x_mka_sak_use_body_present(
1098         struct ieee802_1x_mka_participant *participant)
1099 {
1100         if (participant->to_use_sak)
1101                 return TRUE;
1102         else
1103                 return FALSE;
1104 }
1105
1106
1107 /**
1108  * ieee802_1x_mka_get_sak_use_length
1109  */
1110 static int
1111 ieee802_1x_mka_get_sak_use_length(
1112         struct ieee802_1x_mka_participant *participant)
1113 {
1114         int length = MKA_HDR_LEN;
1115
1116         if (participant->kay->macsec_desired && participant->advised_desired)
1117                 length = sizeof(struct ieee802_1x_mka_sak_use_body);
1118         else
1119                 length = MKA_HDR_LEN;
1120
1121         length = (length + 0x3) & ~0x3;
1122
1123         return length;
1124 }
1125
1126
1127 /**
1128  *
1129  */
1130 static u32
1131 ieee802_1x_mka_get_lpn(struct ieee802_1x_mka_participant *principal,
1132                        struct ieee802_1x_mka_ki *ki)
1133 {
1134         struct receive_sa *rxsa;
1135         struct receive_sc *rxsc;
1136         u32 lpn = 0;
1137
1138         dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
1139                 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
1140                 {
1141                         if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
1142                                 secy_get_receive_lowest_pn(principal->kay,
1143                                                            rxsa);
1144
1145                                 lpn = lpn > rxsa->lowest_pn ?
1146                                         lpn : rxsa->lowest_pn;
1147                                 break;
1148                         }
1149                 }
1150         }
1151
1152         if (lpn == 0)
1153                 lpn = 1;
1154
1155         return lpn;
1156 }
1157
1158
1159 /**
1160  * ieee802_1x_mka_encode_sak_use_body -
1161  */
1162 static int
1163 ieee802_1x_mka_encode_sak_use_body(
1164         struct ieee802_1x_mka_participant *participant,
1165         struct wpabuf *buf)
1166 {
1167         struct ieee802_1x_mka_sak_use_body *body;
1168         unsigned int length;
1169         u32 pn = 1;
1170
1171         length = ieee802_1x_mka_get_sak_use_length(participant);
1172         body = wpabuf_put(buf, length);
1173
1174         body->type = MKA_SAK_USE;
1175         set_mka_param_body_len(body, length - MKA_HDR_LEN);
1176
1177         if (length == MKA_HDR_LEN) {
1178                 body->ptx = TRUE;
1179                 body->prx = TRUE;
1180                 body->lan = 0;
1181                 body->lrx = FALSE;
1182                 body->ltx = FALSE;
1183                 body->delay_protect = FALSE;
1184                 return 0;
1185         }
1186
1187         /* data protect, lowest accept packet number */
1188         body->delay_protect = participant->kay->macsec_replay_protect;
1189         pn = ieee802_1x_mka_get_lpn(participant, &participant->lki);
1190         if (pn > participant->kay->pn_exhaustion) {
1191                 wpa_printf(MSG_WARNING, "KaY: My LPN exhaustion");
1192                 if (participant->is_key_server)
1193                         participant->new_sak = TRUE;
1194         }
1195
1196         body->llpn = host_to_be32(pn);
1197         pn = ieee802_1x_mka_get_lpn(participant, &participant->oki);
1198         body->olpn = host_to_be32(pn);
1199
1200         /* plain tx, plain rx */
1201         if (participant->kay->macsec_protect)
1202                 body->ptx = FALSE;
1203         else
1204                 body->ptx = TRUE;
1205
1206         if (participant->kay->macsec_validate == Strict)
1207                 body->prx = FALSE;
1208         else
1209                 body->prx = TRUE;
1210
1211         /* latest key: rx, tx, key server member identifier key number */
1212         body->lan = participant->lan;
1213         os_memcpy(body->lsrv_mi, participant->lki.mi,
1214                   sizeof(body->lsrv_mi));
1215         body->lkn = host_to_be32(participant->lki.kn);
1216         body->lrx = participant->lrx;
1217         body->ltx = participant->ltx;
1218
1219         /* old key: rx, tx, key server member identifier key number */
1220         body->oan = participant->oan;
1221         if (participant->oki.kn != participant->lki.kn &&
1222             participant->oki.kn != 0) {
1223                 body->otx = TRUE;
1224                 body->orx = TRUE;
1225                 os_memcpy(body->osrv_mi, participant->oki.mi,
1226                           sizeof(body->osrv_mi));
1227                 body->okn = host_to_be32(participant->oki.kn);
1228         } else {
1229                 body->otx = FALSE;
1230                 body->orx = FALSE;
1231         }
1232
1233         /* set CP's variable */
1234         if (body->ltx) {
1235                 if (!participant->kay->tx_enable)
1236                         participant->kay->tx_enable = TRUE;
1237
1238                 if (!participant->kay->port_enable)
1239                         participant->kay->port_enable = TRUE;
1240         }
1241         if (body->lrx) {
1242                 if (!participant->kay->rx_enable)
1243                         participant->kay->rx_enable = TRUE;
1244         }
1245
1246         ieee802_1x_mka_dump_sak_use_body(body);
1247         return 0;
1248 }
1249
1250
1251 /**
1252  * ieee802_1x_mka_decode_sak_use_body -
1253  */
1254 static int
1255 ieee802_1x_mka_decode_sak_use_body(
1256         struct ieee802_1x_mka_participant *participant,
1257         const u8 *mka_msg, size_t msg_len)
1258 {
1259         struct ieee802_1x_mka_hdr *hdr;
1260         struct ieee802_1x_mka_sak_use_body *body;
1261         struct ieee802_1x_kay_peer *peer;
1262         struct transmit_sa *txsa;
1263         struct data_key *sa_key = NULL;
1264         size_t body_len;
1265         struct ieee802_1x_mka_ki ki;
1266         u32 lpn;
1267         Boolean all_receiving;
1268         Boolean founded;
1269
1270         if (!participant->principal) {
1271                 wpa_printf(MSG_WARNING, "KaY: Participant is not principal");
1272                 return -1;
1273         }
1274         peer = ieee802_1x_kay_get_live_peer(participant,
1275                                             participant->current_peer_id.mi);
1276         if (!peer) {
1277                 wpa_printf(MSG_WARNING, "KaY: the peer is not my live peer");
1278                 return -1;
1279         }
1280
1281         hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1282         body_len = get_mka_param_body_len(hdr);
1283         body = (struct ieee802_1x_mka_sak_use_body *) mka_msg;
1284         ieee802_1x_mka_dump_sak_use_body(body);
1285
1286         if ((body_len != 0) && (body_len < 40)) {
1287                 wpa_printf(MSG_ERROR,
1288                            "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 40, or more octets",
1289                            (int) body_len);
1290                 return -1;
1291         }
1292
1293         /* TODO: what action should I take when peer does not support MACsec */
1294         if (body_len == 0) {
1295                 wpa_printf(MSG_WARNING, "KaY: Peer does not support MACsec");
1296                 return 0;
1297         }
1298
1299         /* TODO: when the plain tx or rx of peer is true, should I change
1300          * the attribute of controlled port
1301          */
1302         if (body->prx)
1303                 wpa_printf(MSG_WARNING, "KaY: peer's plain rx are TRUE");
1304
1305         if (body->ptx)
1306                 wpa_printf(MSG_WARNING, "KaY: peer's plain tx are TRUE");
1307
1308         /* check latest key is valid */
1309         if (body->ltx || body->lrx) {
1310                 founded = FALSE;
1311                 os_memcpy(ki.mi, body->lsrv_mi, sizeof(ki.mi));
1312                 ki.kn = be_to_host32(body->lkn);
1313                 dl_list_for_each(sa_key, &participant->sak_list,
1314                                  struct data_key, list) {
1315                         if (is_ki_equal(&sa_key->key_identifier, &ki)) {
1316                                 founded = TRUE;
1317                                 break;
1318                         }
1319                 }
1320                 if (!founded) {
1321                         wpa_printf(MSG_WARNING, "KaY: Latest key is invalid");
1322                         return -1;
1323                 }
1324                 if (os_memcmp(participant->lki.mi, body->lsrv_mi,
1325                               sizeof(participant->lki.mi)) == 0 &&
1326                     be_to_host32(body->lkn) == participant->lki.kn &&
1327                     body->lan == participant->lan) {
1328                         peer->sak_used = TRUE;
1329                 }
1330                 if (body->ltx && peer->is_key_server) {
1331                         ieee802_1x_cp_set_servertransmitting(
1332                                 participant->kay->cp, TRUE);
1333                         ieee802_1x_cp_sm_step(participant->kay->cp);
1334                 }
1335         }
1336
1337         /* check old key is valid */
1338         if (body->otx || body->orx) {
1339                 if (os_memcmp(participant->oki.mi, body->osrv_mi,
1340                               sizeof(participant->oki.mi)) != 0 ||
1341                     be_to_host32(body->okn) != participant->oki.kn ||
1342                     body->oan != participant->oan) {
1343                         wpa_printf(MSG_WARNING, "KaY: Old key is invalid");
1344                         return -1;
1345                 }
1346         }
1347
1348         /* TODO: how to set the MACsec hardware when delay_protect is true */
1349         if (body->delay_protect &&
1350             (!be_to_host32(body->llpn) || !be_to_host32(body->olpn))) {
1351                 wpa_printf(MSG_WARNING,
1352                            "KaY: Lowest packet number should greater than 0 when delay_protect is TRUE");
1353                 return -1;
1354         }
1355
1356         /* check all live peer have used the sak for receiving sa */
1357         all_receiving = TRUE;
1358         dl_list_for_each(peer, &participant->live_peers,
1359                          struct ieee802_1x_kay_peer, list) {
1360                 if (!peer->sak_used) {
1361                         all_receiving = FALSE;
1362                         break;
1363                 }
1364         }
1365         if (all_receiving) {
1366                 participant->to_dist_sak = FALSE;
1367                 ieee802_1x_cp_set_allreceiving(participant->kay->cp, TRUE);
1368                 ieee802_1x_cp_sm_step(participant->kay->cp);
1369         }
1370
1371         /* if i'm key server, and detects peer member pn exhaustion, rekey.*/
1372         lpn = be_to_host32(body->llpn);
1373         if (lpn > participant->kay->pn_exhaustion) {
1374                 if (participant->is_key_server) {
1375                         participant->new_sak = TRUE;
1376                         wpa_printf(MSG_WARNING, "KaY: Peer LPN exhaustion");
1377                 }
1378         }
1379
1380         founded = FALSE;
1381         dl_list_for_each(txsa, &participant->txsc->sa_list,
1382                          struct transmit_sa, list) {
1383                 if (sa_key != NULL && txsa->pkey == sa_key) {
1384                         founded = TRUE;
1385                         break;
1386                 }
1387         }
1388         if (!founded) {
1389                 wpa_printf(MSG_WARNING, "KaY: Can't find txsa");
1390                 return -1;
1391         }
1392
1393         /* FIXME: Secy creates txsa with default npn. If MKA detected Latest Key
1394          * npn is larger than txsa's npn, set it to txsa.
1395          */
1396         secy_get_transmit_next_pn(participant->kay, txsa);
1397         if (lpn > txsa->next_pn) {
1398                 secy_set_transmit_next_pn(participant->kay, txsa);
1399                 wpa_printf(MSG_INFO, "KaY: update lpn =0x%x", lpn);
1400         }
1401
1402         return 0;
1403 }
1404
1405
1406 /**
1407  * ieee802_1x_mka_dist_sak_body_present
1408  */
1409 static Boolean
1410 ieee802_1x_mka_dist_sak_body_present(
1411         struct ieee802_1x_mka_participant *participant)
1412 {
1413         if (!participant->to_dist_sak || !participant->new_key)
1414                 return FALSE;
1415
1416         return TRUE;
1417 }
1418
1419
1420 /**
1421  * ieee802_1x_kay_get_dist_sak_length
1422  */
1423 static int
1424 ieee802_1x_mka_get_dist_sak_length(
1425         struct ieee802_1x_mka_participant *participant)
1426 {
1427         int length;
1428         int cs_index = participant->kay->macsec_csindex;
1429
1430         if (participant->advised_desired) {
1431                 length = sizeof(struct ieee802_1x_mka_dist_sak_body);
1432                 if (cs_index != DEFAULT_CS_INDEX)
1433                         length += CS_ID_LEN;
1434
1435                 length += cipher_suite_tbl[cs_index].sak_len + 8;
1436         } else {
1437                 length = MKA_HDR_LEN;
1438         }
1439         length = (length + 0x3) & ~0x3;
1440
1441         return length;
1442 }
1443
1444
1445 /**
1446  * ieee802_1x_mka_encode_dist_sak_body -
1447  */
1448 static int
1449 ieee802_1x_mka_encode_dist_sak_body(
1450         struct ieee802_1x_mka_participant *participant,
1451         struct wpabuf *buf)
1452 {
1453         struct ieee802_1x_mka_dist_sak_body *body;
1454         struct data_key *sak;
1455         unsigned int length;
1456         int cs_index;
1457         int sak_pos;
1458
1459         length = ieee802_1x_mka_get_dist_sak_length(participant);
1460         body = wpabuf_put(buf, length);
1461         body->type = MKA_DISTRIBUTED_SAK;
1462         set_mka_param_body_len(body, length - MKA_HDR_LEN);
1463         if (length == MKA_HDR_LEN) {
1464                 body->confid_offset = 0;
1465                 body->dan = 0;
1466                 return 0;
1467         }
1468
1469         sak = participant->new_key;
1470         body->confid_offset = sak->confidentiality_offset;
1471         body->dan = sak->an;
1472         body->kn = host_to_be32(sak->key_identifier.kn);
1473         cs_index = participant->kay->macsec_csindex;
1474         sak_pos = 0;
1475         if (cs_index != DEFAULT_CS_INDEX) {
1476                 os_memcpy(body->sak, cipher_suite_tbl[cs_index].id, CS_ID_LEN);
1477                 sak_pos = CS_ID_LEN;
1478         }
1479         if (aes_wrap(participant->kek.key, 16,
1480                      cipher_suite_tbl[cs_index].sak_len / 8,
1481                      sak->key, body->sak + sak_pos)) {
1482                 wpa_printf(MSG_ERROR, "KaY: AES wrap failed");
1483                 return -1;
1484         }
1485
1486         ieee802_1x_mka_dump_dist_sak_body(body);
1487
1488         return 0;
1489 }
1490
1491
1492 /**
1493  * ieee802_1x_kay_init_data_key -
1494  */
1495 static struct data_key *
1496 ieee802_1x_kay_init_data_key(const struct key_conf *conf)
1497 {
1498         struct data_key *pkey;
1499
1500         if (!conf)
1501                 return NULL;
1502
1503         pkey = os_zalloc(sizeof(*pkey));
1504         if (pkey == NULL) {
1505                 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1506                 return NULL;
1507         }
1508
1509         pkey->key = os_zalloc(conf->key_len);
1510         if (pkey->key == NULL) {
1511                 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1512                 os_free(pkey);
1513                 return NULL;
1514         }
1515
1516         os_memcpy(pkey->key, conf->key, conf->key_len);
1517         os_memcpy(&pkey->key_identifier, &conf->ki,
1518                   sizeof(pkey->key_identifier));
1519         pkey->confidentiality_offset = conf->offset;
1520         pkey->an = conf->an;
1521         pkey->transmits = conf->tx;
1522         pkey->receives = conf->rx;
1523         os_get_time(&pkey->created_time);
1524
1525         pkey->user = 1;
1526
1527         return pkey;
1528 }
1529
1530
1531 /**
1532  * ieee802_1x_kay_decode_dist_sak_body -
1533  */
1534 static int
1535 ieee802_1x_mka_decode_dist_sak_body(
1536         struct ieee802_1x_mka_participant *participant,
1537         const u8 *mka_msg, size_t msg_len)
1538 {
1539         struct ieee802_1x_mka_hdr *hdr;
1540         struct ieee802_1x_mka_dist_sak_body *body;
1541         struct ieee802_1x_kay_peer *peer;
1542         struct macsec_ciphersuite *cs;
1543         size_t body_len;
1544         struct key_conf *conf;
1545         struct data_key *sa_key = NULL;
1546         struct ieee802_1x_mka_ki sak_ki;
1547         int sak_len;
1548         u8 *wrap_sak;
1549         u8 *unwrap_sak;
1550
1551         hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1552         body_len = get_mka_param_body_len(hdr);
1553         if ((body_len != 0) && (body_len != 28) && (body_len < 36)) {
1554                 wpa_printf(MSG_ERROR,
1555                            "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 28, 36, or more octets",
1556                            (int) body_len);
1557                 return -1;
1558         }
1559
1560         if (!participant->principal) {
1561                 wpa_printf(MSG_ERROR,
1562                            "KaY: I can't accept the distributed SAK as I am not principal");
1563                 return -1;
1564         }
1565         if (participant->is_key_server) {
1566                 wpa_printf(MSG_ERROR,
1567                            "KaY: I can't accept the distributed SAK as myself is key server ");
1568                 return -1;
1569         }
1570         if (!participant->kay->macsec_desired ||
1571             participant->kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
1572                 wpa_printf(MSG_ERROR,
1573                            "KaY: I am not MACsec-desired or without MACsec capable");
1574                 return -1;
1575         }
1576
1577         peer = ieee802_1x_kay_get_live_peer(participant,
1578                                             participant->current_peer_id.mi);
1579         if (!peer) {
1580                 wpa_printf(MSG_ERROR,
1581                            "KaY: The key server is not in my live peers list");
1582                 return -1;
1583         }
1584         if (os_memcmp(&participant->kay->key_server_sci,
1585                       &peer->sci, sizeof(struct ieee802_1x_mka_sci)) != 0) {
1586                 wpa_printf(MSG_ERROR, "KaY: The key server is not elected");
1587                 return -1;
1588         }
1589         if (body_len == 0) {
1590                 participant->kay->authenticated = TRUE;
1591                 participant->kay->secured = FALSE;
1592                 participant->kay->failed = FALSE;
1593                 participant->advised_desired = FALSE;
1594                 ieee802_1x_cp_connect_authenticated(participant->kay->cp);
1595                 ieee802_1x_cp_sm_step(participant->kay->cp);
1596                 wpa_printf(MSG_WARNING, "KaY:The Key server advise no MACsec");
1597                 participant->to_use_sak = TRUE;
1598                 return 0;
1599         }
1600         participant->advised_desired = TRUE;
1601         participant->kay->authenticated = FALSE;
1602         participant->kay->secured = TRUE;
1603         participant->kay->failed = FALSE;
1604         ieee802_1x_cp_connect_secure(participant->kay->cp);
1605         ieee802_1x_cp_sm_step(participant->kay->cp);
1606
1607         body = (struct ieee802_1x_mka_dist_sak_body *)mka_msg;
1608         ieee802_1x_mka_dump_dist_sak_body(body);
1609         dl_list_for_each(sa_key, &participant->sak_list, struct data_key, list)
1610         {
1611                 if (os_memcmp(sa_key->key_identifier.mi,
1612                               participant->current_peer_id.mi, MI_LEN) == 0 &&
1613                     sa_key->key_identifier.kn == be_to_host32(body->kn)) {
1614                         wpa_printf(MSG_WARNING, "KaY:The Key has installed");
1615                         return 0;
1616                 }
1617         }
1618         if (body_len == 28) {
1619                 sak_len = DEFAULT_SA_KEY_LEN;
1620                 wrap_sak =  body->sak;
1621                 participant->kay->macsec_csindex = DEFAULT_CS_INDEX;
1622         } else {
1623                 cs = ieee802_1x_kay_get_cipher_suite(participant, body->sak);
1624                 if (!cs) {
1625                         wpa_printf(MSG_ERROR,
1626                                    "KaY: I can't support the Cipher Suite advised by key server");
1627                         return -1;
1628                 }
1629                 sak_len = cs->sak_len;
1630                 wrap_sak = body->sak + CS_ID_LEN;
1631                 participant->kay->macsec_csindex = cs->index;
1632         }
1633
1634         unwrap_sak = os_zalloc(sak_len);
1635         if (!unwrap_sak) {
1636                 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1637                 return -1;
1638         }
1639         if (aes_unwrap(participant->kek.key, 16, sak_len >> 3, wrap_sak,
1640                        unwrap_sak)) {
1641                 wpa_printf(MSG_ERROR, "KaY: AES unwrap failed");
1642                 os_free(unwrap_sak);
1643                 return -1;
1644         }
1645         wpa_hexdump(MSG_DEBUG, "\tAES Key Unwrap of SAK:", unwrap_sak, sak_len);
1646
1647         conf = os_zalloc(sizeof(*conf));
1648         if (!conf) {
1649                 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1650                 os_free(unwrap_sak);
1651                 return -1;
1652         }
1653         conf->key_len = sak_len;
1654
1655         conf->key = os_zalloc(conf->key_len);
1656         if (!conf->key) {
1657                 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1658                 os_free(unwrap_sak);
1659                 os_free(conf);
1660                 return -1;
1661         }
1662
1663         os_memcpy(conf->key, unwrap_sak, conf->key_len);
1664
1665         os_memcpy(&sak_ki.mi, &participant->current_peer_id.mi,
1666                   sizeof(sak_ki.mi));
1667         sak_ki.kn = be_to_host32(body->kn);
1668
1669         os_memcpy(conf->ki.mi, sak_ki.mi, MI_LEN);
1670         conf->ki.kn = sak_ki.kn;
1671         conf->an = body->dan;
1672         conf->offset = body->confid_offset;
1673         conf->rx = TRUE;
1674         conf->tx = TRUE;
1675
1676         sa_key = ieee802_1x_kay_init_data_key(conf);
1677         if (!sa_key) {
1678                 os_free(unwrap_sak);
1679                 os_free(conf->key);
1680                 os_free(conf);
1681                 return -1;
1682         }
1683
1684         dl_list_add(&participant->sak_list, &sa_key->list);
1685
1686         ieee802_1x_cp_set_ciphersuite(
1687                 participant->kay->cp,
1688                 cipher_suite_tbl[participant->kay->macsec_csindex].id);
1689         ieee802_1x_cp_sm_step(participant->kay->cp);
1690         ieee802_1x_cp_set_offset(participant->kay->cp, body->confid_offset);
1691         ieee802_1x_cp_sm_step(participant->kay->cp);
1692         ieee802_1x_cp_set_distributedki(participant->kay->cp, &sak_ki);
1693         ieee802_1x_cp_set_distributedan(participant->kay->cp, body->dan);
1694         ieee802_1x_cp_signal_newsak(participant->kay->cp);
1695         ieee802_1x_cp_sm_step(participant->kay->cp);
1696
1697         participant->to_use_sak = TRUE;
1698
1699         os_free(unwrap_sak);
1700         os_free(conf->key);
1701         os_free(conf);
1702
1703         return 0;
1704 }
1705
1706
1707 /**
1708  * ieee802_1x_mka_icv_body_present
1709  */
1710 static Boolean
1711 ieee802_1x_mka_icv_body_present(struct ieee802_1x_mka_participant *participant)
1712 {
1713         return TRUE;
1714 }
1715
1716
1717 /**
1718  * ieee802_1x_kay_get_icv_length
1719  */
1720 static int
1721 ieee802_1x_mka_get_icv_length(struct ieee802_1x_mka_participant *participant)
1722 {
1723         int length;
1724
1725         length = sizeof(struct ieee802_1x_mka_icv_body);
1726         length += mka_alg_tbl[participant->kay->mka_algindex].icv_len;
1727
1728         return (length + 0x3) & ~0x3;
1729 }
1730
1731
1732 /**
1733  * ieee802_1x_mka_encode_icv_body -
1734  */
1735 static int
1736 ieee802_1x_mka_encode_icv_body(struct ieee802_1x_mka_participant *participant,
1737                                struct wpabuf *buf)
1738 {
1739         struct ieee802_1x_mka_icv_body *body;
1740         unsigned int length;
1741         u8 cmac[MAX_ICV_LEN];
1742
1743         length = ieee802_1x_mka_get_icv_length(participant);
1744         if (length != DEFAULT_ICV_LEN)  {
1745                 body = wpabuf_put(buf, MKA_HDR_LEN);
1746                 body->type = MKA_ICV_INDICATOR;
1747                 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1748         }
1749
1750         if (mka_alg_tbl[participant->kay->mka_algindex].icv_hash(
1751                     participant->ick.key, wpabuf_head(buf), buf->used, cmac)) {
1752                 wpa_printf(MSG_ERROR, "KaY, omac1_aes_128 failed");
1753                 return -1;
1754         }
1755
1756         if (length != DEFAULT_ICV_LEN)  {
1757                 os_memcpy(wpabuf_put(buf, length - MKA_HDR_LEN), cmac,
1758                           length - MKA_HDR_LEN);
1759         } else {
1760                 os_memcpy(wpabuf_put(buf, length), cmac, length);
1761         }
1762
1763         return 0;
1764 }
1765
1766 /**
1767  * ieee802_1x_mka_decode_icv_body -
1768  */
1769 static u8 *
1770 ieee802_1x_mka_decode_icv_body(struct ieee802_1x_mka_participant *participant,
1771                                const u8 *mka_msg, size_t msg_len)
1772 {
1773         struct ieee802_1x_mka_hdr *hdr;
1774         struct ieee802_1x_mka_icv_body *body;
1775         size_t body_len;
1776         size_t left_len;
1777         u8 body_type;
1778         const u8 *pos;
1779
1780         pos = mka_msg;
1781         left_len = msg_len;
1782         while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
1783                 hdr = (struct ieee802_1x_mka_hdr *) pos;
1784                 body_len = get_mka_param_body_len(hdr);
1785                 body_type = get_mka_param_body_type(hdr);
1786
1787                 if (left_len < (body_len + MKA_HDR_LEN))
1788                         break;
1789
1790                 if (body_type != MKA_ICV_INDICATOR) {
1791                         left_len -= MKA_HDR_LEN + body_len;
1792                         pos += MKA_HDR_LEN + body_len;
1793                         continue;
1794                 }
1795
1796                 body = (struct ieee802_1x_mka_icv_body *)pos;
1797                 if (body_len
1798                         < mka_alg_tbl[participant->kay->mka_algindex].icv_len) {
1799                         return NULL;
1800                 }
1801
1802                 return body->icv;
1803         }
1804
1805         return (u8 *) (mka_msg + msg_len - DEFAULT_ICV_LEN);
1806 }
1807
1808
1809 /**
1810  * ieee802_1x_mka_decode_dist_cak_body-
1811  */
1812 static int
1813 ieee802_1x_mka_decode_dist_cak_body(
1814         struct ieee802_1x_mka_participant *participant,
1815         const u8 *mka_msg, size_t msg_len)
1816 {
1817         struct ieee802_1x_mka_hdr *hdr;
1818         size_t body_len;
1819
1820         hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1821         body_len = get_mka_param_body_len(hdr);
1822         if (body_len < 28) {
1823                 wpa_printf(MSG_ERROR,
1824                            "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 28 or more octets",
1825                            (int) body_len);
1826                 return -1;
1827         }
1828
1829         return 0;
1830 }
1831
1832
1833 /**
1834  * ieee802_1x_mka_decode_kmd_body -
1835  */
1836 static int
1837 ieee802_1x_mka_decode_kmd_body(
1838         struct ieee802_1x_mka_participant *participant,
1839         const u8 *mka_msg, size_t msg_len)
1840 {
1841         struct ieee802_1x_mka_hdr *hdr;
1842         size_t body_len;
1843
1844         hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1845         body_len = get_mka_param_body_len(hdr);
1846         if (body_len < 5) {
1847                 wpa_printf(MSG_ERROR,
1848                            "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 5 or more octets",
1849                            (int) body_len);
1850                 return -1;
1851         }
1852
1853         return 0;
1854 }
1855
1856
1857 /**
1858  * ieee802_1x_mka_decode_announce_body -
1859  */
1860 static int ieee802_1x_mka_decode_announce_body(
1861         struct ieee802_1x_mka_participant *participant,
1862         const u8 *mka_msg, size_t msg_len)
1863 {
1864         return 0;
1865 }
1866
1867
1868 static struct mka_param_body_handler mak_body_handler[] = {
1869         /* basic parameter set */
1870         {
1871                 ieee802_1x_mka_encode_basic_body,
1872                 NULL,
1873                 ieee802_1x_mka_basic_body_length,
1874                 ieee802_1x_mka_basic_body_present
1875         },
1876
1877         /* live peer list parameter set */
1878         {
1879                 ieee802_1x_mka_encode_live_peer_body,
1880                 ieee802_1x_mka_decode_live_peer_body,
1881                 ieee802_1x_mka_get_live_peer_length,
1882                 ieee802_1x_mka_live_peer_body_present
1883         },
1884
1885         /* potential peer list parameter set */
1886         {
1887                 ieee802_1x_mka_encode_potential_peer_body,
1888                 ieee802_1x_mka_decode_potential_peer_body,
1889                 ieee802_1x_mka_get_potential_peer_length,
1890                 ieee802_1x_mka_potential_peer_body_present
1891         },
1892
1893         /* sak use parameter set */
1894         {
1895                 ieee802_1x_mka_encode_sak_use_body,
1896                 ieee802_1x_mka_decode_sak_use_body,
1897                 ieee802_1x_mka_get_sak_use_length,
1898                 ieee802_1x_mka_sak_use_body_present
1899         },
1900
1901         /* distribute sak parameter set */
1902         {
1903                 ieee802_1x_mka_encode_dist_sak_body,
1904                 ieee802_1x_mka_decode_dist_sak_body,
1905                 ieee802_1x_mka_get_dist_sak_length,
1906                 ieee802_1x_mka_dist_sak_body_present
1907         },
1908
1909         /* distribute cak parameter set */
1910         {
1911                 NULL,
1912                 ieee802_1x_mka_decode_dist_cak_body,
1913                 NULL,
1914                 NULL
1915         },
1916
1917         /* kmd parameter set */
1918         {
1919                 NULL,
1920                 ieee802_1x_mka_decode_kmd_body,
1921                 NULL,
1922                 NULL
1923         },
1924
1925         /* announce parameter set */
1926         {
1927                 NULL,
1928                 ieee802_1x_mka_decode_announce_body,
1929                 NULL,
1930                 NULL
1931         },
1932
1933         /* icv parameter set */
1934         {
1935                 ieee802_1x_mka_encode_icv_body,
1936                 NULL,
1937                 ieee802_1x_mka_get_icv_length,
1938                 ieee802_1x_mka_icv_body_present
1939         },
1940 };
1941
1942
1943 /**
1944  * ieee802_1x_kay_deinit_data_key -
1945  */
1946 static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
1947 {
1948         if (!pkey)
1949                 return;
1950
1951         pkey->user--;
1952         if (pkey->user > 1)
1953                 return;
1954
1955         dl_list_del(&pkey->list);
1956         os_free(pkey->key);
1957         os_free(pkey);
1958 }
1959
1960
1961 /**
1962  * ieee802_1x_kay_generate_new_sak -
1963  */
1964 static int
1965 ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
1966 {
1967         struct data_key *sa_key = NULL;
1968         struct key_conf *conf;
1969         struct ieee802_1x_kay_peer *peer;
1970         struct ieee802_1x_kay *kay = participant->kay;
1971         int ctx_len, ctx_offset;
1972         u8 *context;
1973
1974         /* check condition for generating a fresh SAK:
1975          * must have one live peer
1976          * and MKA life time elapse since last distribution
1977          * or potential peer is empty
1978          */
1979         if (dl_list_empty(&participant->live_peers)) {
1980                 wpa_printf(MSG_ERROR,
1981                            "KaY: Live peers list must not empty when generating fresh SAK");
1982                 return -1;
1983         }
1984
1985         /* FIXME: A fresh SAK not generated until
1986          * the live peer list contains at least one peer and
1987          * MKA life time has elapsed since the prior SAK was first distributed,
1988          * or the Key server's potential peer is empty
1989          * but I can't understand the second item, so
1990          * here only check first item and ingore
1991          *   && (!dl_list_empty(&participant->potential_peers))) {
1992          */
1993         if ((time(NULL) - kay->dist_time) < MKA_LIFE_TIME / 1000) {
1994                 wpa_printf(MSG_ERROR,
1995                            "KaY: Life time have not elapsed since prior SAK distributed");
1996                 return -1;
1997         }
1998
1999         conf = os_zalloc(sizeof(*conf));
2000         if (!conf) {
2001                 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
2002                 return -1;
2003         }
2004         conf->key_len = cipher_suite_tbl[kay->macsec_csindex].sak_len;
2005
2006         conf->key = os_zalloc(conf->key_len);
2007         if (!conf->key) {
2008                 os_free(conf);
2009                 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
2010                 return -1;
2011         }
2012
2013         ctx_len = conf->key_len + sizeof(kay->dist_kn);
2014         dl_list_for_each(peer, &participant->live_peers,
2015                          struct ieee802_1x_kay_peer, list)
2016                 ctx_len += sizeof(peer->mi);
2017         ctx_len += sizeof(participant->mi);
2018
2019         context = os_zalloc(ctx_len);
2020         if (!context) {
2021                 os_free(conf->key);
2022                 os_free(conf);
2023                 return -1;
2024         }
2025         ctx_offset = 0;
2026         if (os_get_random(context + ctx_offset, conf->key_len) < 0) {
2027                 os_free(context);
2028                 os_free(conf->key);
2029                 os_free(conf);
2030                 return -1;
2031         }
2032         ctx_offset += conf->key_len;
2033         dl_list_for_each(peer, &participant->live_peers,
2034                          struct ieee802_1x_kay_peer, list) {
2035                 os_memcpy(context + ctx_offset, peer->mi, sizeof(peer->mi));
2036                 ctx_offset += sizeof(peer->mi);
2037         }
2038         os_memcpy(context + ctx_offset, participant->mi,
2039                   sizeof(participant->mi));
2040         ctx_offset += sizeof(participant->mi);
2041         os_memcpy(context + ctx_offset, &kay->dist_kn, sizeof(kay->dist_kn));
2042
2043         if (conf->key_len == 16) {
2044                 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2045                                                 context, ctx_len, conf->key);
2046         } else if (conf->key_len == 32) {
2047                 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2048                                                 context, ctx_len, conf->key);
2049         } else {
2050                 wpa_printf(MSG_ERROR, "KaY: SAK Length not support");
2051                 os_free(conf->key);
2052                 os_free(conf);
2053                 os_free(context);
2054                 return -1;
2055         }
2056         wpa_hexdump(MSG_DEBUG, "KaY: generated new SAK",
2057                     conf->key, conf->key_len);
2058
2059         os_memcpy(conf->ki.mi, participant->mi, MI_LEN);
2060         conf->ki.kn = participant->kay->dist_kn;
2061         conf->an = participant->kay->dist_an;
2062         conf->offset = kay->macsec_confidentiality;
2063         conf->rx = TRUE;
2064         conf->tx = TRUE;
2065
2066         sa_key = ieee802_1x_kay_init_data_key(conf);
2067         if (!sa_key) {
2068                 os_free(conf->key);
2069                 os_free(conf);
2070                 os_free(context);
2071                 return -1;
2072         }
2073         participant->new_key = sa_key;
2074
2075         dl_list_add(&participant->sak_list, &sa_key->list);
2076         ieee802_1x_cp_set_ciphersuite(participant->kay->cp,
2077                                       cipher_suite_tbl[kay->macsec_csindex].id);
2078         ieee802_1x_cp_sm_step(kay->cp);
2079         ieee802_1x_cp_set_offset(kay->cp, conf->offset);
2080         ieee802_1x_cp_sm_step(kay->cp);
2081         ieee802_1x_cp_set_distributedki(kay->cp, &conf->ki);
2082         ieee802_1x_cp_set_distributedan(kay->cp, conf->an);
2083         ieee802_1x_cp_signal_newsak(kay->cp);
2084         ieee802_1x_cp_sm_step(kay->cp);
2085
2086         dl_list_for_each(peer, &participant->live_peers,
2087                          struct ieee802_1x_kay_peer, list)
2088                 peer->sak_used = FALSE;
2089
2090         participant->kay->dist_kn++;
2091         participant->kay->dist_an++;
2092         if (participant->kay->dist_an > 3)
2093                 participant->kay->dist_an = 0;
2094
2095         participant->kay->dist_time = time(NULL);
2096
2097         os_free(conf->key);
2098         os_free(conf);
2099         os_free(context);
2100         return 0;
2101 }
2102
2103
2104 /**
2105  * ieee802_1x_kay_elect_key_server - elect the key server
2106  * when to elect: whenever the live peers list changes
2107  */
2108 static int
2109 ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant *participant)
2110 {
2111         struct ieee802_1x_kay_peer *peer;
2112         struct ieee802_1x_kay_peer *key_server = NULL;
2113         struct ieee802_1x_kay *kay = participant->kay;
2114         Boolean i_is_key_server;
2115
2116         if (participant->is_obliged_key_server) {
2117                 participant->new_sak = TRUE;
2118                 participant->to_dist_sak = FALSE;
2119                 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2120                 return 0;
2121         }
2122
2123         /* elect the key server among the peers */
2124         dl_list_for_each(peer, &participant->live_peers,
2125                          struct ieee802_1x_kay_peer, list) {
2126                 if (!peer->is_key_server)
2127                         continue;
2128
2129                 if (!key_server) {
2130                         key_server = peer;
2131                         continue;
2132                 }
2133
2134                 if (peer->key_server_priority <
2135                     key_server->key_server_priority) {
2136                         key_server = peer;
2137                 } else if (peer->key_server_priority ==
2138                            key_server->key_server_priority) {
2139                         if (os_memcmp(peer->sci.addr, key_server->sci.addr,
2140                                       ETH_ALEN) < 0)
2141                                 key_server = peer;
2142                 }
2143         }
2144
2145         /* elect the key server between me and the above elected peer */
2146         i_is_key_server = FALSE;
2147         if (key_server && participant->can_be_key_server) {
2148                 if (kay->actor_priority
2149                            < key_server->key_server_priority) {
2150                         i_is_key_server = TRUE;
2151                 } else if (kay->actor_priority
2152                                         == key_server->key_server_priority) {
2153                         if (os_memcmp(kay->actor_sci.addr, key_server->sci.addr,
2154                                       ETH_ALEN) < 0)
2155                                 i_is_key_server = TRUE;
2156                 }
2157         } else if (participant->can_be_key_server) {
2158                 i_is_key_server = TRUE;
2159         }
2160
2161         if (i_is_key_server) {
2162                 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2163                 if (os_memcmp(&kay->key_server_sci, &kay->actor_sci,
2164                               sizeof(kay->key_server_sci))) {
2165                         ieee802_1x_cp_signal_chgdserver(kay->cp);
2166                         ieee802_1x_cp_sm_step(kay->cp);
2167                 }
2168
2169                 participant->is_key_server = TRUE;
2170                 participant->principal = TRUE;
2171                 participant->new_sak = TRUE;
2172                 wpa_printf(MSG_DEBUG, "KaY: I is elected as key server");
2173                 participant->to_dist_sak = FALSE;
2174                 participant->is_elected = TRUE;
2175
2176                 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
2177                           sizeof(kay->key_server_sci));
2178                 kay->key_server_priority = kay->actor_priority;
2179         } else if (key_server) {
2180                 ieee802_1x_cp_set_electedself(kay->cp, FALSE);
2181                 if (os_memcmp(&kay->key_server_sci, &key_server->sci,
2182                               sizeof(kay->key_server_sci))) {
2183                         ieee802_1x_cp_signal_chgdserver(kay->cp);
2184                         ieee802_1x_cp_sm_step(kay->cp);
2185                 }
2186
2187                 participant->is_key_server = FALSE;
2188                 participant->principal = TRUE;
2189                 participant->is_elected = TRUE;
2190
2191                 os_memcpy(&kay->key_server_sci, &key_server->sci,
2192                           sizeof(kay->key_server_sci));
2193                 kay->key_server_priority = key_server->key_server_priority;
2194         } else {
2195                 participant->principal = FALSE;
2196                 participant->is_key_server = FALSE;
2197                 participant->is_elected = FALSE;
2198         }
2199
2200         return 0;
2201 }
2202
2203
2204 /**
2205  * ieee802_1x_kay_decide_macsec_use - the key server determinate
2206  *               how to use MACsec: whether use MACsec and its capability
2207  * protectFrames will be advised if the key server and one of its live peers are
2208  * MACsec capable and one of those request MACsec protection
2209  */
2210 static int
2211 ieee802_1x_kay_decide_macsec_use(
2212         struct ieee802_1x_mka_participant *participant)
2213 {
2214         struct ieee802_1x_kay *kay = participant->kay;
2215         struct ieee802_1x_kay_peer *peer;
2216         enum macsec_cap less_capability;
2217         Boolean has_peer;
2218
2219         if (!participant->is_key_server)
2220                 return -1;
2221
2222         /* key server self is MACsec-desired and requesting MACsec */
2223         if (!kay->macsec_desired) {
2224                 participant->advised_desired = FALSE;
2225                 return -1;
2226         }
2227         if (kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
2228                 participant->advised_desired = FALSE;
2229                 return -1;
2230         }
2231         less_capability = kay->macsec_capable;
2232
2233         /* at least one of peers is MACsec-desired and requesting MACsec */
2234         has_peer = FALSE;
2235         dl_list_for_each(peer, &participant->live_peers,
2236                          struct ieee802_1x_kay_peer, list) {
2237                 if (!peer->macsec_desired)
2238                         continue;
2239
2240                 if (peer->macsec_capbility == MACSEC_CAP_NOT_IMPLEMENTED)
2241                         continue;
2242
2243                 less_capability = (less_capability < peer->macsec_capbility) ?
2244                         less_capability : peer->macsec_capbility;
2245                 has_peer = TRUE;
2246         }
2247
2248         if (has_peer) {
2249                 participant->advised_desired = TRUE;
2250                 participant->advised_capability = less_capability;
2251                 kay->authenticated = FALSE;
2252                 kay->secured = TRUE;
2253                 kay->failed = FALSE;
2254                 ieee802_1x_cp_connect_secure(kay->cp);
2255                 ieee802_1x_cp_sm_step(kay->cp);
2256         } else {
2257                 participant->advised_desired = FALSE;
2258                 participant->advised_capability = MACSEC_CAP_NOT_IMPLEMENTED;
2259                 participant->to_use_sak = FALSE;
2260                 kay->authenticated = TRUE;
2261                 kay->secured = FALSE;
2262                 kay->failed = FALSE;
2263                 kay->ltx_kn = 0;
2264                 kay->ltx_an = 0;
2265                 kay->lrx_kn = 0;
2266                 kay->lrx_an = 0;
2267                 kay->otx_kn = 0;
2268                 kay->otx_an = 0;
2269                 kay->orx_kn = 0;
2270                 kay->orx_an = 0;
2271                 ieee802_1x_cp_connect_authenticated(kay->cp);
2272                 ieee802_1x_cp_sm_step(kay->cp);
2273         }
2274
2275         return 0;
2276 }
2277
2278 static const u8 pae_group_addr[ETH_ALEN] = {
2279         0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
2280 };
2281
2282
2283 /**
2284  * ieee802_1x_kay_encode_mkpdu -
2285  */
2286 static int
2287 ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant *participant,
2288                             struct wpabuf *pbuf)
2289 {
2290         unsigned int i;
2291         struct ieee8023_hdr *ether_hdr;
2292         struct ieee802_1x_hdr *eapol_hdr;
2293
2294         ether_hdr = wpabuf_put(pbuf, sizeof(*ether_hdr));
2295         os_memcpy(ether_hdr->dest, pae_group_addr, sizeof(ether_hdr->dest));
2296         os_memcpy(ether_hdr->src, participant->kay->actor_sci.addr,
2297                   sizeof(ether_hdr->dest));
2298         ether_hdr->ethertype = host_to_be16(ETH_P_EAPOL);
2299
2300         eapol_hdr = wpabuf_put(pbuf, sizeof(*eapol_hdr));
2301         eapol_hdr->version = EAPOL_VERSION;
2302         eapol_hdr->type = IEEE802_1X_TYPE_EAPOL_MKA;
2303         eapol_hdr->length = host_to_be16(pbuf->size - pbuf->used);
2304
2305         for (i = 0; i < ARRAY_SIZE(mak_body_handler); i++) {
2306                 if (mak_body_handler[i].body_present &&
2307                     mak_body_handler[i].body_present(participant)) {
2308                         if (mak_body_handler[i].body_tx(participant, pbuf))
2309                                 return -1;
2310                 }
2311         }
2312
2313         return 0;
2314 }
2315
2316 /**
2317  * ieee802_1x_participant_send_mkpdu -
2318  */
2319 static int
2320 ieee802_1x_participant_send_mkpdu(
2321         struct ieee802_1x_mka_participant *participant)
2322 {
2323         struct wpabuf *buf;
2324         struct ieee802_1x_kay *kay = participant->kay;
2325         size_t length = 0;
2326         unsigned int i;
2327
2328         wpa_printf(MSG_DEBUG, "KaY: to enpacket and send the MKPDU");
2329         length += sizeof(struct ieee802_1x_hdr) + sizeof(struct ieee8023_hdr);
2330         for (i = 0; i < ARRAY_SIZE(mak_body_handler); i++) {
2331                 if (mak_body_handler[i].body_present &&
2332                     mak_body_handler[i].body_present(participant))
2333                         length += mak_body_handler[i].body_length(participant);
2334         }
2335
2336         buf = wpabuf_alloc(length);
2337         if (!buf) {
2338                 wpa_printf(MSG_ERROR, "KaY: out of memory");
2339                 return -1;
2340         }
2341
2342         if (ieee802_1x_kay_encode_mkpdu(participant, buf)) {
2343                 wpa_printf(MSG_ERROR, "KaY: encode mkpdu fail!");
2344                 return -1;
2345         }
2346
2347         l2_packet_send(kay->l2_mka, NULL, 0, wpabuf_head(buf), wpabuf_len(buf));
2348         wpabuf_free(buf);
2349
2350         kay->active = TRUE;
2351         participant->active = TRUE;
2352
2353         return 0;
2354 }
2355
2356
2357 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa);
2358 /**
2359  * ieee802_1x_participant_timer -
2360  */
2361 static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
2362 {
2363         struct ieee802_1x_mka_participant *participant;
2364         struct ieee802_1x_kay *kay;
2365         struct ieee802_1x_kay_peer *peer, *pre_peer;
2366         time_t now = time(NULL);
2367         Boolean lp_changed;
2368         struct receive_sc *rxsc, *pre_rxsc;
2369         struct transmit_sa *txsa, *pre_txsa;
2370
2371         participant = (struct ieee802_1x_mka_participant *)eloop_ctx;
2372         kay = participant->kay;
2373         if (participant->cak_life) {
2374                 if (now > participant->cak_life) {
2375                         kay->authenticated = FALSE;
2376                         kay->secured = FALSE;
2377                         kay->failed = TRUE;
2378                         ieee802_1x_kay_delete_mka(kay, &participant->ckn);
2379                         return;
2380                 }
2381         }
2382
2383         /* should delete MKA instance if there are not live peers
2384          * when the MKA life elapsed since its creating */
2385         if (participant->mka_life) {
2386                 if (dl_list_empty(&participant->live_peers)) {
2387                         if (now > participant->mka_life) {
2388                                 kay->authenticated = FALSE;
2389                                 kay->secured = FALSE;
2390                                 kay->failed = TRUE;
2391                                 ieee802_1x_kay_delete_mka(kay,
2392                                                           &participant->ckn);
2393                                 return;
2394                         }
2395                 } else {
2396                         participant->mka_life = 0;
2397                 }
2398         }
2399
2400         lp_changed = FALSE;
2401         dl_list_for_each_safe(peer, pre_peer, &participant->live_peers,
2402                               struct ieee802_1x_kay_peer, list) {
2403                 if (now > peer->expire) {
2404                         wpa_printf(MSG_DEBUG, "KaY: Live peer removed");
2405                         wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2406                                     sizeof(peer->mi));
2407                         wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2408                         dl_list_for_each_safe(rxsc, pre_rxsc,
2409                                               &participant->rxsc_list,
2410                                               struct receive_sc, list) {
2411                                 if (os_memcmp(&rxsc->sci, &peer->sci,
2412                                               sizeof(rxsc->sci)) == 0) {
2413                                         secy_delete_receive_sc(kay, rxsc);
2414                                         ieee802_1x_kay_deinit_receive_sc(
2415                                                 participant, rxsc);
2416                                 }
2417                         }
2418                         dl_list_del(&peer->list);
2419                         os_free(peer);
2420                         lp_changed = TRUE;
2421                 }
2422         }
2423
2424         if (lp_changed) {
2425                 if (dl_list_empty(&participant->live_peers)) {
2426                         participant->advised_desired = FALSE;
2427                         participant->advised_capability =
2428                                 MACSEC_CAP_NOT_IMPLEMENTED;
2429                         participant->to_use_sak = FALSE;
2430                         kay->authenticated = TRUE;
2431                         kay->secured = FALSE;
2432                         kay->failed = FALSE;
2433                         kay->ltx_kn = 0;
2434                         kay->ltx_an = 0;
2435                         kay->lrx_kn = 0;
2436                         kay->lrx_an = 0;
2437                         kay->otx_kn = 0;
2438                         kay->otx_an = 0;
2439                         kay->orx_kn = 0;
2440                         kay->orx_an = 0;
2441                         dl_list_for_each_safe(txsa, pre_txsa,
2442                                               &participant->txsc->sa_list,
2443                                               struct transmit_sa, list) {
2444                                 secy_disable_transmit_sa(kay, txsa);
2445                                 ieee802_1x_kay_deinit_transmit_sa(txsa);
2446                         }
2447
2448                         ieee802_1x_cp_connect_authenticated(kay->cp);
2449                         ieee802_1x_cp_sm_step(kay->cp);
2450                 } else {
2451                         ieee802_1x_kay_elect_key_server(participant);
2452                         ieee802_1x_kay_decide_macsec_use(participant);
2453                 }
2454         }
2455
2456         dl_list_for_each_safe(peer, pre_peer, &participant->potential_peers,
2457                               struct ieee802_1x_kay_peer, list) {
2458                 if (now > peer->expire) {
2459                         wpa_printf(MSG_DEBUG, "KaY: Potential peer removed");
2460                         wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2461                                     sizeof(peer->mi));
2462                         wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2463                         dl_list_del(&peer->list);
2464                         os_free(peer);
2465                 }
2466         }
2467
2468         if (participant->new_sak) {
2469                 if (!ieee802_1x_kay_generate_new_sak(participant))
2470                         participant->to_dist_sak = TRUE;
2471
2472                 participant->new_sak = FALSE;
2473         }
2474
2475         if (participant->retry_count < MAX_RETRY_CNT) {
2476                 ieee802_1x_participant_send_mkpdu(participant);
2477                 participant->retry_count++;
2478         }
2479
2480         eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
2481                                ieee802_1x_participant_timer,
2482                                participant, NULL);
2483 }
2484
2485
2486 /**
2487  * ieee802_1x_kay_init_transmit_sa -
2488  */
2489 static struct transmit_sa *
2490 ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
2491                                 struct data_key *key)
2492 {
2493         struct transmit_sa *psa;
2494
2495         key->tx_latest = TRUE;
2496         key->rx_latest = TRUE;
2497
2498         psa = os_zalloc(sizeof(*psa));
2499         if (!psa) {
2500                 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2501                 return NULL;
2502         }
2503
2504         if (key->confidentiality_offset >= CONFIDENTIALITY_OFFSET_0 &&
2505             key->confidentiality_offset <= CONFIDENTIALITY_OFFSET_50)
2506                 psa->confidentiality = TRUE;
2507         else
2508                 psa->confidentiality = FALSE;
2509
2510         psa->an = an;
2511         psa->pkey = key;
2512         psa->next_pn = next_PN;
2513         psa->sc = psc;
2514
2515         os_get_time(&psa->created_time);
2516         psa->in_use = FALSE;
2517
2518         dl_list_add(&psc->sa_list, &psa->list);
2519         wpa_printf(MSG_DEBUG,
2520                    "KaY: Create transmit SA(an: %d, next_PN: %u) of SC(channel: %d)",
2521                    (int) an, next_PN, psc->channel);
2522
2523         return psa;
2524 }
2525
2526
2527 /**
2528  * ieee802_1x_kay_deinit_transmit_sa -
2529  */
2530 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
2531 {
2532         psa->pkey = NULL;
2533         wpa_printf(MSG_DEBUG,
2534                    "KaY: Delete transmit SA(an: %d) of SC(channel: %d)",
2535                    psa->an, psa->sc->channel);
2536         dl_list_del(&psa->list);
2537         os_free(psa);
2538 }
2539
2540
2541 /**
2542  * init_transmit_sc -
2543  */
2544 static struct transmit_sc *
2545 ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci *sci,
2546                                 int channel)
2547 {
2548         struct transmit_sc *psc;
2549
2550         psc = os_zalloc(sizeof(*psc));
2551         if (!psc) {
2552                 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2553                 return NULL;
2554         }
2555         os_memcpy(&psc->sci, sci, sizeof(psc->sci));
2556         psc->channel = channel;
2557
2558         os_get_time(&psc->created_time);
2559         psc->transmitting = FALSE;
2560         psc->encoding_sa = FALSE;
2561         psc->enciphering_sa = FALSE;
2562
2563         dl_list_init(&psc->sa_list);
2564         wpa_printf(MSG_DEBUG, "KaY: Create transmit SC(channel: %d)", channel);
2565         wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)sci , sizeof(*sci));
2566
2567         return psc;
2568 }
2569
2570
2571 /**
2572  * ieee802_1x_kay_deinit_transmit_sc -
2573  */
2574 static void
2575 ieee802_1x_kay_deinit_transmit_sc(
2576         struct ieee802_1x_mka_participant *participant, struct transmit_sc *psc)
2577 {
2578         struct transmit_sa *psa, *tmp;
2579
2580         wpa_printf(MSG_DEBUG, "KaY: Delete transmit SC(channel: %d)",
2581                    psc->channel);
2582         dl_list_for_each_safe(psa, tmp, &psc->sa_list, struct transmit_sa,
2583                               list) {
2584                 secy_disable_transmit_sa(participant->kay, psa);
2585                 ieee802_1x_kay_deinit_transmit_sa(psa);
2586         }
2587
2588         os_free(psc);
2589 }
2590
2591
2592 /****************** Interface between CP and KAY *********************/
2593 /**
2594  * ieee802_1x_kay_set_latest_sa_attr -
2595  */
2596 int ieee802_1x_kay_set_latest_sa_attr(struct ieee802_1x_kay *kay,
2597                                       struct ieee802_1x_mka_ki *lki, u8 lan,
2598                                       Boolean ltx, Boolean lrx)
2599 {
2600         struct ieee802_1x_mka_participant *principal;
2601
2602         principal = ieee802_1x_kay_get_principal_participant(kay);
2603         if (!principal)
2604                 return -1;
2605
2606         if (!lki)
2607                 os_memset(&principal->lki, 0, sizeof(principal->lki));
2608         else
2609                 os_memcpy(&principal->lki, lki, sizeof(principal->lki));
2610
2611         principal->lan = lan;
2612         principal->ltx = ltx;
2613         principal->lrx = lrx;
2614         if (!lki) {
2615                 kay->ltx_kn = 0;
2616                 kay->lrx_kn = 0;
2617         } else {
2618                 kay->ltx_kn = lki->kn;
2619                 kay->lrx_kn = lki->kn;
2620         }
2621         kay->ltx_an = lan;
2622         kay->lrx_an = lan;
2623
2624         return 0;
2625 }
2626
2627
2628 /**
2629  * ieee802_1x_kay_set_old_sa_attr -
2630  */
2631 int ieee802_1x_kay_set_old_sa_attr(struct ieee802_1x_kay *kay,
2632                                    struct ieee802_1x_mka_ki *oki,
2633                                    u8 oan, Boolean otx, Boolean orx)
2634 {
2635         struct ieee802_1x_mka_participant *principal;
2636
2637         principal = ieee802_1x_kay_get_principal_participant(kay);
2638         if (!principal)
2639                 return -1;
2640
2641         if (!oki)
2642                 os_memset(&principal->oki, 0, sizeof(principal->oki));
2643         else
2644                 os_memcpy(&principal->oki, oki, sizeof(principal->oki));
2645
2646         principal->oan = oan;
2647         principal->otx = otx;
2648         principal->orx = orx;
2649
2650         if (!oki) {
2651                 kay->otx_kn = 0;
2652                 kay->orx_kn = 0;
2653         } else {
2654                 kay->otx_kn = oki->kn;
2655                 kay->orx_kn = oki->kn;
2656         }
2657         kay->otx_an = oan;
2658         kay->orx_an = oan;
2659
2660         return 0;
2661 }
2662
2663
2664 /**
2665  * ieee802_1x_kay_create_sas -
2666  */
2667 int ieee802_1x_kay_create_sas(struct ieee802_1x_kay *kay,
2668                               struct ieee802_1x_mka_ki *lki)
2669 {
2670         struct data_key *sa_key, *latest_sak;
2671         struct ieee802_1x_mka_participant *principal;
2672         struct receive_sc *rxsc;
2673         struct receive_sa *rxsa;
2674         struct transmit_sa *txsa;
2675
2676         principal = ieee802_1x_kay_get_principal_participant(kay);
2677         if (!principal)
2678                 return -1;
2679
2680         latest_sak = NULL;
2681         dl_list_for_each(sa_key, &principal->sak_list, struct data_key, list) {
2682                 if (is_ki_equal(&sa_key->key_identifier, lki)) {
2683                         sa_key->rx_latest = TRUE;
2684                         sa_key->tx_latest = TRUE;
2685                         latest_sak = sa_key;
2686                         principal->to_use_sak = TRUE;
2687                 } else {
2688                         sa_key->rx_latest = FALSE;
2689                         sa_key->tx_latest = FALSE;
2690                 }
2691         }
2692         if (!latest_sak) {
2693                 wpa_printf(MSG_ERROR, "lki related sak not found");
2694                 return -1;
2695         }
2696
2697         dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2698                 rxsa = ieee802_1x_kay_init_receive_sa(rxsc, latest_sak->an, 1,
2699                                                       latest_sak);
2700                 if (!rxsa)
2701                         return -1;
2702
2703                 secy_create_receive_sa(kay, rxsa);
2704         }
2705
2706         txsa = ieee802_1x_kay_init_transmit_sa(principal->txsc, latest_sak->an,
2707                                                1, latest_sak);
2708         if (!txsa)
2709                 return -1;
2710
2711         secy_create_transmit_sa(kay, txsa);
2712
2713
2714
2715         return 0;
2716 }
2717
2718
2719 /**
2720  * ieee802_1x_kay_delete_sas -
2721  */
2722 int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
2723                               struct ieee802_1x_mka_ki *ki)
2724 {
2725         struct data_key *sa_key, *pre_key;
2726         struct transmit_sa *txsa, *pre_txsa;
2727         struct receive_sa *rxsa, *pre_rxsa;
2728         struct receive_sc *rxsc;
2729         struct ieee802_1x_mka_participant *principal;
2730
2731         wpa_printf(MSG_DEBUG, "KaY: Entry into %s", __func__);
2732         principal = ieee802_1x_kay_get_principal_participant(kay);
2733         if (!principal)
2734                 return -1;
2735
2736         /* remove the transmit sa */
2737         dl_list_for_each_safe(txsa, pre_txsa, &principal->txsc->sa_list,
2738                               struct transmit_sa, list) {
2739                 if (is_ki_equal(&txsa->pkey->key_identifier, ki)) {
2740                         secy_disable_transmit_sa(kay, txsa);
2741                         ieee802_1x_kay_deinit_transmit_sa(txsa);
2742                 }
2743         }
2744
2745         /* remove the receive sa */
2746         dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2747                 dl_list_for_each_safe(rxsa, pre_rxsa, &rxsc->sa_list,
2748                                       struct receive_sa, list) {
2749                         if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
2750                                 secy_disable_receive_sa(kay, rxsa);
2751                                 ieee802_1x_kay_deinit_receive_sa(rxsa);
2752                         }
2753                 }
2754         }
2755
2756         /* remove the sak */
2757         dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
2758                               struct data_key, list) {
2759                 if (is_ki_equal(&sa_key->key_identifier, ki)) {
2760                         ieee802_1x_kay_deinit_data_key(sa_key);
2761                         break;
2762                 }
2763                 if (principal->new_key == sa_key)
2764                         principal->new_key = NULL;
2765         }
2766
2767         return 0;
2768 }
2769
2770
2771 /**
2772  * ieee802_1x_kay_enable_tx_sas -
2773  */
2774 int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay *kay,
2775                                  struct ieee802_1x_mka_ki *lki)
2776 {
2777         struct ieee802_1x_mka_participant *principal;
2778         struct transmit_sa *txsa;
2779
2780         principal = ieee802_1x_kay_get_principal_participant(kay);
2781         if (!principal)
2782                 return -1;
2783
2784         dl_list_for_each(txsa, &principal->txsc->sa_list, struct transmit_sa,
2785                          list) {
2786                 if (is_ki_equal(&txsa->pkey->key_identifier, lki)) {
2787                         txsa->in_use = TRUE;
2788                         secy_enable_transmit_sa(kay, txsa);
2789                         ieee802_1x_cp_set_usingtransmitas(
2790                                 principal->kay->cp, TRUE);
2791                         ieee802_1x_cp_sm_step(principal->kay->cp);
2792                 }
2793         }
2794
2795         return 0;
2796 }
2797
2798
2799 /**
2800  * ieee802_1x_kay_enable_rx_sas -
2801  */
2802 int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay *kay,
2803                                  struct ieee802_1x_mka_ki *lki)
2804 {
2805         struct ieee802_1x_mka_participant *principal;
2806         struct receive_sa *rxsa;
2807         struct receive_sc *rxsc;
2808
2809         principal = ieee802_1x_kay_get_principal_participant(kay);
2810         if (!principal)
2811                 return -1;
2812
2813         dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2814                 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
2815                 {
2816                         if (is_ki_equal(&rxsa->pkey->key_identifier, lki)) {
2817                                 rxsa->in_use = TRUE;
2818                                 secy_enable_receive_sa(kay, rxsa);
2819                                 ieee802_1x_cp_set_usingreceivesas(
2820                                         principal->kay->cp, TRUE);
2821                                 ieee802_1x_cp_sm_step(principal->kay->cp);
2822                         }
2823                 }
2824         }
2825
2826         return 0;
2827 }
2828
2829
2830 /**
2831  * ieee802_1x_kay_enable_new_info -
2832  */
2833 int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay *kay)
2834 {
2835         struct ieee802_1x_mka_participant *principal;
2836
2837         principal = ieee802_1x_kay_get_principal_participant(kay);
2838         if (!principal)
2839                 return -1;
2840
2841         if (principal->retry_count < MAX_RETRY_CNT) {
2842                 ieee802_1x_participant_send_mkpdu(principal);
2843                 principal->retry_count++;
2844         }
2845
2846         return 0;
2847 }
2848
2849
2850 /**
2851  * ieee802_1x_kay_cp_conf -
2852  */
2853 int ieee802_1x_kay_cp_conf(struct ieee802_1x_kay *kay,
2854                            struct ieee802_1x_cp_conf *pconf)
2855 {
2856         pconf->protect = kay->macsec_protect;
2857         pconf->replay_protect = kay->macsec_replay_protect;
2858         pconf->validate = kay->macsec_validate;
2859
2860         return 0;
2861 }
2862
2863
2864 /**
2865  * ieee802_1x_kay_alloc_cp_sm -
2866  */
2867 static struct ieee802_1x_cp_sm *
2868 ieee802_1x_kay_alloc_cp_sm(struct ieee802_1x_kay *kay)
2869 {
2870         struct ieee802_1x_cp_conf conf;
2871
2872         os_memset(&conf, 0, sizeof(conf));
2873         conf.protect = kay->macsec_protect;
2874         conf.replay_protect = kay->macsec_replay_protect;
2875         conf.validate = kay->macsec_validate;
2876         conf.replay_window = kay->macsec_replay_window;
2877
2878         return ieee802_1x_cp_sm_init(kay, &conf);
2879 }
2880
2881
2882 /**
2883  * ieee802_1x_kay_mkpdu_sanity_check -
2884  *     sanity check specified in clause 11.11.2 of IEEE802.1X-2010
2885  */
2886 static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
2887                                              const u8 *buf, size_t len)
2888 {
2889         struct ieee8023_hdr *eth_hdr;
2890         struct ieee802_1x_hdr *eapol_hdr;
2891         struct ieee802_1x_mka_hdr *mka_hdr;
2892         struct ieee802_1x_mka_basic_body *body;
2893         size_t mka_msg_len;
2894         struct ieee802_1x_mka_participant *participant;
2895         size_t body_len;
2896         u8 icv[MAX_ICV_LEN];
2897         u8 *msg_icv;
2898
2899         eth_hdr = (struct ieee8023_hdr *) buf;
2900         eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
2901         mka_hdr = (struct ieee802_1x_mka_hdr *) (eapol_hdr + 1);
2902
2903         /* destination address should be not individual address */
2904         if (os_memcmp(eth_hdr->dest, pae_group_addr, ETH_ALEN) != 0) {
2905                 wpa_printf(MSG_MSGDUMP,
2906                            "KaY: ethernet destination address is not PAE group address");
2907                 return -1;
2908         }
2909
2910         /* MKPDU should not less than 32 octets */
2911         mka_msg_len = be_to_host16(eapol_hdr->length);
2912         if (mka_msg_len < 32) {
2913                 wpa_printf(MSG_MSGDUMP, "KaY: MKPDU is less than 32 octets");
2914                 return -1;
2915         }
2916         /* MKPDU should multiple 4 octets */
2917         if ((mka_msg_len % 4) != 0) {
2918                 wpa_printf(MSG_MSGDUMP,
2919                            "KaY: MKPDU is not multiple of 4 octets");
2920                 return -1;
2921         }
2922
2923         body = (struct ieee802_1x_mka_basic_body *) mka_hdr;
2924         ieee802_1x_mka_dump_basic_body(body);
2925         body_len = get_mka_param_body_len(body);
2926         /* EAPOL-MKA body should comprise basic parameter set and ICV */
2927         if (mka_msg_len < MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN) {
2928                 wpa_printf(MSG_ERROR,
2929                            "KaY: Received EAPOL-MKA Packet Body Length (%d bytes) is less than the Basic Parameter Set Header Length (%d bytes) + the Basic Parameter Set Body Length (%d bytes) + %d bytes of ICV",
2930                            (int) mka_msg_len, (int) MKA_HDR_LEN,
2931                            (int) body_len, DEFAULT_ICV_LEN);
2932                 return -1;
2933         }
2934
2935         /* CKN should be owned by I */
2936         participant = ieee802_1x_kay_get_participant(kay, body->ckn);
2937         if (!participant) {
2938                 wpa_printf(MSG_DEBUG, "CKN is not included in my CA");
2939                 return -1;
2940         }
2941
2942         /* algorithm agility check */
2943         if (os_memcmp(body->algo_agility, mka_algo_agility,
2944                       sizeof(body->algo_agility)) != 0) {
2945                 wpa_printf(MSG_ERROR,
2946                            "KaY: peer's algorithm agility not supported for me");
2947                 return -1;
2948         }
2949
2950         /* ICV check */
2951         /*
2952          * The ICV will comprise the final octets of the packet body, whatever
2953          * its size, not the fixed length 16 octets, indicated by the EAPOL
2954          * packet body length.
2955          */
2956         if (mka_alg_tbl[kay->mka_algindex].icv_hash(
2957                     participant->ick.key,
2958                     buf, len - mka_alg_tbl[kay->mka_algindex].icv_len, icv)) {
2959                 wpa_printf(MSG_ERROR, "KaY: omac1_aes_128 failed");
2960                 return -1;
2961         }
2962         msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
2963                                                  mka_msg_len);
2964
2965         if (msg_icv) {
2966                 if (os_memcmp_const(msg_icv, icv,
2967                                     mka_alg_tbl[kay->mka_algindex].icv_len) !=
2968                     0) {
2969                         wpa_printf(MSG_ERROR,
2970                                    "KaY: Computed ICV is not equal to Received ICV");
2971                 return -1;
2972                 }
2973         } else {
2974                 wpa_printf(MSG_ERROR, "KaY: No ICV");
2975                 return -1;
2976         }
2977
2978         return 0;
2979 }
2980
2981
2982 /**
2983  * ieee802_1x_kay_decode_mkpdu -
2984  */
2985 static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
2986                                        const u8 *buf, size_t len)
2987 {
2988         struct ieee802_1x_mka_participant *participant;
2989         struct ieee802_1x_mka_hdr *hdr;
2990         size_t body_len;
2991         size_t left_len;
2992         u8 body_type;
2993         int i;
2994         const u8 *pos;
2995         Boolean my_included;
2996         Boolean handled[256];
2997
2998         if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
2999                 return -1;
3000
3001         /* handle basic parameter set */
3002         pos = buf + sizeof(struct ieee8023_hdr) + sizeof(struct ieee802_1x_hdr);
3003         left_len = len - sizeof(struct ieee8023_hdr) -
3004                 sizeof(struct ieee802_1x_hdr);
3005         participant = ieee802_1x_mka_decode_basic_body(kay, pos, left_len);
3006         if (!participant)
3007                 return -1;
3008
3009         /* to skip basic parameter set */
3010         hdr = (struct ieee802_1x_mka_hdr *) pos;
3011         body_len = get_mka_param_body_len(hdr);
3012         pos += body_len + MKA_HDR_LEN;
3013         left_len -= body_len + MKA_HDR_LEN;
3014
3015         /* check i am in the peer's peer list */
3016         my_included = ieee802_1x_mka_i_in_peerlist(participant, pos, left_len);
3017         if (my_included) {
3018                 /* accept the peer as live peer */
3019                 if (!ieee802_1x_kay_is_in_peer(
3020                             participant,
3021                             participant->current_peer_id.mi)) {
3022                         if (!ieee802_1x_kay_create_live_peer(
3023                                     participant,
3024                                     participant->current_peer_id.mi,
3025                                     be_to_host32(
3026                                             participant->current_peer_id.mn)))
3027                                 return -1;
3028                         ieee802_1x_kay_elect_key_server(participant);
3029                         ieee802_1x_kay_decide_macsec_use(participant);
3030                 }
3031                 if (ieee802_1x_kay_is_in_potential_peer(
3032                             participant, participant->current_peer_id.mi)) {
3033                         if (!ieee802_1x_kay_move_live_peer(
3034                                     participant,
3035                                     participant->current_peer_id.mi,
3036                                     be_to_host32(participant->
3037                                                  current_peer_id.mn)))
3038                                 return -1;
3039                         ieee802_1x_kay_elect_key_server(participant);
3040                         ieee802_1x_kay_decide_macsec_use(participant);
3041                 }
3042         }
3043
3044         /*
3045          * Handle other parameter set than basic parameter set.
3046          * Each parameter set should be present only once.
3047          */
3048         for (i = 0; i < 256; i++)
3049                 handled[i] = FALSE;
3050
3051         handled[0] = TRUE;
3052         while (left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN) {
3053                 hdr = (struct ieee802_1x_mka_hdr *) pos;
3054                 body_len = get_mka_param_body_len(hdr);
3055                 body_type = get_mka_param_body_type(hdr);
3056
3057                 if (body_type == MKA_ICV_INDICATOR)
3058                         return 0;
3059
3060                 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
3061                         wpa_printf(MSG_ERROR,
3062                                    "KaY: MKA Peer Packet Body Length (%d bytes) is less than the Parameter Set Header Length (%d bytes) + the Parameter Set Body Length (%d bytes) + %d bytes of ICV",
3063                                    (int) left_len, (int) MKA_HDR_LEN,
3064                                    (int) body_len, DEFAULT_ICV_LEN);
3065                         goto next_para_set;
3066                 }
3067
3068                 if (handled[body_type])
3069                         goto next_para_set;
3070
3071                 handled[body_type] = TRUE;
3072                 if (body_type < ARRAY_SIZE(mak_body_handler) &&
3073                     mak_body_handler[body_type].body_rx) {
3074                         mak_body_handler[body_type].body_rx
3075                                 (participant, pos, left_len);
3076                 } else {
3077                         wpa_printf(MSG_ERROR,
3078                                    "The type %d not supported in this MKA version %d",
3079                                    body_type, MKA_VERSION_ID);
3080                 }
3081
3082 next_para_set:
3083                 pos += body_len + MKA_HDR_LEN;
3084                 left_len -= body_len + MKA_HDR_LEN;
3085         }
3086
3087         kay->active = TRUE;
3088         participant->retry_count = 0;
3089         participant->active = TRUE;
3090
3091         return 0;
3092 }
3093
3094
3095
3096 static void kay_l2_receive(void *ctx, const u8 *src_addr, const u8 *buf,
3097                            size_t len)
3098 {
3099         struct ieee802_1x_kay *kay = ctx;
3100         struct ieee8023_hdr *eth_hdr;
3101         struct ieee802_1x_hdr *eapol_hdr;
3102
3103         /* must contain at least ieee8023_hdr + ieee802_1x_hdr */
3104         if (len < sizeof(*eth_hdr) + sizeof(*eapol_hdr)) {
3105                 wpa_printf(MSG_MSGDUMP, "KaY: EAPOL frame too short (%lu)",
3106                            (unsigned long) len);
3107                 return;
3108         }
3109
3110         eth_hdr = (struct ieee8023_hdr *) buf;
3111         eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
3112         if (len != sizeof(*eth_hdr) + sizeof(*eapol_hdr) +
3113             be_to_host16(eapol_hdr->length)) {
3114                 wpa_printf(MSG_MSGDUMP, "KAY: EAPOL MPDU is invalid: (%lu-%lu)",
3115                            (unsigned long) len,
3116                            (unsigned long) be_to_host16(eapol_hdr->length));
3117                 return;
3118         }
3119
3120         if (eapol_hdr->version < EAPOL_VERSION) {
3121                 wpa_printf(MSG_MSGDUMP, "KaY: version %d does not support MKA",
3122                            eapol_hdr->version);
3123                 return;
3124         }
3125         if (be_to_host16(eth_hdr->ethertype) != ETH_P_PAE ||
3126             eapol_hdr->type != IEEE802_1X_TYPE_EAPOL_MKA)
3127                 return;
3128
3129         wpa_hexdump(MSG_DEBUG, "RX EAPOL-MKA: ", buf, len);
3130         if (dl_list_empty(&kay->participant_list)) {
3131                 wpa_printf(MSG_ERROR, "KaY: no MKA participant instance");
3132                 return;
3133         }
3134
3135         ieee802_1x_kay_decode_mkpdu(kay, buf, len);
3136 }
3137
3138
3139 /**
3140  * ieee802_1x_kay_init -
3141  */
3142 struct ieee802_1x_kay *
3143 ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
3144                     const char *ifname, const u8 *addr)
3145 {
3146         struct ieee802_1x_kay *kay;
3147
3148         kay = os_zalloc(sizeof(*kay));
3149         if (!kay) {
3150                 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3151                 return NULL;
3152         }
3153
3154         kay->ctx = ctx;
3155
3156         kay->enable = TRUE;
3157         kay->active = FALSE;
3158
3159         kay->authenticated = FALSE;
3160         kay->secured = FALSE;
3161         kay->failed = FALSE;
3162         kay->policy = policy;
3163
3164         os_strlcpy(kay->if_name, ifname, IFNAMSIZ);
3165         os_memcpy(kay->actor_sci.addr, addr, ETH_ALEN);
3166         kay->actor_sci.port = host_to_be16(0x0001);
3167         kay->actor_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3168
3169         /* While actor acts as a key server, shall distribute sakey */
3170         kay->dist_kn = 1;
3171         kay->dist_an = 0;
3172         kay->dist_time = 0;
3173
3174         kay->pn_exhaustion = PENDING_PN_EXHAUSTION;
3175         kay->macsec_csindex = DEFAULT_CS_INDEX;
3176         kay->mka_algindex = DEFAULT_MKA_ALG_INDEX;
3177         kay->mka_version = MKA_VERSION_ID;
3178
3179         os_memcpy(kay->algo_agility, mka_algo_agility,
3180                   sizeof(kay->algo_agility));
3181
3182         dl_list_init(&kay->participant_list);
3183
3184         if (policy == DO_NOT_SECURE) {
3185                 kay->macsec_capable = MACSEC_CAP_NOT_IMPLEMENTED;
3186                 kay->macsec_desired = FALSE;
3187                 kay->macsec_protect = FALSE;
3188                 kay->macsec_validate = Disabled;
3189                 kay->macsec_replay_protect = FALSE;
3190                 kay->macsec_replay_window = 0;
3191                 kay->macsec_confidentiality = CONFIDENTIALITY_NONE;
3192         } else {
3193                 kay->macsec_capable = MACSEC_CAP_INTEG_AND_CONF_0_30_50;
3194                 kay->macsec_desired = TRUE;
3195                 kay->macsec_protect = TRUE;
3196                 kay->macsec_validate = Strict;
3197                 kay->macsec_replay_protect = FALSE;
3198                 kay->macsec_replay_window = 0;
3199                 kay->macsec_confidentiality = CONFIDENTIALITY_OFFSET_0;
3200         }
3201
3202         wpa_printf(MSG_DEBUG, "KaY: state machine created");
3203
3204         /* Initialize the SecY must be prio to CP, as CP will control SecY */
3205         secy_init_macsec(kay);
3206         secy_get_available_transmit_sc(kay, &kay->sc_ch);
3207
3208         wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
3209
3210         /* init CP */
3211         kay->cp = ieee802_1x_kay_alloc_cp_sm(kay);
3212         if (kay->cp == NULL) {
3213                 ieee802_1x_kay_deinit(kay);
3214                 return NULL;
3215         }
3216
3217         if (policy == DO_NOT_SECURE) {
3218                 ieee802_1x_cp_connect_authenticated(kay->cp);
3219                 ieee802_1x_cp_sm_step(kay->cp);
3220         } else {
3221                 kay->l2_mka = l2_packet_init(kay->if_name, NULL, ETH_P_PAE,
3222                                              kay_l2_receive, kay, 1);
3223                 if (kay->l2_mka == NULL) {
3224                         wpa_printf(MSG_WARNING,
3225                                    "KaY: Failed to initialize L2 packet processing for MKA packet");
3226                         ieee802_1x_kay_deinit(kay);
3227                         return NULL;
3228                 }
3229         }
3230
3231         return kay;
3232 }
3233
3234
3235 /**
3236  * ieee802_1x_kay_deinit -
3237  */
3238 void
3239 ieee802_1x_kay_deinit(struct ieee802_1x_kay *kay)
3240 {
3241         struct ieee802_1x_mka_participant *participant;
3242
3243         if (!kay)
3244                 return;
3245
3246         wpa_printf(MSG_DEBUG, "KaY: state machine removed");
3247
3248         while (!dl_list_empty(&kay->participant_list)) {
3249                 participant = dl_list_entry(kay->participant_list.next,
3250                                             struct ieee802_1x_mka_participant,
3251                                             list);
3252                 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
3253         }
3254
3255         ieee802_1x_cp_sm_deinit(kay->cp);
3256         secy_deinit_macsec(kay);
3257
3258         if (kay->l2_mka) {
3259                 l2_packet_deinit(kay->l2_mka);
3260                 kay->l2_mka = NULL;
3261         }
3262
3263         os_free(kay->ctx);
3264         os_free(kay);
3265 }
3266
3267
3268 /**
3269  * ieee802_1x_kay_create_mka -
3270  */
3271 struct ieee802_1x_mka_participant *
3272 ieee802_1x_kay_create_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn,
3273                           struct mka_key *cak, u32 life,
3274                           enum mka_created_mode mode, Boolean is_authenticator)
3275 {
3276         struct ieee802_1x_mka_participant *participant;
3277         unsigned int usecs;
3278
3279         if (!kay || !ckn || !cak) {
3280                 wpa_printf(MSG_ERROR, "KaY: ckn or cak is null");
3281                 return NULL;
3282         }
3283
3284         if (cak->len != mka_alg_tbl[kay->mka_algindex].cak_len) {
3285                 wpa_printf(MSG_ERROR, "KaY: CAK length not follow key schema");
3286                 return NULL;
3287         }
3288         if (ckn->len > MAX_CKN_LEN) {
3289                 wpa_printf(MSG_ERROR, "KaY: CKN is out of range(<=32 bytes)");
3290                 return NULL;
3291         }
3292         if (!kay->enable) {
3293                 wpa_printf(MSG_ERROR, "KaY: Now is at disable state");
3294                 return NULL;
3295         }
3296
3297         participant = os_zalloc(sizeof(*participant));
3298         if (!participant) {
3299                 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3300                 return NULL;
3301         }
3302
3303         participant->ckn.len = ckn->len;
3304         os_memcpy(participant->ckn.name, ckn->name, ckn->len);
3305         participant->cak.len = cak->len;
3306         os_memcpy(participant->cak.key, cak->key, cak->len);
3307         if (life)
3308                 participant->cak_life = life + time(NULL);
3309
3310         switch (mode) {
3311         case EAP_EXCHANGE:
3312                 if (is_authenticator) {
3313                         participant->is_obliged_key_server = TRUE;
3314                         participant->can_be_key_server = TRUE;
3315                         participant->is_key_server = TRUE;
3316                         participant->principal = TRUE;
3317
3318                         os_memcpy(&kay->key_server_sci, &kay->actor_sci,
3319                                   sizeof(kay->key_server_sci));
3320                         kay->key_server_priority = kay->actor_priority;
3321                         participant->is_elected = TRUE;
3322                 } else {
3323                         participant->is_obliged_key_server = FALSE;
3324                         participant->can_be_key_server = FALSE;
3325                         participant->is_key_server = FALSE;
3326                         participant->is_elected = TRUE;
3327                 }
3328                 break;
3329
3330         default:
3331                 participant->is_obliged_key_server = FALSE;
3332                 participant->can_be_key_server = TRUE;
3333                 participant->is_key_server = TRUE;
3334                 participant->is_elected = FALSE;
3335                 break;
3336         }
3337
3338         participant->cached = FALSE;
3339
3340         participant->active = FALSE;
3341         participant->participant = FALSE;
3342         participant->retain = FALSE;
3343         participant->activate = DEFAULT;
3344
3345         if (participant->is_key_server)
3346                 participant->principal = TRUE;
3347
3348         dl_list_init(&participant->live_peers);
3349         dl_list_init(&participant->potential_peers);
3350
3351         participant->retry_count = 0;
3352         participant->kay = kay;
3353
3354         if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
3355                 goto fail;
3356         participant->mn = 0;
3357
3358         participant->lrx = FALSE;
3359         participant->ltx = FALSE;
3360         participant->orx = FALSE;
3361         participant->otx = FALSE;
3362         participant->to_dist_sak = FALSE;
3363         participant->to_use_sak = FALSE;
3364         participant->new_sak = FALSE;
3365         dl_list_init(&participant->sak_list);
3366         participant->new_key = NULL;
3367         dl_list_init(&participant->rxsc_list);
3368         participant->txsc = ieee802_1x_kay_init_transmit_sc(&kay->actor_sci,
3369                                                             kay->sc_ch);
3370         secy_cp_control_protect_frames(kay, kay->macsec_protect);
3371         secy_cp_control_replay(kay, kay->macsec_replay_protect,
3372                                kay->macsec_replay_window);
3373         secy_create_transmit_sc(kay, participant->txsc);
3374
3375         /* to derive KEK from CAK and CKN */
3376         participant->kek.len = mka_alg_tbl[kay->mka_algindex].kek_len;
3377         if (mka_alg_tbl[kay->mka_algindex].kek_trfm(participant->cak.key,
3378                                                     participant->ckn.name,
3379                                                     participant->ckn.len,
3380                                                     participant->kek.key)) {
3381                 wpa_printf(MSG_ERROR, "KaY: Derived KEK failed");
3382                 goto fail;
3383         }
3384         wpa_hexdump_key(MSG_DEBUG, "KaY: Derived KEK",
3385                         participant->kek.key, participant->kek.len);
3386
3387         /* to derive ICK from CAK and CKN */
3388         participant->ick.len = mka_alg_tbl[kay->mka_algindex].ick_len;
3389         if (mka_alg_tbl[kay->mka_algindex].ick_trfm(participant->cak.key,
3390                                                     participant->ckn.name,
3391                                                     participant->ckn.len,
3392                                                     participant->ick.key)) {
3393                 wpa_printf(MSG_ERROR, "KaY: Derived ICK failed");
3394                 goto fail;
3395         }
3396         wpa_hexdump_key(MSG_DEBUG, "KaY: Derived ICK",
3397                         participant->ick.key, participant->ick.len);
3398
3399         dl_list_add(&kay->participant_list, &participant->list);
3400         wpa_hexdump(MSG_DEBUG, "KaY: Participant created:",
3401                     ckn->name, ckn->len);
3402
3403         usecs = os_random() % (MKA_HELLO_TIME * 1000);
3404         eloop_register_timeout(0, usecs, ieee802_1x_participant_timer,
3405                                participant, NULL);
3406         participant->mka_life = MKA_LIFE_TIME / 1000 + time(NULL) +
3407                 usecs / 1000000;
3408
3409         return participant;
3410
3411 fail:
3412         os_free(participant);
3413         return NULL;
3414 }
3415
3416
3417 /**
3418  * ieee802_1x_kay_delete_mka -
3419  */
3420 void
3421 ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
3422 {
3423         struct ieee802_1x_mka_participant *participant;
3424         struct ieee802_1x_kay_peer *peer;
3425         struct data_key *sak;
3426         struct receive_sc *rxsc;
3427
3428         if (!kay || !ckn)
3429                 return;
3430
3431         wpa_printf(MSG_DEBUG, "KaY: participant removed");
3432
3433         /* get the participant */
3434         participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3435         if (!participant) {
3436                 wpa_hexdump(MSG_DEBUG, "KaY: participant is not found",
3437                             ckn->name, ckn->len);
3438                 return;
3439         }
3440
3441         dl_list_del(&participant->list);
3442
3443         /* remove live peer */
3444         while (!dl_list_empty(&participant->live_peers)) {
3445                 peer = dl_list_entry(participant->live_peers.next,
3446                                      struct ieee802_1x_kay_peer, list);
3447                 dl_list_del(&peer->list);
3448                 os_free(peer);
3449         }
3450
3451         /* remove potential peer */
3452         while (!dl_list_empty(&participant->potential_peers)) {
3453                 peer = dl_list_entry(participant->potential_peers.next,
3454                                      struct ieee802_1x_kay_peer, list);
3455                 dl_list_del(&peer->list);
3456                 os_free(peer);
3457         }
3458
3459         /* remove sak */
3460         while (!dl_list_empty(&participant->sak_list)) {
3461                 sak = dl_list_entry(participant->sak_list.next,
3462                                     struct data_key, list);
3463                 dl_list_del(&sak->list);
3464                 os_free(sak->key);
3465                 os_free(sak);
3466         }
3467         while (!dl_list_empty(&participant->rxsc_list)) {
3468                 rxsc = dl_list_entry(participant->rxsc_list.next,
3469                                      struct receive_sc, list);
3470                 secy_delete_receive_sc(kay, rxsc);
3471                 ieee802_1x_kay_deinit_receive_sc(participant, rxsc);
3472         }
3473         secy_delete_transmit_sc(kay, participant->txsc);
3474         ieee802_1x_kay_deinit_transmit_sc(participant, participant->txsc);
3475
3476         os_memset(&participant->cak, 0, sizeof(participant->cak));
3477         os_memset(&participant->kek, 0, sizeof(participant->kek));
3478         os_memset(&participant->ick, 0, sizeof(participant->ick));
3479         os_free(participant);
3480 }
3481
3482
3483 /**
3484  * ieee802_1x_kay_mka_participate -
3485  */
3486 void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay *kay,
3487                                     struct mka_key_name *ckn,
3488                                     Boolean status)
3489 {
3490         struct ieee802_1x_mka_participant *participant;
3491
3492         if (!kay || !ckn)
3493                 return;
3494
3495         participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3496         if (!participant)
3497                 return;
3498
3499         participant->active = status;
3500 }
3501
3502
3503 /**
3504  * ieee802_1x_kay_new_sak -
3505  */
3506 int
3507 ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
3508 {
3509         struct ieee802_1x_mka_participant *participant;
3510
3511         if (!kay)
3512                 return -1;
3513
3514         participant = ieee802_1x_kay_get_principal_participant(kay);
3515         if (!participant)
3516                 return -1;
3517
3518         participant->new_sak = TRUE;
3519         wpa_printf(MSG_DEBUG, "KaY: new SAK signal");
3520
3521         return 0;
3522 }
3523
3524
3525 /**
3526  * ieee802_1x_kay_change_cipher_suite -
3527  */
3528 int
3529 ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay, int cs_index)
3530 {
3531         struct ieee802_1x_mka_participant *participant;
3532
3533         if (!kay)
3534                 return -1;
3535
3536         if ((unsigned int) cs_index >= CS_TABLE_SIZE) {
3537                 wpa_printf(MSG_ERROR,
3538                            "KaY: Configured cipher suite index is out of range");
3539                 return -1;
3540         }
3541         if (kay->macsec_csindex == cs_index)
3542                 return -2;
3543
3544         if (cs_index == 0)
3545                 kay->macsec_desired = FALSE;
3546
3547         kay->macsec_csindex = cs_index;
3548         kay->macsec_capable = cipher_suite_tbl[kay->macsec_csindex].capable;
3549
3550         participant = ieee802_1x_kay_get_principal_participant(kay);
3551         if (participant) {
3552                 wpa_printf(MSG_INFO, "KaY: Cipher Suite changed");
3553                 participant->new_sak = TRUE;
3554         }
3555
3556         return 0;
3557 }