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