Blame SOURCES/macsec-0011-mka-Implement-reference-counting-on-data_key.patch

41389a
From 99b82bf53792d48b5d0c3f9edcccc6e53c9510fe Mon Sep 17 00:00:00 2001
41389a
Message-Id: <99b82bf53792d48b5d0c3f9edcccc6e53c9510fe.1488376601.git.dcaratti@redhat.com>
41389a
From: Sabrina Dubroca <sd@queasysnail.net>
41389a
Date: Fri, 21 Oct 2016 14:45:29 +0200
41389a
Subject: [PATCH] mka: Implement reference counting on data_key
41389a
41389a
struct data_key already had a 'user' field for reference counting, but
41389a
it was basically unused.
41389a
41389a
Add an ieee802_1x_kay_use_data_key() function to take a reference on a
41389a
key, and use ieee802_1x_kay_deinit_data_key() to release the reference.
41389a
41389a
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
41389a
---
41389a
 src/pae/ieee802_1x_kay.c | 28 ++++++++++++++++++++++++----
41389a
 src/pae/ieee802_1x_kay.h |  2 +-
41389a
 2 files changed, 25 insertions(+), 5 deletions(-)
41389a
41389a
diff --git a/src/pae/ieee802_1x_kay.c b/src/pae/ieee802_1x_kay.c
41389a
index e312d04..63bbd13 100644
41389a
--- a/src/pae/ieee802_1x_kay.c
41389a
+++ b/src/pae/ieee802_1x_kay.c
41389a
@@ -411,6 +411,8 @@ ieee802_1x_kay_get_peer_sci(struct ieee802_1x_mka_participant *participant,
41389a
 }
41389a
 
41389a
 
41389a
+static void ieee802_1x_kay_use_data_key(struct data_key *pkey);
41389a
+
41389a
 /**
41389a
  * ieee802_1x_kay_init_receive_sa -
41389a
  */
41389a
@@ -429,6 +431,7 @@ ieee802_1x_kay_init_receive_sa(struct receive_sc *psc, u8 an, u32 lowest_pn,
41389a
 		return NULL;
41389a
 	}
41389a
 
41389a
+	ieee802_1x_kay_use_data_key(key);
41389a
 	psa->pkey = key;
41389a
 	psa->lowest_pn = lowest_pn;
41389a
 	psa->next_pn = lowest_pn;
41389a
@@ -447,11 +450,14 @@ ieee802_1x_kay_init_receive_sa(struct receive_sc *psc, u8 an, u32 lowest_pn,
41389a
 }
41389a
 
41389a
 
41389a
+static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey);
41389a
+
41389a
 /**
41389a
  * ieee802_1x_kay_deinit_receive_sa -
41389a
  */
41389a
 static void ieee802_1x_kay_deinit_receive_sa(struct receive_sa *psa)
41389a
 {
41389a
+	ieee802_1x_kay_deinit_data_key(psa->pkey);
41389a
 	psa->pkey = NULL;
41389a
 	wpa_printf(MSG_DEBUG,
41389a
 		   "KaY: Delete receive SA(an: %hhu) of SC",
41389a
@@ -1612,6 +1618,7 @@ ieee802_1x_mka_decode_dist_sak_body(
41389a
 	sa_key->an = body->dan;
41389a
 	ieee802_1x_kay_init_data_key(sa_key);
41389a
 
41389a
+	ieee802_1x_kay_use_data_key(sa_key);
41389a
 	dl_list_add(&participant->sak_list, &sa_key->list);
41389a
 
41389a
 	ieee802_1x_cp_set_ciphersuite(kay->cp, cs->id);
41389a
@@ -1873,7 +1880,17 @@ static struct mka_param_body_handler mka_body_handler[] = {
41389a
 
41389a
 
41389a
 /**
41389a
- * ieee802_1x_kay_deinit_data_key -
41389a
+ * ieee802_1x_kay_use_data_key - Take reference on a key
41389a
+ */
41389a
+static void ieee802_1x_kay_use_data_key(struct data_key *pkey)
41389a
+{
41389a
+	pkey->user++;
41389a
+}
41389a
+
41389a
+
41389a
+/**
41389a
+ * ieee802_1x_kay_deinit_data_key - Release reference on a key and
41389a
+ * free if there are no remaining users
41389a
  */
41389a
 static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
41389a
 {
41389a
@@ -1884,7 +1901,6 @@ static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
41389a
 	if (pkey->user > 1)
41389a
 		return;
41389a
 
41389a
-	dl_list_del(&pkey->list);
41389a
 	os_free(pkey->key);
41389a
 	os_free(pkey);
41389a
 }
41389a
@@ -1994,7 +2010,9 @@ ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
41389a
 
41389a
 	participant->new_key = sa_key;
41389a
 
41389a
+	ieee802_1x_kay_use_data_key(sa_key);
41389a
 	dl_list_add(&participant->sak_list, &sa_key->list);
41389a
+
41389a
 	ieee802_1x_cp_set_ciphersuite(kay->cp, cs->id);
41389a
 	ieee802_1x_cp_sm_step(kay->cp);
41389a
 	ieee802_1x_cp_set_offset(kay->cp, kay->macsec_confidentiality);
41389a
@@ -2436,6 +2454,7 @@ ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
41389a
 		psa->confidentiality = FALSE;
41389a
 
41389a
 	psa->an = an;
41389a
+	ieee802_1x_kay_use_data_key(key);
41389a
 	psa->pkey = key;
41389a
 	psa->next_pn = next_PN;
41389a
 	psa->sc = psc;
41389a
@@ -2457,6 +2476,7 @@ ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
41389a
  */
41389a
 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
41389a
 {
41389a
+	ieee802_1x_kay_deinit_data_key(psa->pkey);
41389a
 	psa->pkey = NULL;
41389a
 	wpa_printf(MSG_DEBUG,
41389a
 		   "KaY: Delete transmit SA(an: %hhu) of SC",
41389a
@@ -2708,6 +2728,7 @@ int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
41389a
 	dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
41389a
 			      struct data_key, list) {
41389a
 		if (is_ki_equal(&sa_key->key_identifier, ki)) {
41389a
+			dl_list_del(&sa_key->list);
41389a
 			ieee802_1x_kay_deinit_data_key(sa_key);
41389a
 			break;
41389a
 		}
41389a
@@ -3375,8 +3396,7 @@ ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
41389a
 		sak = dl_list_entry(participant->sak_list.next,
41389a
 				    struct data_key, list);
41389a
 		dl_list_del(&sak->list);
41389a
-		os_free(sak->key);
41389a
-		os_free(sak);
41389a
+		ieee802_1x_kay_deinit_data_key(sak);
41389a
 	}
41389a
 	while (!dl_list_empty(&participant->rxsc_list)) {
41389a
 		rxsc = dl_list_entry(participant->rxsc_list.next,
41389a
diff --git a/src/pae/ieee802_1x_kay.h b/src/pae/ieee802_1x_kay.h
41389a
index 5233cb2..576a8a0 100644
41389a
--- a/src/pae/ieee802_1x_kay.h
41389a
+++ b/src/pae/ieee802_1x_kay.h
41389a
@@ -70,7 +70,7 @@ struct data_key {
41389a
 	Boolean rx_latest;
41389a
 	Boolean tx_latest;
41389a
 
41389a
-	int user;  /* FIXME: to indicate if it can be delete safely */
41389a
+	int user;
41389a
 
41389a
 	struct dl_list list;
41389a
 };
41389a
-- 
41389a
2.7.4
41389a