drivers/net/ovpn/crypto.c

Source file repositories/reference/linux-study-clean/drivers/net/ovpn/crypto.c

File Facts

System
Linux kernel
Corpus path
drivers/net/ovpn/crypto.c
Extension
.c
Size
5129 bytes
Lines
211
Domain
Driver Families
Bucket
drivers/net
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0
/*  OpenVPN data channel offload
 *
 *  Copyright (C) 2020-2025 OpenVPN, Inc.
 *
 *  Author:	James Yonan <james@openvpn.net>
 *		Antonio Quartulli <antonio@openvpn.net>
 */

#include <linux/types.h>
#include <linux/net.h>
#include <linux/netdevice.h>
#include <uapi/linux/ovpn.h>

#include "ovpnpriv.h"
#include "main.h"
#include "pktid.h"
#include "crypto_aead.h"
#include "crypto.h"

static void ovpn_ks_destroy_rcu(struct rcu_head *head)
{
	struct ovpn_crypto_key_slot *ks;

	ks = container_of(head, struct ovpn_crypto_key_slot, rcu);
	ovpn_aead_crypto_key_slot_destroy(ks);
}

void ovpn_crypto_key_slot_release(struct kref *kref)
{
	struct ovpn_crypto_key_slot *ks;

	ks = container_of(kref, struct ovpn_crypto_key_slot, refcount);
	call_rcu(&ks->rcu, ovpn_ks_destroy_rcu);
}

/* can only be invoked when all peer references have been dropped (i.e. RCU
 * release routine)
 */
void ovpn_crypto_state_release(struct ovpn_crypto_state *cs)
{
	struct ovpn_crypto_key_slot *ks;

	ks = rcu_access_pointer(cs->slots[0]);
	if (ks) {
		RCU_INIT_POINTER(cs->slots[0], NULL);
		ovpn_crypto_key_slot_put(ks);
	}

	ks = rcu_access_pointer(cs->slots[1]);
	if (ks) {
		RCU_INIT_POINTER(cs->slots[1], NULL);
		ovpn_crypto_key_slot_put(ks);
	}
}

/* removes the key matching the specified id from the crypto context */
bool ovpn_crypto_kill_key(struct ovpn_crypto_state *cs, u8 key_id)
{
	struct ovpn_crypto_key_slot *ks = NULL;

	spin_lock_bh(&cs->lock);
	if (rcu_access_pointer(cs->slots[0])->key_id == key_id) {
		ks = rcu_replace_pointer(cs->slots[0], NULL,
					 lockdep_is_held(&cs->lock));
	} else if (rcu_access_pointer(cs->slots[1])->key_id == key_id) {
		ks = rcu_replace_pointer(cs->slots[1], NULL,
					 lockdep_is_held(&cs->lock));
	}
	spin_unlock_bh(&cs->lock);

	if (ks)
		ovpn_crypto_key_slot_put(ks);

	/* let the caller know if a key was actually killed */
	return ks;
}

/* Reset the ovpn_crypto_state object in a way that is atomic
 * to RCU readers.
 */
int ovpn_crypto_state_reset(struct ovpn_crypto_state *cs,
			    const struct ovpn_peer_key_reset *pkr)
{
	struct ovpn_crypto_key_slot *old = NULL, *new;
	u8 idx;

	if (pkr->slot != OVPN_KEY_SLOT_PRIMARY &&
	    pkr->slot != OVPN_KEY_SLOT_SECONDARY)
		return -EINVAL;

Annotation

Implementation Notes