Skip to content

Commit 79cf2df

Browse files
committed
mac80211: clean up key freeing a bit
When a key is allocated but not really added, there's no need to go through the entire teardown process. Also, if adding a key fails, ieee80211_key_link() can take care of freeing it instead of the (only) caller. Signed-off-by: Johannes Berg <johannes.berg@intel.com>
1 parent 3d5839b commit 79cf2df

3 files changed

Lines changed: 25 additions & 22 deletions

File tree

‎net/mac80211/cfg.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
175175
* add it to the device after the station.
176176
*/
177177
if (!sta || !test_sta_flag(sta, WLAN_STA_ASSOC)) {
178-
ieee80211_key_free(sdata->local, key);
178+
ieee80211_key_free_unused(key);
179179
err = -ENOENT;
180180
goto out_unlock;
181181
}
@@ -214,8 +214,6 @@ static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
214214
}
215215

216216
err = ieee80211_key_link(key, sdata, sta);
217-
if (err)
218-
ieee80211_key_free(sdata->local, key);
219217

220218
out_unlock:
221219
mutex_unlock(&sdata->local->sta_mtx);

‎net/mac80211/key.c‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,15 @@ struct ieee80211_key *ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
397397
return key;
398398
}
399399

400+
static void ieee80211_key_free_common(struct ieee80211_key *key)
401+
{
402+
if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
403+
ieee80211_aes_key_free(key->u.ccmp.tfm);
404+
if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
405+
ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
406+
kfree(key);
407+
}
408+
400409
static void __ieee80211_key_destroy(struct ieee80211_key *key,
401410
bool delay_tailroom)
402411
{
@@ -412,10 +421,6 @@ static void __ieee80211_key_destroy(struct ieee80211_key *key,
412421
if (key->local)
413422
ieee80211_key_disable_hw_accel(key);
414423

415-
if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
416-
ieee80211_aes_key_free(key->u.ccmp.tfm);
417-
if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
418-
ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
419424
if (key->local) {
420425
struct ieee80211_sub_if_data *sdata = key->sdata;
421426

@@ -431,7 +436,13 @@ static void __ieee80211_key_destroy(struct ieee80211_key *key,
431436
}
432437
}
433438

434-
kfree(key);
439+
ieee80211_key_free_common(key);
440+
}
441+
442+
void ieee80211_key_free_unused(struct ieee80211_key *key)
443+
{
444+
WARN_ON(key->sdata || key->local);
445+
ieee80211_key_free_common(key);
435446
}
436447

437448
int ieee80211_key_link(struct ieee80211_key *key,
@@ -469,6 +480,9 @@ int ieee80211_key_link(struct ieee80211_key *key,
469480

470481
ret = ieee80211_key_enable_hw_accel(key);
471482

483+
if (ret)
484+
__ieee80211_key_free(key, true);
485+
472486
mutex_unlock(&sdata->local->key_mtx);
473487

474488
return ret;
@@ -489,14 +503,6 @@ void __ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
489503
__ieee80211_key_destroy(key, delay_tailroom);
490504
}
491505

492-
void ieee80211_key_free(struct ieee80211_local *local,
493-
struct ieee80211_key *key)
494-
{
495-
mutex_lock(&local->key_mtx);
496-
__ieee80211_key_free(key, true);
497-
mutex_unlock(&local->key_mtx);
498-
}
499-
500506
void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
501507
{
502508
struct ieee80211_key *key;

‎net/mac80211/key.h‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,13 @@ struct ieee80211_key *ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
129129
size_t seq_len, const u8 *seq);
130130
/*
131131
* Insert a key into data structures (sdata, sta if necessary)
132-
* to make it used, free old key.
132+
* to make it used, free old key. On failure, also free the new key.
133133
*/
134-
int __must_check ieee80211_key_link(struct ieee80211_key *key,
135-
struct ieee80211_sub_if_data *sdata,
136-
struct sta_info *sta);
134+
int ieee80211_key_link(struct ieee80211_key *key,
135+
struct ieee80211_sub_if_data *sdata,
136+
struct sta_info *sta);
137137
void __ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom);
138-
void ieee80211_key_free(struct ieee80211_local *local,
139-
struct ieee80211_key *key);
138+
void ieee80211_key_free_unused(struct ieee80211_key *key);
140139
void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
141140
bool uni, bool multi);
142141
void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,

0 commit comments

Comments
 (0)