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