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