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