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