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