Remove the IPsec offload code (audit V2)
cdx no longer builds it, so it goes: cdx's dpa_ipsec_freebsd.c and cdx_dpa_ipsec_freebsd.c, the vendor control_ipsec.c and query_ipsec.c, and cmm's PF_KEY handling (cmm_ipsec.c). FreeBSD's IPsec stack handles all IPsec; flows into a tunnel never match the hardware's routes, so cmm keeps them on the software path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
96a812fa5b
commit
f1a16265a8
14 changed files with 15 additions and 4921 deletions
|
|
@ -220,7 +220,6 @@ Wire ──► FMan RX Port
|
|||
| PF → CMM | /dev/pfnotify | State insert/ready/delete events |
|
||||
| CMM ← PF | DIOCGETSTATESV2 ioctl | Periodic state table poll (fallback) |
|
||||
| CMM ← routing | PF_ROUTE socket | Route/neighbor/interface changes |
|
||||
| CMM ← IPsec | PF_KEY socket | SA add/delete events |
|
||||
| CMM ← bridge | /dev/autobridge | L2 flow learning events |
|
||||
| cmmctl → CMM | Unix socket | Query/control commands |
|
||||
| dpa_app → FMan | /dev/fman chardev | PCD configuration (XML → hardware) |
|
||||
|
|
|
|||
|
|
@ -16,17 +16,16 @@ SRCS= cdx_main_freebsd.c cdx_timer_freebsd.c cdx_cmdhandler_freebsd.c \
|
|||
cdx_ifstats_freebsd.c cdx_debug_freebsd.c \
|
||||
cdx_qos_freebsd.c cdx_mc_freebsd.c \
|
||||
cdx_devoh_freebsd.c cdx_reassm_freebsd.c \
|
||||
cdx_ceetm_freebsd.c \
|
||||
dpa_ipsec_freebsd.c cdx_dpa_ipsec_freebsd.c
|
||||
cdx_ceetm_freebsd.c
|
||||
|
||||
# Tier 1 CDX sources (from original CDX tree, mostly unchanged)
|
||||
SRCS+= cdx_hal.c \
|
||||
layer2.c control_ipv4.c control_ipv6.c control_tx.c control_rx.c \
|
||||
control_vlan.c control_lagg.c control_pppoe.c control_bridge.c control_tunnel.c \
|
||||
control_socket.c control_stat.c control_natpt.c control_qm.c \
|
||||
control_wifi.c control_ipsec.c control_rtp_relay.c \
|
||||
control_wifi.c control_rtp_relay.c \
|
||||
control_pktcap.c \
|
||||
query_Rx.c query_ipsec.c
|
||||
query_Rx.c
|
||||
|
||||
SRCS+= opt_platform.h opt_dpaa.h device_if.h bus_if.h ofw_bus_if.h
|
||||
|
||||
|
|
@ -41,9 +40,10 @@ CFLAGS+= -I${FASTPATH_ROOT}/vendor/cdx
|
|||
CFLAGS+= -DENABLE_EGRESS_QOS
|
||||
CFLAGS+= -DDPAA_FAST_PATH
|
||||
CFLAGS+= -DENABLE_FLOW_TIME_STAMPS
|
||||
# No IPsec offload (DPA_IPSEC_OFFLOAD): its exception path panics on most
|
||||
# decrypted packet sizes, hands packets to the stack without a receive
|
||||
# interface, and skips enc0 filtering (audit V2). cmm opens no PF_KEY socket.
|
||||
# No IPsec offload (DPA_IPSEC_OFFLOAD, and its sources are gone): its
|
||||
# exception path panicked on most decrypted packet sizes, handed packets to
|
||||
# the stack without a receive interface and skipped enc0 filtering (audit
|
||||
# V2). FreeBSD's IPsec stack handles all IPsec.
|
||||
CFLAGS+= -DWIFI_ENABLE
|
||||
|
||||
# NCSW build environment defines (replicate dflags_arm64.h without its
|
||||
|
|
@ -86,11 +86,9 @@ CFLAGS.control_stat.c+= ${VENDOR_WARN}
|
|||
CFLAGS.control_natpt.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_qm.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_wifi.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_ipsec.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_rtp_relay.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_pktcap.c+= ${VENDOR_WARN}
|
||||
CFLAGS.query_Rx.c+= ${VENDOR_WARN}
|
||||
CFLAGS.query_ipsec.c+= ${VENDOR_WARN}
|
||||
|
||||
# cdx_ehash.c is ported vendor code (originally cdx-5.03.1/cdx_ehash.c)
|
||||
CFLAGS.cdx_ehash.c+= ${VENDOR_WARN}
|
||||
|
|
@ -98,8 +96,4 @@ CFLAGS.cdx_ehash.c+= ${VENDOR_WARN}
|
|||
# cdx_mc_freebsd.c is ported vendor code (originally dpa_control_mc.c + cdx_mc_query.c)
|
||||
CFLAGS.cdx_mc_freebsd.c+= ${VENDOR_WARN}
|
||||
|
||||
# IPsec offload — ported from cdx-5.03.1/dpa_ipsec.c + cdx_dpa_ipsec.c
|
||||
CFLAGS.dpa_ipsec_freebsd.c+= ${VENDOR_WARN}
|
||||
CFLAGS.cdx_dpa_ipsec_freebsd.c+= ${VENDOR_WARN}
|
||||
|
||||
.include <bsd.kmod.mk>
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -16,7 +16,6 @@
|
|||
* - cdx_qos_freebsd.c (QoS / policer)
|
||||
* - cdx_mc_freebsd.c (multicast)
|
||||
* - cdx_debug_freebsd.c (debug display)
|
||||
* - dpa_ipsec_freebsd.c (IPsec SA management)
|
||||
* - cdx_reassm_freebsd.c (IP reassembly)
|
||||
* - cdx_ceetm_freebsd.c (CEETM QoS)
|
||||
*
|
||||
|
|
@ -189,10 +188,9 @@ dpa_update_timestamp(uint32_t ts)
|
|||
/* mc4_init, mc6_init, mc4_exit, mc6_exit moved to cdx_mc_freebsd.c */
|
||||
|
||||
/*
|
||||
* M_ipsec_sa_cache_lookup_by_h — real implementation in control_ipsec.c,
|
||||
* which builds only with DPA_IPSEC_OFFLOAD (off, see Makefile). Without it
|
||||
* no SA is ever programmed, so none is found: conntracks that name an SA
|
||||
* are refused by their callers.
|
||||
* M_ipsec_sa_cache_lookup_by_h: IPsec offload is gone (see Makefile), so no
|
||||
* SA is ever programmed and none is found; the conntrack code that asks
|
||||
* refuses conntracks naming an SA.
|
||||
*/
|
||||
#ifndef DPA_IPSEC_OFFLOAD
|
||||
void *M_ipsec_sa_cache_lookup_by_h(U16 handle); /* control_ipsec.h */
|
||||
|
|
|
|||
|
|
@ -292,7 +292,7 @@ cdx_module_init(void)
|
|||
}
|
||||
register_cdx_deinit_func(cdx_deinit_ip_reassembly);
|
||||
|
||||
/* IPsec SA management done via FCI/CMM path (dpa_ipsec_freebsd.c) */
|
||||
/* No IPsec offload: FreeBSD's IPsec stack handles all IPsec */
|
||||
|
||||
exit:
|
||||
if (rc) {
|
||||
|
|
|
|||
|
|
@ -1,723 +0,0 @@
|
|||
/*
|
||||
* DPAA IPsec plumbing layer — FreeBSD port.
|
||||
*
|
||||
* Ported from ASK/cdx-5.03.1/dpa_ipsec.c. Provides the DPAA
|
||||
* infrastructure for CDX inline IPsec offload:
|
||||
* - Per-SA frame queue creation/teardown (FQ_TO_SEC, FQ_FROM_SEC, FQ_TO_CP)
|
||||
* - IPsec OH port discovery and channel setup
|
||||
* - Slow-path exception handler for FQ_TO_CP (decrypted miss packets)
|
||||
* - Buffer pool info for CAAM preheader (BPID/BSIZE)
|
||||
* - sec_descriptor allocation (64-byte aligned)
|
||||
*
|
||||
* Uses FreeBSD NCSW QMan API (qman_fqr_create_ctx, qman_fqr_create,
|
||||
* qman_fqr_register_cb, qman_fqr_free) instead of Linux SDK QMan API.
|
||||
*
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017,2021 NXP
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#include "portdefs.h"
|
||||
#include "cdx.h"
|
||||
#include "cdx_ioctl.h"
|
||||
#include "misc.h"
|
||||
#include "dpa_ipsec.h"
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/malloc.h>
|
||||
#include <sys/mbuf.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_var.h>
|
||||
#include <net/netisr.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <vm/vm.h>
|
||||
#include <vm/pmap.h>
|
||||
#include <vm/uma.h>
|
||||
|
||||
/* DPAA kernel headers */
|
||||
#include <dev/dpaa/qman.h>
|
||||
#include <dev/dpaa/bman.h>
|
||||
#include <dev/dpaa/dpaa_oh.h>
|
||||
|
||||
/* NCSW types */
|
||||
#include <contrib/ncsw/inc/ncsw_ext.h>
|
||||
#include <contrib/ncsw/inc/integrations/dpaa_integration_ext.h>
|
||||
|
||||
MALLOC_DECLARE(M_CDX);
|
||||
|
||||
#define DPAIPSEC_CELL_INDEX 3 /* OH port cell-index for IPsec */
|
||||
#define IPSEC_WQ_ID 2 /* Work queue for per-SA FQs */
|
||||
|
||||
/* Buffer size for IPsec BMan pool — same as FM_PORT_BUFFER_SIZE (9600+64) */
|
||||
#define IPSEC_BUFFER_SIZE (9600 + 64)
|
||||
|
||||
#define DPAIPSEC_ERROR(fmt, ...) \
|
||||
printf("cdx_ipsec: " fmt, ## __VA_ARGS__)
|
||||
#define DPAIPSEC_INFO(fmt, ...) \
|
||||
printf("cdx_ipsec: " fmt, ## __VA_ARGS__)
|
||||
|
||||
/* ================================================================
|
||||
* Per-SA info — holds sec_descriptor + 3 FQR handles
|
||||
* ================================================================ */
|
||||
|
||||
struct dpa_ipsec_sainfo {
|
||||
void *shdesc_mem; /* raw allocation (for free) */
|
||||
struct sec_descriptor *shared_desc; /* 64-byte aligned */
|
||||
t_Handle fqr[NUM_FQS_PER_SA]; /* NCSW FQR handles */
|
||||
uint32_t fqid[NUM_FQS_PER_SA]; /* resolved FQIDs */
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* IPsec instance singleton — one per CDX module lifetime
|
||||
* ================================================================ */
|
||||
|
||||
struct ipsec_info {
|
||||
uint32_t crypto_channel_id; /* CAAM DCP channel */
|
||||
device_t oh_dev; /* OH port device_t */
|
||||
uint32_t ofport_channel; /* OH port QMan channel */
|
||||
uint32_t ipsec_bpid; /* buffer pool ID for CAAM output */
|
||||
uint32_t ipsec_buf_size; /* buffer size in that pool */
|
||||
t_Handle ipsec_pool; /* BMan pool handle */
|
||||
uma_zone_t ipsec_zone; /* UMA zone for pool buffers */
|
||||
int initialized;
|
||||
};
|
||||
|
||||
static struct ipsec_info ipsecinfo;
|
||||
|
||||
/* Interface list from cdx_devman_freebsd.c / cdx_dpa_stub.c */
|
||||
extern struct dpa_iface_info *dpa_interface_info;
|
||||
|
||||
/* ================================================================
|
||||
* Accessor functions — called by Tier 1 control_ipsec.c
|
||||
* ================================================================ */
|
||||
|
||||
void *
|
||||
dpa_get_ipsec_instance(void)
|
||||
{
|
||||
|
||||
return (&ipsecinfo);
|
||||
}
|
||||
|
||||
struct sec_descriptor *
|
||||
get_shared_desc(void *handle)
|
||||
{
|
||||
struct dpa_ipsec_sainfo *sa = (struct dpa_ipsec_sainfo *)handle;
|
||||
|
||||
return (sa->shared_desc);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
get_fqid_to_sec(void *handle)
|
||||
{
|
||||
struct dpa_ipsec_sainfo *sa = (struct dpa_ipsec_sainfo *)handle;
|
||||
|
||||
return (sa->fqid[FQ_TO_SEC]);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
get_fqid_from_sec(void *handle)
|
||||
{
|
||||
struct dpa_ipsec_sainfo *sa = (struct dpa_ipsec_sainfo *)handle;
|
||||
|
||||
return (sa->fqid[FQ_FROM_SEC]);
|
||||
}
|
||||
|
||||
#ifdef UNIQUE_IPSEC_CP_FQID
|
||||
uint32_t
|
||||
ipsec_get_to_cp_fqid(void *handle)
|
||||
{
|
||||
struct dpa_ipsec_sainfo *sa = (struct dpa_ipsec_sainfo *)handle;
|
||||
|
||||
return (sa->fqid[FQ_TO_CP]);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ================================================================
|
||||
* OH port table descriptor — used for classification table lookups
|
||||
* ================================================================ */
|
||||
|
||||
/* From cdx_devoh_freebsd.c */
|
||||
extern void *get_oh_port_td(uint32_t fm_index, uint32_t port_idx,
|
||||
uint32_t type);
|
||||
|
||||
int
|
||||
dpa_ipsec_ofport_td(struct ipsec_info *info __unused, uint32_t table_type,
|
||||
void **td, uint32_t *portid)
|
||||
{
|
||||
struct dpa_iface_info *p;
|
||||
|
||||
if (table_type >= MAX_MATCH_TABLES) {
|
||||
DPAIPSEC_ERROR("invalid table type %u\n", table_type);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Walk interface list to find the IPsec OH port.
|
||||
* Table descriptors are stored in cdx_devoh_freebsd.c's
|
||||
* offline_port_info[][] — use get_oh_port_td() accessor.
|
||||
*/
|
||||
for (p = dpa_interface_info; p != NULL; p = p->next) {
|
||||
if ((p->if_flags & (1 << 8)) == 0) /* IF_TYPE_OFPORT */
|
||||
continue;
|
||||
if (p->oh_info.port_idx != DPAIPSEC_CELL_INDEX)
|
||||
continue;
|
||||
|
||||
*td = get_oh_port_td(p->oh_info.fman_idx,
|
||||
p->oh_info.port_idx, table_type);
|
||||
*portid = p->oh_info.portid;
|
||||
return (0);
|
||||
}
|
||||
|
||||
DPAIPSEC_ERROR("IPsec OH port not found in interface list\n");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Buffer pool info — used by preheader for CAAM output buffers
|
||||
* ================================================================ */
|
||||
|
||||
int
|
||||
cdx_dpa_get_ipsec_pool_info(uint32_t *bpid, uint32_t *buf_size)
|
||||
{
|
||||
|
||||
if (!ipsecinfo.initialized) {
|
||||
DPAIPSEC_ERROR("ipsec not initialized\n");
|
||||
return (-1);
|
||||
}
|
||||
*bpid = ipsecinfo.ipsec_bpid;
|
||||
*buf_size = ipsecinfo.ipsec_buf_size;
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Dedicated IPsec BMan buffer pool.
|
||||
*
|
||||
* CAAM QI acquires output buffers from BMan using the BPID in the
|
||||
* preheader. Using a dedicated pool avoids competing with FMan RX
|
||||
* for buffers from the dtsec pool — the same starvation pattern
|
||||
* that caused the dtsec 256→2048 buffer fix.
|
||||
*/
|
||||
static t_Error
|
||||
ipsec_pool_put_buffer(t_Handle h_BufferPool, uint8_t *buffer,
|
||||
t_Handle context __unused)
|
||||
{
|
||||
|
||||
uma_zfree(ipsecinfo.ipsec_zone, buffer);
|
||||
return (E_OK);
|
||||
}
|
||||
|
||||
static uint8_t *
|
||||
ipsec_pool_get_buffer(t_Handle h_BufferPool, t_Handle *context)
|
||||
{
|
||||
|
||||
return (uma_zalloc(ipsecinfo.ipsec_zone, M_NOWAIT));
|
||||
}
|
||||
|
||||
static void
|
||||
ipsec_pool_depleted(t_Handle h_App, bool in)
|
||||
{
|
||||
|
||||
if (!in)
|
||||
return;
|
||||
|
||||
while (bman_count(ipsecinfo.ipsec_pool) < IPSEC_BUFCOUNT)
|
||||
bman_pool_fill(ipsecinfo.ipsec_pool, IPSEC_BUFCOUNT);
|
||||
}
|
||||
|
||||
static int
|
||||
ipsec_pool_create(struct ipsec_info *info)
|
||||
{
|
||||
uint8_t bpid;
|
||||
|
||||
info->ipsec_buf_size = IPSEC_BUFFER_SIZE;
|
||||
|
||||
info->ipsec_zone = uma_zcreate("cdx_ipsec_bufs", IPSEC_BUFFER_SIZE,
|
||||
NULL, NULL, NULL, NULL, 255 /* 256-byte align for DMA */, 0);
|
||||
if (info->ipsec_zone == NULL) {
|
||||
DPAIPSEC_ERROR("failed to create UMA zone\n");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
info->ipsec_pool = bman_pool_create(&bpid,
|
||||
IPSEC_BUFFER_SIZE,
|
||||
0, 0, /* no software stockpile */
|
||||
IPSEC_BUFCOUNT, /* pre-seed 512 buffers */
|
||||
ipsec_pool_get_buffer,
|
||||
ipsec_pool_put_buffer,
|
||||
IPSEC_BUFCOUNT / 4, /* depletion entry threshold */
|
||||
IPSEC_BUFCOUNT / 2, /* depletion exit threshold */
|
||||
0, 0, /* no HW depletion */
|
||||
ipsec_pool_depleted, info, NULL, NULL);
|
||||
if (info->ipsec_pool == NULL) {
|
||||
DPAIPSEC_ERROR("failed to create BMan pool\n");
|
||||
uma_zdestroy(info->ipsec_zone);
|
||||
info->ipsec_zone = NULL;
|
||||
return (-1);
|
||||
}
|
||||
info->ipsec_bpid = bpid;
|
||||
|
||||
DPAIPSEC_INFO("IPsec pool: bpid %u, buf_size %u, count %u\n",
|
||||
info->ipsec_bpid, info->ipsec_buf_size,
|
||||
bman_count(info->ipsec_pool));
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void
|
||||
ipsec_pool_destroy(struct ipsec_info *info)
|
||||
{
|
||||
|
||||
if (info->ipsec_pool != NULL) {
|
||||
bman_pool_destroy(info->ipsec_pool);
|
||||
info->ipsec_pool = NULL;
|
||||
}
|
||||
if (info->ipsec_zone != NULL) {
|
||||
uma_zdestroy(info->ipsec_zone);
|
||||
info->ipsec_zone = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Slow-path exception handler — FQ_TO_CP callback
|
||||
*
|
||||
* Decrypted packets that don't match any fast-path flow entry
|
||||
* arrive here via the OH port's miss path. Convert the BMan
|
||||
* buffer to an mbuf and deliver to the FreeBSD network stack.
|
||||
* ================================================================ */
|
||||
|
||||
static uint32_t ipsec_exception_pkt_cnt;
|
||||
|
||||
static e_RxStoreResponse
|
||||
ipsec_exception_pkt_handler(t_Handle app __unused, t_Handle qm_fqr __unused,
|
||||
t_Handle qm_portal __unused, uint32_t fqid_offset __unused,
|
||||
t_DpaaFD *fd)
|
||||
{
|
||||
struct mbuf *m;
|
||||
void *frame_va;
|
||||
uint32_t fd_status, fd_length, fd_offset;
|
||||
uint8_t fd_format;
|
||||
|
||||
ipsec_exception_pkt_cnt++;
|
||||
|
||||
/*
|
||||
* Only handle short single-buffer (SBSF) frames for now.
|
||||
* Format is in elion bits [7:5] (NCSW t_DpaaFD).
|
||||
*/
|
||||
fd_format = (fd->elion >> 5) & 0x3;
|
||||
if (fd_format != 0) {
|
||||
DPAIPSEC_ERROR("exception: unsupported FD format %u\n",
|
||||
fd_format);
|
||||
goto release;
|
||||
}
|
||||
|
||||
fd_status = fd->status;
|
||||
/* Short SBSF: length[28:20] = offset (9 bits), length[19:0] = len */
|
||||
fd_length = fd->length & 0xFFFFF;
|
||||
fd_offset = (fd->length >> 20) & 0x1FF;
|
||||
|
||||
/* Check for SEC errors */
|
||||
if (fd_status & 0xFF000000) {
|
||||
DPAIPSEC_ERROR("exception: SEC error status 0x%08x\n",
|
||||
fd_status);
|
||||
goto release;
|
||||
}
|
||||
|
||||
/* Get virtual address from physical */
|
||||
frame_va = (void *)PHYS_TO_DMAP(
|
||||
((uint64_t)fd->addrh << 32) | fd->addrl);
|
||||
if (frame_va == NULL)
|
||||
goto release;
|
||||
|
||||
/*
|
||||
* Build an mbuf wrapping the frame data.
|
||||
* The decrypted frame starts at frame_va + fd_offset.
|
||||
*/
|
||||
m = m_gethdr(M_NOWAIT, MT_DATA);
|
||||
if (m == NULL)
|
||||
goto release;
|
||||
|
||||
/*
|
||||
* Copy frame data into the mbuf. For the exception path (slow
|
||||
* path), a copy is acceptable. The BMan buffer is released
|
||||
* immediately after.
|
||||
*/
|
||||
if (fd_length > MHLEN) {
|
||||
if (m_cljget(m, M_NOWAIT, fd_length) == NULL) {
|
||||
m_freem(m);
|
||||
goto release;
|
||||
}
|
||||
}
|
||||
m_copyback(m, 0, fd_length, (char *)frame_va + fd_offset);
|
||||
m->m_pkthdr.len = fd_length;
|
||||
m->m_len = fd_length;
|
||||
|
||||
/*
|
||||
* Deliver the decrypted packet to the IP input path.
|
||||
* The frame should be a raw IP packet (ESP headers stripped).
|
||||
*/
|
||||
{
|
||||
struct ip *ip;
|
||||
|
||||
if (m->m_len < (int)sizeof(struct ip)) {
|
||||
m_freem(m);
|
||||
goto release;
|
||||
}
|
||||
ip = mtod(m, struct ip *);
|
||||
if (ip->ip_v == IPVERSION) {
|
||||
m->m_pkthdr.rcvif = NULL;
|
||||
netisr_dispatch(NETISR_IP, m);
|
||||
} else if (ip->ip_v == 6) {
|
||||
m->m_pkthdr.rcvif = NULL;
|
||||
netisr_dispatch(NETISR_IPV6, m);
|
||||
} else {
|
||||
DPAIPSEC_ERROR("exception: unknown IP version %u\n",
|
||||
ip->ip_v);
|
||||
m_freem(m);
|
||||
}
|
||||
}
|
||||
|
||||
release:
|
||||
/* Release BMan buffer back to the IPsec pool */
|
||||
if (ipsecinfo.ipsec_pool != NULL) {
|
||||
void *buf_va;
|
||||
|
||||
buf_va = (void *)PHYS_TO_DMAP(
|
||||
((uint64_t)fd->addrh << 32) | fd->addrl);
|
||||
if (buf_va != NULL)
|
||||
bman_put_buffer(ipsecinfo.ipsec_pool, buf_va);
|
||||
}
|
||||
return (e_RX_STORE_RESPONSE_CONTINUE);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Per-SA allocation / teardown
|
||||
* ================================================================ */
|
||||
|
||||
/*
|
||||
* Allocate per-SA resources: sec_descriptor + 3 FQs.
|
||||
*
|
||||
* FQ creation order (to resolve FQID dependencies):
|
||||
* 1. FQ_TO_CP — SW portal channel, DQRR callback
|
||||
* 2. FQ_FROM_SEC — OH port channel, context_b = FQ_TO_CP FQID
|
||||
* 3. FQ_TO_SEC — CAAM DCP channel, context_a = sec_desc phys,
|
||||
* context_b = FQ_FROM_SEC FQID
|
||||
*/
|
||||
void *
|
||||
cdx_dpa_ipsecsa_alloc(struct ipsec_info *info, uint32_t handle __unused)
|
||||
{
|
||||
struct dpa_ipsec_sainfo *sainfo;
|
||||
t_QmContextA context_a;
|
||||
t_QmContextB context_b;
|
||||
uint64_t desc_pa;
|
||||
|
||||
if (!info || !ipsecinfo.initialized) {
|
||||
DPAIPSEC_ERROR("ipsec not initialized in sa_alloc\n");
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
sainfo = malloc(sizeof(*sainfo), M_CDX, M_WAITOK | M_ZERO);
|
||||
|
||||
/*
|
||||
* Allocate sec_descriptor with 64-byte alignment.
|
||||
* Raw allocation stored for free; aligned pointer for use.
|
||||
*/
|
||||
sainfo->shdesc_mem = malloc(sizeof(struct sec_descriptor) +
|
||||
PRE_HDR_ALIGN, M_CDX, M_WAITOK | M_ZERO);
|
||||
sainfo->shared_desc = (struct sec_descriptor *)
|
||||
roundup2((uintptr_t)sainfo->shdesc_mem, PRE_HDR_ALIGN);
|
||||
|
||||
/*
|
||||
* Step 1: Create FQ_TO_CP — scheduled to CPU 0's SW portal.
|
||||
* This FQ receives decrypted slow-path packets from the OH port.
|
||||
*/
|
||||
sainfo->fqr[FQ_TO_CP] = qman_fqr_create(1,
|
||||
(e_QmFQChannel)e_QM_FQ_CHANNEL_SWPORTAL0,
|
||||
IPSEC_WQ_ID,
|
||||
FALSE, /* don't force FQID */
|
||||
0, /* alignment */
|
||||
FALSE, /* not parked */
|
||||
TRUE, /* holdActive — keep portal affinity */
|
||||
TRUE, /* preferInCache */
|
||||
FALSE, /* no congestion avoidance */
|
||||
NULL, /* no CG handle */
|
||||
0, /* no overhead accounting */
|
||||
0); /* no tail drop */
|
||||
if (sainfo->fqr[FQ_TO_CP] == NULL) {
|
||||
DPAIPSEC_ERROR("failed to create FQ_TO_CP\n");
|
||||
goto err_cp;
|
||||
}
|
||||
sainfo->fqid[FQ_TO_CP] = qman_fqr_get_base_fqid(sainfo->fqr[FQ_TO_CP]);
|
||||
|
||||
/* Register DQRR callback for exception packets */
|
||||
if (qman_fqr_register_cb(sainfo->fqr[FQ_TO_CP],
|
||||
ipsec_exception_pkt_handler, NULL) != E_OK) {
|
||||
DPAIPSEC_ERROR("failed to register FQ_TO_CP callback\n");
|
||||
goto err_from;
|
||||
}
|
||||
|
||||
/*
|
||||
* Step 2: Create FQ_FROM_SEC — OH port channel.
|
||||
*
|
||||
* Context-A flags:
|
||||
* OVERRIDE_FQ — override destination FQ with context_b FQID
|
||||
* A1_FIELD_VALID — enable A1 field (SEC error check)
|
||||
* A1 value = 2 — check SEC error status
|
||||
*
|
||||
* Context-B = FQ_TO_CP FQID (miss destination)
|
||||
*/
|
||||
memset(&context_a, 0, sizeof(context_a));
|
||||
context_a.res[0] =
|
||||
(((CDX_FQD_CTX_A_OVERRIDE_FQ | CDX_FQD_CTX_A_A1_FIELD_VALID)
|
||||
<< CDX_FQD_CTX_A_SHIFT_BITS) |
|
||||
CDX_FQD_CTX_A_A1_VAL_TO_CHECK_SECERR);
|
||||
context_b = sainfo->fqid[FQ_TO_CP];
|
||||
|
||||
sainfo->fqr[FQ_FROM_SEC] = qman_fqr_create_ctx(1,
|
||||
(e_QmFQChannel)ipsecinfo.ofport_channel,
|
||||
IPSEC_WQ_ID,
|
||||
FALSE, /* don't force FQID */
|
||||
0, /* alignment */
|
||||
TRUE, /* preferInCache */
|
||||
&context_a,
|
||||
&context_b);
|
||||
if (sainfo->fqr[FQ_FROM_SEC] == NULL) {
|
||||
DPAIPSEC_ERROR("failed to create FQ_FROM_SEC\n");
|
||||
goto err_from;
|
||||
}
|
||||
sainfo->fqid[FQ_FROM_SEC] = qman_fqr_get_base_fqid(
|
||||
sainfo->fqr[FQ_FROM_SEC]);
|
||||
|
||||
/*
|
||||
* Step 3: Create FQ_TO_SEC — CAAM DCP channel.
|
||||
*
|
||||
* Context-A = physical address of sec_descriptor (preheader + shdesc).
|
||||
* CAAM QI reads the shared descriptor from this address.
|
||||
*
|
||||
* Context-B = FQ_FROM_SEC FQID (CAAM response destination).
|
||||
*/
|
||||
desc_pa = vtophys(sainfo->shared_desc);
|
||||
memset(&context_a, 0, sizeof(context_a));
|
||||
context_a.res[0] = (uint32_t)(desc_pa >> 32);
|
||||
context_a.res[1] = (uint32_t)(desc_pa);
|
||||
context_b = sainfo->fqid[FQ_FROM_SEC];
|
||||
|
||||
sainfo->fqr[FQ_TO_SEC] = qman_fqr_create_ctx(1,
|
||||
e_QM_FQ_CHANNEL_CAAM,
|
||||
IPSEC_WQ_ID,
|
||||
FALSE, /* don't force FQID */
|
||||
0, /* alignment */
|
||||
TRUE, /* preferInCache */
|
||||
&context_a,
|
||||
&context_b);
|
||||
if (sainfo->fqr[FQ_TO_SEC] == NULL) {
|
||||
DPAIPSEC_ERROR("failed to create FQ_TO_SEC\n");
|
||||
goto err_to;
|
||||
}
|
||||
sainfo->fqid[FQ_TO_SEC] = qman_fqr_get_base_fqid(
|
||||
sainfo->fqr[FQ_TO_SEC]);
|
||||
|
||||
DPAIPSEC_INFO("SA alloc: to_sec=%u from_sec=%u to_cp=%u desc_pa=0x%lx\n",
|
||||
sainfo->fqid[FQ_TO_SEC], sainfo->fqid[FQ_FROM_SEC],
|
||||
sainfo->fqid[FQ_TO_CP], (unsigned long)desc_pa);
|
||||
|
||||
return (sainfo);
|
||||
|
||||
err_to:
|
||||
qman_fqr_free(sainfo->fqr[FQ_FROM_SEC]);
|
||||
err_from:
|
||||
if (sainfo->fqr[FQ_TO_CP] != NULL)
|
||||
qman_fqr_free(sainfo->fqr[FQ_TO_CP]);
|
||||
err_cp:
|
||||
zfree(sainfo->shdesc_mem, M_CDX); /* the SA's keys, inline */
|
||||
zfree(sainfo, M_CDX);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Retire a single per-SA FQ.
|
||||
*
|
||||
* On Linux, this calls qman_retire_fq() which transitions the FQ to
|
||||
* "retiring" state asynchronously. On FreeBSD, NCSW qman_fqr_free()
|
||||
* does full retire+OOS synchronously. We just free the FQ here and
|
||||
* NULL it out so cdx_dpa_ipsecsa_release() skips it.
|
||||
*
|
||||
* Returns: 0 = FQ already retired/freed, 1 = still retiring (never on NCSW).
|
||||
*/
|
||||
int
|
||||
cdx_dpa_ipsec_retire_fq(void *handle, int fq_num)
|
||||
{
|
||||
struct dpa_ipsec_sainfo *sainfo;
|
||||
|
||||
if (handle == NULL)
|
||||
return (0);
|
||||
|
||||
sainfo = (struct dpa_ipsec_sainfo *)handle;
|
||||
|
||||
if (fq_num < 0 || fq_num >= NUM_FQS_PER_SA)
|
||||
return (0);
|
||||
|
||||
if (sainfo->fqr[fq_num] != NULL) {
|
||||
qman_fqr_free(sainfo->fqr[fq_num]);
|
||||
sainfo->fqr[fq_num] = NULL;
|
||||
}
|
||||
|
||||
return (0); /* synchronous — always immediately retired */
|
||||
}
|
||||
|
||||
/*
|
||||
* Check if a per-SA FQ is in retired state.
|
||||
*
|
||||
* On FreeBSD, qman_fqr_free() is synchronous so the FQ is always
|
||||
* fully retired (and freed) after cdx_dpa_ipsec_retire_fq().
|
||||
*
|
||||
* Returns: 0 = retired (OK to proceed), nonzero = still not retired.
|
||||
*/
|
||||
int
|
||||
cdx_ipsec_sa_fq_check_if_retired_state(void *handle, int fq_num)
|
||||
{
|
||||
struct dpa_ipsec_sainfo *sainfo;
|
||||
|
||||
if (handle == NULL)
|
||||
return (0); /* treat NULL as "already retired" */
|
||||
|
||||
sainfo = (struct dpa_ipsec_sainfo *)handle;
|
||||
|
||||
if (fq_num < 0 || fq_num >= NUM_FQS_PER_SA)
|
||||
return (0);
|
||||
|
||||
/* If FQR is NULL, it's already freed (retired). Return 0 = OK. */
|
||||
return (sainfo->fqr[fq_num] != NULL ? 1 : 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Release all per-SA resources: tear down FQs, free descriptor memory.
|
||||
*/
|
||||
int
|
||||
cdx_dpa_ipsecsa_release(void *handle)
|
||||
{
|
||||
struct dpa_ipsec_sainfo *sainfo;
|
||||
int ii;
|
||||
|
||||
if (handle == NULL)
|
||||
return (-1);
|
||||
|
||||
sainfo = (struct dpa_ipsec_sainfo *)handle;
|
||||
|
||||
/* Free FQRs in reverse order */
|
||||
for (ii = NUM_FQS_PER_SA - 1; ii >= 0; ii--) {
|
||||
if (sainfo->fqr[ii] != NULL) {
|
||||
qman_fqr_free(sainfo->fqr[ii]);
|
||||
sainfo->fqr[ii] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
zfree(sainfo->shdesc_mem, M_CDX); /* the SA's keys, inline */
|
||||
zfree(sainfo, M_CDX);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* XFRM state stubs
|
||||
*
|
||||
* On Linux, these hold/release a reference to the kernel's xfrm_state
|
||||
* to keep it alive while CDX offloads the SA. On FreeBSD there is no
|
||||
* XFRM framework — CDX manages SA lifecycle independently via FCI
|
||||
* commands from CMM. Return a non-NULL sentinel so the Tier 1 NULL
|
||||
* check in control_ipsec.c passes.
|
||||
* ================================================================ */
|
||||
|
||||
#define CDX_XFRM_SENTINEL ((void *)(uintptr_t)0x1)
|
||||
|
||||
void *
|
||||
cdx_get_xfrm_state_of_sa(void *dev __unused, uint16_t handle __unused)
|
||||
{
|
||||
|
||||
return (CDX_XFRM_SENTINEL);
|
||||
}
|
||||
|
||||
void
|
||||
cdx_dpa_ipsec_xfrm_state_dec_ref_cnt(void *xfrm_state __unused)
|
||||
{
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* SG / skb-free buffer pool stubs
|
||||
*
|
||||
* On Linux, these create dedicated BMan pools for scatter-gather
|
||||
* and skb recycling. On FreeBSD, mbuf allocation handles this
|
||||
* natively. Provide stub implementations that succeed.
|
||||
* ================================================================ */
|
||||
|
||||
int
|
||||
cdx_init_scatter_gather_bpool(void)
|
||||
{
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
cdx_init_skb_2bfreed_bpool(void)
|
||||
{
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Module init / exit
|
||||
* ================================================================ */
|
||||
|
||||
int
|
||||
cdx_dpa_ipsec_init(void)
|
||||
{
|
||||
device_t oh_dev;
|
||||
|
||||
DPAIPSEC_INFO("initializing IPsec DPAA plumbing\n");
|
||||
|
||||
memset(&ipsecinfo, 0, sizeof(ipsecinfo));
|
||||
|
||||
/* CAAM DCP channel for QI submissions */
|
||||
ipsecinfo.crypto_channel_id = (uint32_t)e_QM_FQ_CHANNEL_CAAM;
|
||||
|
||||
/* Discover IPsec OH port (cell-index 3) */
|
||||
oh_dev = dpaa_oh_find_port(DPAIPSEC_CELL_INDEX);
|
||||
if (oh_dev == NULL) {
|
||||
DPAIPSEC_ERROR("IPsec OH port (cell-index %d) not found\n",
|
||||
DPAIPSEC_CELL_INDEX);
|
||||
return (-1);
|
||||
}
|
||||
ipsecinfo.oh_dev = oh_dev;
|
||||
ipsecinfo.ofport_channel = dpaa_oh_get_qman_channel(oh_dev);
|
||||
|
||||
DPAIPSEC_INFO("OH port: cell-index %d, channel 0x%x\n",
|
||||
DPAIPSEC_CELL_INDEX, ipsecinfo.ofport_channel);
|
||||
|
||||
/* Create dedicated IPsec BMan buffer pool */
|
||||
if (ipsec_pool_create(&ipsecinfo) != 0) {
|
||||
DPAIPSEC_ERROR("failed to create IPsec buffer pool\n");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
ipsecinfo.initialized = 1;
|
||||
|
||||
DPAIPSEC_INFO("IPsec DPAA plumbing initialized: "
|
||||
"CAAM channel 0x%x, OH channel 0x%x, bpid %u, buf_size %u\n",
|
||||
ipsecinfo.crypto_channel_id, ipsecinfo.ofport_channel,
|
||||
ipsecinfo.ipsec_bpid, ipsecinfo.ipsec_buf_size);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cdx_dpa_ipsec_exit(void)
|
||||
{
|
||||
|
||||
DPAIPSEC_INFO("IPsec DPAA plumbing exiting\n");
|
||||
ipsecinfo.initialized = 0;
|
||||
ipsec_pool_destroy(&ipsecinfo);
|
||||
}
|
||||
|
|
@ -25,7 +25,6 @@ SRCS= cmm.c \
|
|||
cmm_fe.c \
|
||||
cmm_lagg.c \
|
||||
cmm_vlan.c \
|
||||
cmm_ipsec.c \
|
||||
cmm_tunnel.c \
|
||||
cmm_ctrl.c \
|
||||
cmm_deny.c \
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ FCI_HDR= ${SRCDIR}/fci
|
|||
|
||||
CMM_SRCS= cmm.c cmm_conn.c cmm_route.c cmm_neigh.c \
|
||||
cmm_itf.c cmm_rtsock.c cmm_fe.c \
|
||||
cmm_lagg.c cmm_vlan.c cmm_ipsec.c cmm_tunnel.c cmm_deny.c \
|
||||
cmm_lagg.c cmm_vlan.c cmm_tunnel.c cmm_deny.c \
|
||||
cmm_socket.c cmm_bridge.c cmm_ctrl.c cmm_l2tp.c \
|
||||
cmm_wifi.c cmm_pppoe.c cmm_mcast.c
|
||||
CMM_OBJS= ${CMM_SRCS:.c=.o}
|
||||
|
|
|
|||
|
|
@ -34,7 +34,6 @@
|
|||
#include "cmm_fe.h"
|
||||
#include "cmm_lagg.h"
|
||||
#include "cmm_vlan.h"
|
||||
#include "cmm_ipsec.h"
|
||||
#include "cmm_tunnel.h"
|
||||
#include "cmm_l2tp.h"
|
||||
#include "cmm_deny.h"
|
||||
|
|
@ -104,7 +103,6 @@ main(int argc, char *argv[])
|
|||
g->rtsock_fd = -1;
|
||||
g->rtsock_query_fd = -1;
|
||||
g->kq = -1;
|
||||
g->pfkey_fd = -1;
|
||||
g->ctrl_listen_fd = -1;
|
||||
g->autobridge_fd = -1;
|
||||
g->pfnotify_fd = -1;
|
||||
|
|
@ -300,17 +298,11 @@ main(int argc, char *argv[])
|
|||
cmm_pppoe_init(g);
|
||||
cmm_mcast_init();
|
||||
|
||||
if (cmm_ipsec_init() < 0)
|
||||
goto out;
|
||||
|
||||
/* Load deny rules (optional — missing file means offload everything) */
|
||||
if (cmm_deny_init(deny_conf) < 0)
|
||||
goto out;
|
||||
|
||||
/*
|
||||
* No PF_KEY socket: IPsec SAs are not offloaded (cdx is built without
|
||||
* DPA_IPSEC_OFFLOAD; see fastpath/cdx/Makefile). pfkey_fd stays -1.
|
||||
*/
|
||||
/* IPsec is never offloaded: FreeBSD's IPsec stack handles it all */
|
||||
|
||||
/* Open /dev/pfnotify — required for PF state events */
|
||||
{
|
||||
|
|
@ -357,13 +349,6 @@ main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
|
||||
/* PF_KEY socket: IPsec SA events */
|
||||
if (g->pfkey_fd >= 0) {
|
||||
EV_SET(&kev[nev], g->pfkey_fd, EVFILT_READ,
|
||||
EV_ADD, 0, 0, NULL);
|
||||
nev++;
|
||||
}
|
||||
|
||||
/* Control socket: external tool commands (cmmctl) */
|
||||
if (cmm_ctrl_init(g) == 0) {
|
||||
EV_SET(&kev[nev], g->ctrl_listen_fd, EVFILT_READ,
|
||||
|
|
@ -394,10 +379,10 @@ main(int argc, char *argv[])
|
|||
nev++;
|
||||
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"fds: rtsock=%d fci=%d pfkey=%d ctrl=%d autobridge=%d pfnotify=%d",
|
||||
"fds: rtsock=%d fci=%d ctrl=%d autobridge=%d pfnotify=%d",
|
||||
g->rtsock_fd,
|
||||
g->fci_catch ? fci_fd(g->fci_catch) : -1,
|
||||
g->pfkey_fd, g->ctrl_listen_fd,
|
||||
g->ctrl_listen_fd,
|
||||
g->autobridge_fd, g->pfnotify_fd);
|
||||
|
||||
/* Validate: drop any EVFILT_READ with invalid fd */
|
||||
|
|
@ -458,9 +443,6 @@ main(int argc, char *argv[])
|
|||
} else if ((int)events[i].ident ==
|
||||
g->rtsock_fd) {
|
||||
cmm_rtsock_dispatch(g);
|
||||
} else if (g->pfkey_fd >= 0 &&
|
||||
(int)events[i].ident == g->pfkey_fd) {
|
||||
cmm_pfkey_dispatch(g);
|
||||
} else if (events[i].udata ==
|
||||
(void *)(uintptr_t)3) {
|
||||
/* auto_bridge: L2 flow event */
|
||||
|
|
@ -482,7 +464,6 @@ main(int argc, char *argv[])
|
|||
out:
|
||||
/* Clean shutdown */
|
||||
cmm_ctrl_fini(g);
|
||||
cmm_sa_flush_all(g);
|
||||
cmm_conn_deregister_all(g);
|
||||
cmm_mcast_fini();
|
||||
cmm_wifi_fini(g);
|
||||
|
|
@ -495,7 +476,6 @@ out:
|
|||
cmm_fe_reset(g);
|
||||
|
||||
cmm_deny_fini();
|
||||
cmm_ipsec_fini(g);
|
||||
cmm_conn_fini(g);
|
||||
cmm_route_fini();
|
||||
cmm_neigh_fini();
|
||||
|
|
@ -503,8 +483,6 @@ out:
|
|||
|
||||
if (g->kq >= 0)
|
||||
close(g->kq);
|
||||
if (g->pfkey_fd >= 0)
|
||||
close(g->pfkey_fd);
|
||||
if (g->rtsock_fd >= 0)
|
||||
close(g->rtsock_fd);
|
||||
if (g->rtsock_query_fd >= 0)
|
||||
|
|
|
|||
|
|
@ -54,7 +54,6 @@ struct cmm_global {
|
|||
int rtsock_fd; /* PF_ROUTE socket (monitoring) */
|
||||
int rtsock_query_fd;/* PF_ROUTE socket (RTM_GET queries) */
|
||||
int kq; /* kqueue fd */
|
||||
int pfkey_fd; /* PF_KEY socket */
|
||||
int ctrl_listen_fd; /* control socket (listen) */
|
||||
int ctrl_clients[4];/* control socket clients */
|
||||
int autobridge_fd; /* /dev/autobridge (L2 bridge) */
|
||||
|
|
|
|||
|
|
@ -1,688 +0,0 @@
|
|||
/*
|
||||
* cmm_ipsec.c -- IPsec SA management via PF_KEY
|
||||
*
|
||||
* Monitors PF_KEY socket for SA events from setkey(8) / strongswan,
|
||||
* translates them into FCI commands for CDX hardware offload.
|
||||
*
|
||||
* Flow: setkey(8) -> PF_KEY socket -> CMM -> fci_write() -> CDX -> CAAM HW
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* Copyright (C) 2007 Mindspeed Technologies, Inc.
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017, 2021 NXP
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <net/pfkeyv2.h>
|
||||
#include <netipsec/ipsec.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_ipsec.h"
|
||||
#include "cmm_itf.h"
|
||||
|
||||
/* SA hash table */
|
||||
static struct list_head sa_hash[CMM_SA_HASH_SIZE];
|
||||
static uint16_t next_sagd = 1;
|
||||
|
||||
static uint32_t
|
||||
sa_hash_fn(uint32_t spi, const uint8_t *dst, sa_family_t af)
|
||||
{
|
||||
uint32_t h;
|
||||
|
||||
h = spi;
|
||||
if (af == AF_INET)
|
||||
h ^= *(const uint32_t *)dst;
|
||||
else
|
||||
h ^= *(const uint32_t *)dst ^
|
||||
*(const uint32_t *)(dst + 4) ^
|
||||
*(const uint32_t *)(dst + 8) ^
|
||||
*(const uint32_t *)(dst + 12);
|
||||
return (h & (CMM_SA_HASH_SIZE - 1));
|
||||
}
|
||||
|
||||
static struct cmm_sa_entry *
|
||||
sa_find(uint32_t spi, sa_family_t af, const uint8_t *dst)
|
||||
{
|
||||
uint32_t bucket;
|
||||
struct list_head *pos, *head;
|
||||
|
||||
bucket = sa_hash_fn(spi, dst, af);
|
||||
head = &sa_hash[bucket];
|
||||
for (pos = list_first(head); pos != head; pos = list_next(pos)) {
|
||||
struct cmm_sa_entry *sa;
|
||||
|
||||
sa = container_of(pos, struct cmm_sa_entry, list);
|
||||
if (sa->spi == spi && sa->af == af &&
|
||||
memcmp(sa->dst_addr, dst,
|
||||
af == AF_INET ? 4 : 16) == 0)
|
||||
return (sa);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static int
|
||||
sa_sagd_in_use(uint16_t sagd)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < CMM_SA_HASH_SIZE; i++) {
|
||||
struct list_head *pos, *head;
|
||||
|
||||
head = &sa_hash[i];
|
||||
for (pos = list_first(head); pos != head;
|
||||
pos = list_next(pos)) {
|
||||
struct cmm_sa_entry *sa;
|
||||
|
||||
sa = container_of(pos, struct cmm_sa_entry, list);
|
||||
if (sa->sagd == sagd)
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static uint16_t
|
||||
sa_alloc_sagd(void)
|
||||
{
|
||||
uint16_t sagd;
|
||||
int tries;
|
||||
|
||||
for (tries = 0; tries < 65534; tries++) {
|
||||
sagd = next_sagd++;
|
||||
if (next_sagd == 0 || next_sagd == 0xFFFF)
|
||||
next_sagd = 1;
|
||||
if (!sa_sagd_in_use(sagd))
|
||||
return (sagd);
|
||||
}
|
||||
return (0); /* exhausted */
|
||||
}
|
||||
|
||||
static void
|
||||
pfkey_parse_extensions(const struct sadb_msg *msg, struct pfkey_parsed *p)
|
||||
{
|
||||
const struct sadb_ext *ext;
|
||||
const uint8_t *end;
|
||||
size_t msglen;
|
||||
|
||||
memset(p, 0, sizeof(*p));
|
||||
|
||||
msglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
|
||||
end = (const uint8_t *)msg + msglen;
|
||||
ext = (const struct sadb_ext *)(msg + 1);
|
||||
|
||||
while ((const uint8_t *)ext + sizeof(*ext) <= end) {
|
||||
size_t extlen;
|
||||
|
||||
extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
|
||||
if (extlen < sizeof(*ext) ||
|
||||
(const uint8_t *)ext + extlen > end)
|
||||
break;
|
||||
|
||||
switch (ext->sadb_ext_type) {
|
||||
case SADB_EXT_SA:
|
||||
p->sa = (const struct sadb_sa *)ext;
|
||||
break;
|
||||
case SADB_EXT_ADDRESS_SRC:
|
||||
p->addr_src = (const struct sadb_address *)ext;
|
||||
break;
|
||||
case SADB_EXT_ADDRESS_DST:
|
||||
p->addr_dst = (const struct sadb_address *)ext;
|
||||
break;
|
||||
case SADB_EXT_KEY_AUTH:
|
||||
p->key_auth = (const struct sadb_key *)ext;
|
||||
break;
|
||||
case SADB_EXT_KEY_ENCRYPT:
|
||||
p->key_enc = (const struct sadb_key *)ext;
|
||||
break;
|
||||
case SADB_X_EXT_SA2:
|
||||
p->sa2 = (const struct sadb_x_sa2 *)ext;
|
||||
break;
|
||||
}
|
||||
|
||||
ext = (const struct sadb_ext *)
|
||||
((const uint8_t *)ext + extlen);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Extract sockaddr from a PF_KEY address extension.
|
||||
*/
|
||||
static const struct sockaddr *
|
||||
pfkey_addr_sa(const struct sadb_address *addr)
|
||||
{
|
||||
|
||||
return ((const struct sockaddr *)(addr + 1));
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy address bytes (4 for IPv4, 16 for IPv6) from a sockaddr.
|
||||
*/
|
||||
static void
|
||||
pfkey_copy_addr(const struct sockaddr *sa, uint8_t *out)
|
||||
{
|
||||
|
||||
if (sa->sa_family == AF_INET) {
|
||||
const struct sockaddr_in *sin =
|
||||
(const struct sockaddr_in *)sa;
|
||||
memcpy(out, &sin->sin_addr, 4);
|
||||
} else if (sa->sa_family == AF_INET6) {
|
||||
const struct sockaddr_in6 *sin6 =
|
||||
(const struct sockaddr_in6 *)sa;
|
||||
memcpy(out, &sin6->sin6_addr, 16);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy address bytes into a uint32_t[4] array for FCI command structures.
|
||||
* IPv4: first element only, rest zero.
|
||||
*/
|
||||
static void
|
||||
pfkey_copy_addr_u32(const struct sockaddr *sa, unsigned int *out)
|
||||
{
|
||||
|
||||
memset(out, 0, 16);
|
||||
if (sa->sa_family == AF_INET) {
|
||||
const struct sockaddr_in *sin =
|
||||
(const struct sockaddr_in *)sa;
|
||||
memcpy(&out[0], &sin->sin_addr, 4);
|
||||
} else if (sa->sa_family == AF_INET6) {
|
||||
const struct sockaddr_in6 *sin6 =
|
||||
(const struct sockaddr_in6 *)sa;
|
||||
memcpy(out, &sin6->sin6_addr, 16);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
handle_sadb_add(struct cmm_global *g, const struct sadb_msg *msg)
|
||||
{
|
||||
struct pfkey_parsed p;
|
||||
const struct sockaddr *sa_src, *sa_dst;
|
||||
struct cmm_sa_entry *sa;
|
||||
CommandIPSecCreateSA cmd_create;
|
||||
CommandIPSecSetKey cmd_keys;
|
||||
CommandIPSecSetTunnel cmd_tunnel;
|
||||
CommandIPSecSetState cmd_state;
|
||||
uint16_t sagd;
|
||||
uint32_t spi;
|
||||
uint8_t dst_addr[16];
|
||||
sa_family_t af;
|
||||
int is_inbound, is_tunnel;
|
||||
int rc;
|
||||
|
||||
pfkey_parse_extensions(msg, &p);
|
||||
|
||||
/* Validate required extensions */
|
||||
if (p.sa == NULL || p.addr_src == NULL || p.addr_dst == NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"ipsec: SADB_ADD missing required extensions");
|
||||
return;
|
||||
}
|
||||
if (p.key_enc == NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"ipsec: SADB_ADD missing encryption key");
|
||||
return;
|
||||
}
|
||||
|
||||
sa_src = pfkey_addr_sa(p.addr_src);
|
||||
sa_dst = pfkey_addr_sa(p.addr_dst);
|
||||
af = sa_dst->sa_family;
|
||||
spi = p.sa->sadb_sa_spi; /* network byte order */
|
||||
|
||||
memset(dst_addr, 0, sizeof(dst_addr));
|
||||
pfkey_copy_addr(sa_dst, dst_addr);
|
||||
|
||||
/* Check for existing SA */
|
||||
sa = sa_find(spi, af, dst_addr);
|
||||
if (sa != NULL) {
|
||||
if (msg->sadb_msg_type != SADB_UPDATE) {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"ipsec: SA SPI=0x%x already tracked, ignoring",
|
||||
ntohl(spi));
|
||||
return;
|
||||
}
|
||||
/* Rekey: delete old SA from CDX, then recreate with new keys */
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"ipsec: rekey SPI=0x%x, deleting sagd=0x%04x",
|
||||
ntohl(spi), sa->sagd);
|
||||
if (sa->offloaded) {
|
||||
CommandIPSecDeleteSA cmd_del;
|
||||
memset(&cmd_del, 0, sizeof(cmd_del));
|
||||
cmd_del.sagd = sa->sagd;
|
||||
fci_write(g->fci_handle, FPP_CMD_IPSEC_SA_DELETE,
|
||||
sizeof(cmd_del), (unsigned short *)&cmd_del);
|
||||
}
|
||||
list_del(&sa->list);
|
||||
free(sa);
|
||||
}
|
||||
|
||||
/* Allocate SAGD */
|
||||
sagd = sa_alloc_sagd();
|
||||
if (sagd == 0) {
|
||||
cmm_print(CMM_LOG_ERR, "ipsec: SAGD space exhausted");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Direction: if dst is one of our addresses, it's inbound */
|
||||
is_inbound = cmm_itf_is_local_addr(af,
|
||||
af == AF_INET ?
|
||||
(const void *)&((const struct sockaddr_in *)sa_dst)->sin_addr :
|
||||
(const void *)&((const struct sockaddr_in6 *)sa_dst)->sin6_addr);
|
||||
|
||||
/* Tunnel mode? */
|
||||
is_tunnel = (p.sa2 != NULL &&
|
||||
p.sa2->sadb_x_sa2_mode == IPSEC_MODE_TUNNEL);
|
||||
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"ipsec: SADB_ADD ESP SPI=0x%x sagd=0x%04x %s %s",
|
||||
ntohl(spi), sagd,
|
||||
is_inbound ? "inbound" : "outbound",
|
||||
is_tunnel ? "tunnel" : "transport");
|
||||
|
||||
/*
|
||||
* FCI 1: SA_ADD — create the SA in CDX
|
||||
*/
|
||||
memset(&cmd_create, 0, sizeof(cmd_create));
|
||||
cmd_create.sagd = sagd;
|
||||
cmd_create.said.spi = spi;
|
||||
cmd_create.said.sa_type = 0; /* ESP */
|
||||
cmd_create.said.proto_family =
|
||||
(af == AF_INET) ? PROTO_FAMILY_IPV4 : PROTO_FAMILY_IPV6;
|
||||
cmd_create.said.replay_window = p.sa->sadb_sa_replay;
|
||||
cmd_create.said.flags = is_inbound ? NLKEY_SAFLAGS_INBOUND : 0;
|
||||
pfkey_copy_addr_u32(sa_dst, cmd_create.said.dst_ip);
|
||||
pfkey_copy_addr_u32(sa_src, cmd_create.said.src_ip);
|
||||
cmd_create.said.mtu = 1500;
|
||||
cmd_create.said.dev_mtu = 1500;
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPSEC_SA_ADD,
|
||||
sizeof(cmd_create), (unsigned short *)&cmd_create);
|
||||
if (rc != 0) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"ipsec: SA_ADD failed: %d (SPI=0x%x sagd=0x%04x)",
|
||||
rc, ntohl(spi), sagd);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* FCI 2: SA_SET_KEYS
|
||||
*/
|
||||
memset(&cmd_keys, 0, sizeof(cmd_keys));
|
||||
cmd_keys.sagd = sagd;
|
||||
|
||||
if (p.key_auth != NULL && p.key_auth->sadb_key_bits > 0) {
|
||||
size_t auth_len, enc_len;
|
||||
|
||||
/* CBC + HMAC: 2 keys (auth + cipher) */
|
||||
cmd_keys.num_keys = 2;
|
||||
|
||||
auth_len = (p.key_auth->sadb_key_bits + 7) / 8;
|
||||
if (auth_len > IPSEC_MAX_KEY_SIZE) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"ipsec: auth key too large (%zu > %d)",
|
||||
auth_len, IPSEC_MAX_KEY_SIZE);
|
||||
goto fail_delete;
|
||||
}
|
||||
|
||||
enc_len = (p.key_enc->sadb_key_bits + 7) / 8;
|
||||
if (enc_len > IPSEC_MAX_KEY_SIZE) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"ipsec: cipher key too large (%zu > %d)",
|
||||
enc_len, IPSEC_MAX_KEY_SIZE);
|
||||
goto fail_delete;
|
||||
}
|
||||
|
||||
/* Key 0: auth key */
|
||||
cmd_keys.keys[0].key_bits = p.key_auth->sadb_key_bits;
|
||||
cmd_keys.keys[0].key_alg = p.sa->sadb_sa_auth;
|
||||
cmd_keys.keys[0].key_type = 1; /* auth */
|
||||
memcpy(cmd_keys.keys[0].key,
|
||||
(const uint8_t *)(p.key_auth + 1), auth_len);
|
||||
|
||||
/* Key 1: cipher key */
|
||||
cmd_keys.keys[1].key_bits = p.key_enc->sadb_key_bits;
|
||||
cmd_keys.keys[1].key_alg = p.sa->sadb_sa_encrypt;
|
||||
cmd_keys.keys[1].key_type = 0; /* cipher */
|
||||
memcpy(cmd_keys.keys[1].key,
|
||||
(const uint8_t *)(p.key_enc + 1), enc_len);
|
||||
} else {
|
||||
size_t enc_len;
|
||||
|
||||
/* GCM: 1 key (cipher only, includes 4-byte salt) */
|
||||
enc_len = (p.key_enc->sadb_key_bits + 7) / 8;
|
||||
if (enc_len > IPSEC_MAX_KEY_SIZE) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"ipsec: cipher key too large (%zu > %d)",
|
||||
enc_len, IPSEC_MAX_KEY_SIZE);
|
||||
goto fail_delete;
|
||||
}
|
||||
|
||||
cmd_keys.num_keys = 1;
|
||||
cmd_keys.keys[0].key_bits = p.key_enc->sadb_key_bits;
|
||||
cmd_keys.keys[0].key_alg = p.sa->sadb_sa_encrypt;
|
||||
cmd_keys.keys[0].key_type = 0; /* cipher */
|
||||
memcpy(cmd_keys.keys[0].key,
|
||||
(const uint8_t *)(p.key_enc + 1), enc_len);
|
||||
}
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPSEC_SA_SET_KEYS,
|
||||
sizeof(cmd_keys), (unsigned short *)&cmd_keys);
|
||||
if (rc != 0) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"ipsec: SA_SET_KEYS failed: %d (sagd=0x%04x)",
|
||||
rc, sagd);
|
||||
goto fail_delete;
|
||||
}
|
||||
|
||||
/*
|
||||
* FCI 3: SA_SET_TUNNEL (if tunnel mode)
|
||||
*/
|
||||
if (is_tunnel) {
|
||||
memset(&cmd_tunnel, 0, sizeof(cmd_tunnel));
|
||||
cmd_tunnel.sagd = sagd;
|
||||
cmd_tunnel.proto_family =
|
||||
(af == AF_INET) ? PROTO_FAMILY_IPV4 : PROTO_FAMILY_IPV6;
|
||||
|
||||
if (af == AF_INET) {
|
||||
const struct sockaddr_in *sin_src =
|
||||
(const struct sockaddr_in *)sa_src;
|
||||
const struct sockaddr_in *sin_dst =
|
||||
(const struct sockaddr_in *)sa_dst;
|
||||
|
||||
cmd_tunnel.h.ipv4h.Version_IHL = 0x45;
|
||||
cmd_tunnel.h.ipv4h.TTL = 64;
|
||||
cmd_tunnel.h.ipv4h.Protocol = 50; /* ESP */
|
||||
memcpy(&cmd_tunnel.h.ipv4h.SourceAddress,
|
||||
&sin_src->sin_addr, 4);
|
||||
memcpy(&cmd_tunnel.h.ipv4h.DestinationAddress,
|
||||
&sin_dst->sin_addr, 4);
|
||||
} else {
|
||||
const struct sockaddr_in6 *sin6_src =
|
||||
(const struct sockaddr_in6 *)sa_src;
|
||||
const struct sockaddr_in6 *sin6_dst =
|
||||
(const struct sockaddr_in6 *)sa_dst;
|
||||
|
||||
cmd_tunnel.h.ipv6h.Version_TC_FLHi = htons(0x6000);
|
||||
cmd_tunnel.h.ipv6h.NextHeader = 50; /* ESP */
|
||||
cmd_tunnel.h.ipv6h.HopLimit = 64;
|
||||
memcpy(cmd_tunnel.h.ipv6h.SourceAddress,
|
||||
&sin6_src->sin6_addr, 16);
|
||||
memcpy(cmd_tunnel.h.ipv6h.DestinationAddress,
|
||||
&sin6_dst->sin6_addr, 16);
|
||||
}
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPSEC_SA_SET_TUNNEL,
|
||||
sizeof(cmd_tunnel), (unsigned short *)&cmd_tunnel);
|
||||
if (rc != 0) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"ipsec: SA_SET_TUNNEL failed: %d (sagd=0x%04x)",
|
||||
rc, sagd);
|
||||
goto fail_delete;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* FCI 4: SA_SET_STATE -> VALID
|
||||
*/
|
||||
memset(&cmd_state, 0, sizeof(cmd_state));
|
||||
cmd_state.sagd = sagd;
|
||||
cmd_state.state = SA_STATE_VALID;
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPSEC_SA_SET_STATE,
|
||||
sizeof(cmd_state), (unsigned short *)&cmd_state);
|
||||
if (rc != 0) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"ipsec: SA_SET_STATE failed: %d (sagd=0x%04x)",
|
||||
rc, sagd);
|
||||
goto fail_delete;
|
||||
}
|
||||
|
||||
/* Add to local SA table */
|
||||
sa = malloc(sizeof(*sa));
|
||||
if (sa == NULL) {
|
||||
cmm_print(CMM_LOG_ERR, "ipsec: malloc failed");
|
||||
goto fail_delete;
|
||||
}
|
||||
memset(sa, 0, sizeof(*sa));
|
||||
sa->sagd = sagd;
|
||||
sa->spi = spi;
|
||||
sa->af = af;
|
||||
memcpy(sa->dst_addr, dst_addr, sizeof(sa->dst_addr));
|
||||
sa->offloaded = 1;
|
||||
|
||||
{
|
||||
uint32_t bucket = sa_hash_fn(spi, dst_addr, af);
|
||||
list_add(&sa_hash[bucket], &sa->list);
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"ipsec: SA offloaded SPI=0x%x sagd=0x%04x",
|
||||
ntohl(spi), sagd);
|
||||
explicit_bzero(&cmd_keys, sizeof(cmd_keys));
|
||||
return;
|
||||
|
||||
fail_delete:
|
||||
/* Roll back: delete the SA we partially created */
|
||||
{
|
||||
CommandIPSecDeleteSA cmd_del;
|
||||
memset(&cmd_del, 0, sizeof(cmd_del));
|
||||
cmd_del.sagd = sagd;
|
||||
fci_write(g->fci_handle, FPP_CMD_IPSEC_SA_DELETE,
|
||||
sizeof(cmd_del), (unsigned short *)&cmd_del);
|
||||
}
|
||||
explicit_bzero(&cmd_keys, sizeof(cmd_keys));
|
||||
}
|
||||
|
||||
static void
|
||||
handle_sadb_delete(struct cmm_global *g, const struct sadb_msg *msg)
|
||||
{
|
||||
struct pfkey_parsed p;
|
||||
const struct sockaddr *sa_dst;
|
||||
struct cmm_sa_entry *sa;
|
||||
uint8_t dst_addr[16];
|
||||
sa_family_t af;
|
||||
uint32_t spi;
|
||||
int rc;
|
||||
|
||||
pfkey_parse_extensions(msg, &p);
|
||||
|
||||
if (p.sa == NULL || p.addr_dst == NULL) {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"ipsec: SADB_DELETE missing SA or DST");
|
||||
return;
|
||||
}
|
||||
|
||||
sa_dst = pfkey_addr_sa(p.addr_dst);
|
||||
af = sa_dst->sa_family;
|
||||
spi = p.sa->sadb_sa_spi;
|
||||
|
||||
memset(dst_addr, 0, sizeof(dst_addr));
|
||||
pfkey_copy_addr(sa_dst, dst_addr);
|
||||
|
||||
sa = sa_find(spi, af, dst_addr);
|
||||
if (sa == NULL) {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"ipsec: SADB_DELETE SPI=0x%x not tracked",
|
||||
ntohl(spi));
|
||||
return;
|
||||
}
|
||||
|
||||
if (sa->offloaded) {
|
||||
CommandIPSecDeleteSA cmd_del;
|
||||
|
||||
memset(&cmd_del, 0, sizeof(cmd_del));
|
||||
cmd_del.sagd = sa->sagd;
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPSEC_SA_DELETE,
|
||||
sizeof(cmd_del), (unsigned short *)&cmd_del);
|
||||
if (rc != 0)
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"ipsec: SA_DELETE failed: %d (sagd=0x%04x)",
|
||||
rc, sa->sagd);
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"ipsec: SA removed SPI=0x%x sagd=0x%04x",
|
||||
ntohl(spi), sa->sagd);
|
||||
|
||||
list_del(&sa->list);
|
||||
free(sa);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_pfkey_open(void)
|
||||
{
|
||||
struct sadb_msg msg;
|
||||
int fd, flags;
|
||||
ssize_t n;
|
||||
|
||||
fd = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
|
||||
if (fd < 0) {
|
||||
cmm_print(CMM_LOG_ERR, "ipsec: PF_KEY socket: %s",
|
||||
strerror(errno));
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Set non-blocking */
|
||||
flags = fcntl(fd, F_GETFL, 0);
|
||||
if (flags >= 0)
|
||||
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||
|
||||
/* Register for ESP SA events */
|
||||
memset(&msg, 0, sizeof(msg));
|
||||
msg.sadb_msg_version = PF_KEY_V2;
|
||||
msg.sadb_msg_type = SADB_REGISTER;
|
||||
msg.sadb_msg_satype = SADB_SATYPE_ESP;
|
||||
msg.sadb_msg_len = PFKEY_UNIT64(sizeof(msg));
|
||||
msg.sadb_msg_pid = getpid();
|
||||
|
||||
if (write(fd, &msg, sizeof(msg)) < 0) {
|
||||
cmm_print(CMM_LOG_WARN, "ipsec: SADB_REGISTER: %s",
|
||||
strerror(errno));
|
||||
/* Non-fatal — we can still receive SA events */
|
||||
}
|
||||
|
||||
/* Drain the register reply */
|
||||
{
|
||||
uint8_t buf[4096];
|
||||
while ((n = read(fd, buf, sizeof(buf))) > 0)
|
||||
;
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_INFO, "ipsec: PF_KEY socket opened (fd=%d)", fd);
|
||||
return (fd);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_pfkey_dispatch(struct cmm_global *g)
|
||||
{
|
||||
uint8_t buf[4096];
|
||||
ssize_t n;
|
||||
const struct sadb_msg *msg;
|
||||
|
||||
while ((n = read(g->pfkey_fd, buf, sizeof(buf))) > 0) {
|
||||
if ((size_t)n < sizeof(struct sadb_msg))
|
||||
continue;
|
||||
|
||||
msg = (const struct sadb_msg *)buf;
|
||||
|
||||
/* Ignore our own messages */
|
||||
if (msg->sadb_msg_pid == (uint32_t)getpid())
|
||||
continue;
|
||||
|
||||
/* Only handle ESP */
|
||||
if (msg->sadb_msg_satype != SADB_SATYPE_ESP)
|
||||
continue;
|
||||
|
||||
/* Check message length consistency */
|
||||
if ((size_t)PFKEY_UNUNIT64(msg->sadb_msg_len) > (size_t)n)
|
||||
continue;
|
||||
|
||||
switch (msg->sadb_msg_type) {
|
||||
case SADB_ADD:
|
||||
case SADB_UPDATE:
|
||||
handle_sadb_add(g, msg);
|
||||
break;
|
||||
case SADB_DELETE:
|
||||
handle_sadb_delete(g, msg);
|
||||
break;
|
||||
case SADB_EXPIRE:
|
||||
/* Treat hard expire as delete */
|
||||
if (msg->sadb_msg_errno == 0)
|
||||
handle_sadb_delete(g, msg);
|
||||
break;
|
||||
default:
|
||||
cmm_print(CMM_LOG_TRACE,
|
||||
"ipsec: PF_KEY msg type=%d ignored",
|
||||
msg->sadb_msg_type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
explicit_bzero(buf, sizeof(buf));
|
||||
}
|
||||
|
||||
int
|
||||
cmm_ipsec_init(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < CMM_SA_HASH_SIZE; i++)
|
||||
list_head_init(&sa_hash[i]);
|
||||
|
||||
next_sagd = 1;
|
||||
|
||||
cmm_print(CMM_LOG_DEBUG, "ipsec: SA table initialized (%d buckets)",
|
||||
CMM_SA_HASH_SIZE);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_sa_flush_all(struct cmm_global *g)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < CMM_SA_HASH_SIZE; i++) {
|
||||
struct list_head *pos, *tmp, *head;
|
||||
|
||||
head = &sa_hash[i];
|
||||
pos = list_first(head);
|
||||
while (pos != head) {
|
||||
struct cmm_sa_entry *sa;
|
||||
|
||||
tmp = list_next(pos);
|
||||
sa = container_of(pos, struct cmm_sa_entry, list);
|
||||
|
||||
if (sa->offloaded && g->fci_handle != NULL) {
|
||||
CommandIPSecDeleteSA cmd_del;
|
||||
|
||||
memset(&cmd_del, 0, sizeof(cmd_del));
|
||||
cmd_del.sagd = sa->sagd;
|
||||
fci_write(g->fci_handle,
|
||||
FPP_CMD_IPSEC_SA_DELETE,
|
||||
sizeof(cmd_del),
|
||||
(unsigned short *)&cmd_del);
|
||||
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"ipsec: flushed sagd=0x%04x",
|
||||
sa->sagd);
|
||||
}
|
||||
list_del(&sa->list);
|
||||
free(sa);
|
||||
pos = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cmm_ipsec_fini(struct cmm_global *g __unused)
|
||||
{
|
||||
|
||||
cmm_print(CMM_LOG_DEBUG, "ipsec: subsystem finalized");
|
||||
}
|
||||
|
|
@ -1,156 +0,0 @@
|
|||
/*
|
||||
* cmm_ipsec.h -- IPsec SA management via PF_KEY
|
||||
*
|
||||
* Monitors PF_KEY socket for SA events (add/delete/expire),
|
||||
* translates them into FCI commands for CDX hardware offload.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* Copyright (C) 2007 Mindspeed Technologies, Inc.
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017, 2021 NXP
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#ifndef CMM_IPSEC_H
|
||||
#define CMM_IPSEC_H
|
||||
|
||||
#include "cmm.h"
|
||||
#include <net/pfkeyv2.h>
|
||||
|
||||
/* SA state constants (must match CDX control_ipsec.c) */
|
||||
#define SA_STATE_INIT 0x1
|
||||
#define SA_STATE_VALID 0x2
|
||||
#define SA_STATE_DEAD 0x3
|
||||
|
||||
/* SA flags */
|
||||
#define NLKEY_SAFLAGS_ESN 0x1
|
||||
#define NLKEY_SAFLAGS_INBOUND 0x2
|
||||
|
||||
/* Protocol family (must match CDX fpp.h / keytrack.h) */
|
||||
#define PROTO_FAMILY_IPV4 2
|
||||
#define PROTO_FAMILY_IPV6 10
|
||||
|
||||
/* Limits */
|
||||
#define IPSEC_MAX_KEY_SIZE 64 /* must match CDX control_ipsec.h (512/8) */
|
||||
#define IPSEC_MAX_NUM_KEYS 2
|
||||
|
||||
/* SA hash table */
|
||||
#define CMM_SA_HASH_BITS 5
|
||||
#define CMM_SA_HASH_SIZE (1 << CMM_SA_HASH_BITS)
|
||||
|
||||
/*
|
||||
* FCI command payload structures.
|
||||
* Binary layout must match CDX control_ipsec.c expectations exactly.
|
||||
* Copied from ASK/cmm-17.03.1/src/module_ipsec.h.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
unsigned char Version_IHL;
|
||||
unsigned char TypeOfService;
|
||||
unsigned short TotalLength;
|
||||
unsigned short Identification;
|
||||
unsigned short Flags_FragmentOffset;
|
||||
unsigned char TTL;
|
||||
unsigned char Protocol;
|
||||
unsigned short HeaderChksum;
|
||||
unsigned int SourceAddress;
|
||||
unsigned int DestinationAddress;
|
||||
} ipv4_hdr_t;
|
||||
|
||||
typedef struct {
|
||||
unsigned short Version_TC_FLHi;
|
||||
unsigned short FlowLabelLo;
|
||||
unsigned short TotalLength;
|
||||
unsigned char NextHeader;
|
||||
unsigned char HopLimit;
|
||||
unsigned int SourceAddress[4];
|
||||
unsigned int DestinationAddress[4];
|
||||
} ipv6_hdr_t;
|
||||
|
||||
typedef struct {
|
||||
unsigned int spi;
|
||||
unsigned char sa_type;
|
||||
unsigned char proto_family;
|
||||
unsigned char replay_window;
|
||||
unsigned char flags;
|
||||
unsigned int dst_ip[4];
|
||||
unsigned int src_ip[4];
|
||||
unsigned short mtu;
|
||||
unsigned short dev_mtu;
|
||||
} IPSec_said;
|
||||
|
||||
typedef struct {
|
||||
unsigned short key_bits;
|
||||
unsigned char key_alg;
|
||||
unsigned char key_type;
|
||||
unsigned char key[IPSEC_MAX_KEY_SIZE];
|
||||
} IPSec_key_desc;
|
||||
|
||||
typedef struct {
|
||||
unsigned short sagd;
|
||||
unsigned short rsvd;
|
||||
IPSec_said said;
|
||||
} CommandIPSecCreateSA;
|
||||
|
||||
typedef struct {
|
||||
unsigned short sagd;
|
||||
unsigned short rsvd;
|
||||
} CommandIPSecDeleteSA;
|
||||
|
||||
typedef struct {
|
||||
unsigned short sagd;
|
||||
unsigned short rsvd;
|
||||
unsigned short num_keys;
|
||||
unsigned short rsvd2;
|
||||
IPSec_key_desc keys[IPSEC_MAX_NUM_KEYS];
|
||||
} CommandIPSecSetKey;
|
||||
|
||||
typedef struct {
|
||||
unsigned short sagd;
|
||||
unsigned char rsvd;
|
||||
unsigned char proto_family;
|
||||
union {
|
||||
ipv4_hdr_t ipv4h;
|
||||
ipv6_hdr_t ipv6h;
|
||||
} h;
|
||||
} CommandIPSecSetTunnel;
|
||||
|
||||
typedef struct {
|
||||
unsigned short sagd;
|
||||
unsigned short parent_sa_sagd;
|
||||
unsigned short state;
|
||||
unsigned short rsvd2;
|
||||
} CommandIPSecSetState;
|
||||
|
||||
/* Local SA entry */
|
||||
struct cmm_sa_entry {
|
||||
struct list_head list;
|
||||
uint16_t sagd;
|
||||
uint32_t spi; /* network byte order */
|
||||
sa_family_t af;
|
||||
uint8_t dst_addr[16]; /* IPv4 in first 4 bytes */
|
||||
int offloaded;
|
||||
};
|
||||
|
||||
/* PF_KEY parsed extensions */
|
||||
struct pfkey_parsed {
|
||||
const struct sadb_sa *sa;
|
||||
const struct sadb_address *addr_src;
|
||||
const struct sadb_address *addr_dst;
|
||||
const struct sadb_key *key_auth;
|
||||
const struct sadb_key *key_enc;
|
||||
const struct sadb_x_sa2 *sa2;
|
||||
};
|
||||
|
||||
/* Subsystem init/fini */
|
||||
int cmm_ipsec_init(void);
|
||||
void cmm_ipsec_fini(struct cmm_global *g);
|
||||
|
||||
/* PF_KEY socket */
|
||||
int cmm_pfkey_open(void);
|
||||
void cmm_pfkey_dispatch(struct cmm_global *g);
|
||||
|
||||
/* Flush all offloaded SAs */
|
||||
void cmm_sa_flush_all(struct cmm_global *g);
|
||||
|
||||
#endif /* CMM_IPSEC_H */
|
||||
1279
fastpath/vendor/cdx/control_ipsec.c
vendored
1279
fastpath/vendor/cdx/control_ipsec.c
vendored
File diff suppressed because it is too large
Load diff
458
fastpath/vendor/cdx/query_ipsec.c
vendored
458
fastpath/vendor/cdx/query_ipsec.c
vendored
|
|
@ -1,458 +0,0 @@
|
|||
/*
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017,2021 NXP
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
* The GPL-2.0+ license for this file can be found in the COPYING.GPL file
|
||||
* included with this distribution or at http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef DPA_IPSEC_OFFLOAD
|
||||
//#include "module_ipsec.h"
|
||||
//#include "module_tunnel.h"
|
||||
#include "portdefs.h"
|
||||
#include "cdx.h"
|
||||
#include "cdx_common.h"
|
||||
#include "fe.h"
|
||||
#include "control_ipv4.h"
|
||||
#include "control_stat.h"
|
||||
#include "control_ipv6.h"
|
||||
#include "control_ipsec.h"
|
||||
#include "cdx_dpa_ipsec.h"
|
||||
|
||||
/* This function returns total SA entries configured with a given handle */
|
||||
static int IPsec_Get_Hash_SAEntries(int sa_handle_index)
|
||||
{
|
||||
|
||||
int tot_sa_entries = 0;
|
||||
PSAEntry pSAEntry;
|
||||
struct slist_entry *entry;
|
||||
|
||||
slist_for_each(pSAEntry, entry, &sa_cache_by_h[sa_handle_index], list_h)
|
||||
{
|
||||
tot_sa_entries++;
|
||||
}
|
||||
|
||||
|
||||
return tot_sa_entries;
|
||||
|
||||
}
|
||||
|
||||
static int M_ipsec_sa_get_auth_key(PSAEntry pSAEntry, U8* auth_alg)
|
||||
{
|
||||
uint8_t auth_algo;
|
||||
|
||||
switch (pSAEntry->pSec_sa_context->auth_data.auth_type) {
|
||||
case OP_PCL_IPSEC_HMAC_MD5_96 :
|
||||
auth_algo = SADB_AALG_MD5HMAC;
|
||||
break;
|
||||
case OP_PCL_IPSEC_HMAC_SHA1_96 :
|
||||
auth_algo = SADB_AALG_SHA1HMAC;
|
||||
break;
|
||||
case OP_PCL_IPSEC_HMAC_SHA2_256_128 :
|
||||
auth_algo = SADB_X_AALG_SHA2_256HMAC;
|
||||
break;
|
||||
case OP_PCL_IPSEC_HMAC_SHA2_384_192 :
|
||||
auth_algo = SADB_X_AALG_SHA2_384HMAC;
|
||||
break;
|
||||
case OP_PCL_IPSEC_HMAC_SHA2_512_256 :
|
||||
auth_algo = SADB_X_AALG_SHA2_512HMAC;
|
||||
break;
|
||||
case OP_PCL_IPSEC_AES_XCBC_MAC_96 :
|
||||
auth_algo = SADB_X_AALG_AES_XCBC_MAC;
|
||||
break;
|
||||
case OP_PCL_IPSEC_HMAC_NULL :
|
||||
auth_algo = SADB_X_AALG_NULL;
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
*auth_alg = auth_algo;
|
||||
return NO_ERR;
|
||||
}
|
||||
|
||||
static int M_ipsec_sa_get_cipher_key(PSAEntry pSAEntry, U8* cipher_alg)
|
||||
{
|
||||
uint8_t cipher_algo;
|
||||
|
||||
switch (pSAEntry->pSec_sa_context->cipher_data.cipher_type) {
|
||||
case OP_PCL_IPSEC_AES_CTR:
|
||||
cipher_algo = SADB_X_EALG_AESCTR;
|
||||
break;
|
||||
case OP_PCL_IPSEC_AES_CBC:
|
||||
cipher_algo = SADB_X_EALG_AESCBC;
|
||||
break;
|
||||
case OP_PCL_IPSEC_AES_CCM8:
|
||||
cipher_algo = SADB_X_EALG_AES_CCM_ICV8;
|
||||
break;
|
||||
case OP_PCL_IPSEC_AES_CCM12:
|
||||
cipher_algo = SADB_X_EALG_AES_CCM_ICV12;
|
||||
break;
|
||||
case OP_PCL_IPSEC_AES_CCM16:
|
||||
cipher_algo = SADB_X_EALG_AES_CCM_ICV16;
|
||||
break;
|
||||
case OP_PCL_IPSEC_AES_GCM8:
|
||||
cipher_algo = SADB_X_EALG_AES_GCM_ICV8;
|
||||
break;
|
||||
case OP_PCL_IPSEC_AES_GCM12:
|
||||
cipher_algo = SADB_X_EALG_AES_GCM_ICV12;
|
||||
break;
|
||||
case OP_PCL_IPSEC_AES_GCM16:
|
||||
cipher_algo = SADB_X_EALG_AES_GCM_ICV16;
|
||||
break;
|
||||
case OP_PCL_IPSEC_AES_GMAC:
|
||||
cipher_algo = SADB_X_EALG_NULL_AES_GMAC;
|
||||
break;
|
||||
case OP_PCL_IPSEC_3DES:
|
||||
cipher_algo = SADB_EALG_3DESCBC;
|
||||
break;
|
||||
case OP_PCL_IPSEC_DES:
|
||||
cipher_algo = SADB_EALG_DESCBC;
|
||||
break;
|
||||
case OP_PCL_IPSEC_NULL_ENC:
|
||||
cipher_algo = SADB_EALG_NULL;
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
*cipher_alg = cipher_algo;
|
||||
return NO_ERR;
|
||||
}
|
||||
|
||||
|
||||
/* This function fillgo;
|
||||
ls the snapshot of SA entries in a given handle hash index */
|
||||
static int IPsec_SA_Get_Handle_Snapshot(int sa_handle_index, int sa_entries, PSAQueryCommand pSAHandleSnapshot)
|
||||
{
|
||||
|
||||
int tot_sa_entries=0, rc;
|
||||
PSAEntry pSAEntry;
|
||||
struct slist_entry *entry;
|
||||
|
||||
slist_for_each(pSAEntry, entry, &sa_cache_by_h[sa_handle_index], list_h)
|
||||
{
|
||||
|
||||
pSAHandleSnapshot->src_ip[0] = pSAEntry->id.saddr[0];
|
||||
pSAHandleSnapshot->src_ip[1] = pSAEntry->id.saddr[1];
|
||||
pSAHandleSnapshot->src_ip[2] = pSAEntry->id.saddr[2];
|
||||
pSAHandleSnapshot->src_ip[3] = pSAEntry->id.saddr[3];
|
||||
pSAHandleSnapshot->dst_ip[0] = pSAEntry->id.daddr.a6[0];
|
||||
pSAHandleSnapshot->dst_ip[1] = pSAEntry->id.daddr.a6[1];
|
||||
pSAHandleSnapshot->dst_ip[2] = pSAEntry->id.daddr.a6[2];
|
||||
pSAHandleSnapshot->dst_ip[3] = pSAEntry->id.daddr.a6[3];
|
||||
pSAHandleSnapshot->spi = pSAEntry->id.spi;
|
||||
pSAHandleSnapshot->sa_type = pSAEntry->id.proto;
|
||||
pSAHandleSnapshot->family = pSAEntry->family;
|
||||
pSAHandleSnapshot->handle = pSAEntry->handle;
|
||||
pSAHandleSnapshot->mtu = pSAEntry->mtu;
|
||||
pSAHandleSnapshot->state = pSAEntry->state;
|
||||
pSAHandleSnapshot->flags = pSAEntry->flags;
|
||||
|
||||
if (pSAEntry->flags & SA_ALLOW_SEQ_ROLL)
|
||||
pSAHandleSnapshot->replay_window = 0x0;
|
||||
else
|
||||
pSAHandleSnapshot->replay_window = 0x1;
|
||||
if(pSAEntry->pSec_sa_context)
|
||||
{
|
||||
rc = M_ipsec_sa_get_cipher_key(pSAEntry, &pSAHandleSnapshot->cipher_algo);
|
||||
if (!rc)
|
||||
{
|
||||
memcpy(&pSAHandleSnapshot->cipher_key[0],pSAEntry->pSec_sa_context->cipher_data.cipher_key, sizeof(pSAHandleSnapshot->cipher_key));
|
||||
pSAHandleSnapshot->cipher_key_len = (unsigned char)pSAEntry->pSec_sa_context->cipher_data.cipher_key_len;
|
||||
}
|
||||
|
||||
rc = M_ipsec_sa_get_auth_key(pSAEntry, &pSAHandleSnapshot->auth_algo);
|
||||
if (!rc)
|
||||
{
|
||||
memcpy(&pSAHandleSnapshot->auth_key[0], pSAEntry->pSec_sa_context->auth_data.auth_key, sizeof(pSAHandleSnapshot->auth_key));
|
||||
pSAHandleSnapshot->auth_key_len = (unsigned char)pSAEntry->pSec_sa_context->auth_data.auth_key_len;
|
||||
}
|
||||
|
||||
}
|
||||
if (pSAEntry->mode == SA_MODE_TUNNEL)
|
||||
{
|
||||
pSAHandleSnapshot->mode = SA_MODE_TUNNEL;
|
||||
if (pSAEntry->header_len == IPV4_HDR_SIZE)
|
||||
{
|
||||
pSAHandleSnapshot->tunnel_proto_family = PROTO_FAMILY_IPV4;
|
||||
pSAHandleSnapshot->tnl.ipv4.daddr = pSAEntry->tunnel.ip4.DestinationAddress;
|
||||
pSAHandleSnapshot->tnl.ipv4.saddr = pSAEntry->tunnel.ip4.SourceAddress;
|
||||
pSAHandleSnapshot->tnl.ipv4.tos = pSAEntry->tunnel.ip4.TypeOfService;
|
||||
pSAHandleSnapshot->tnl.ipv4.protocol = pSAEntry->tunnel.ip4.Protocol;
|
||||
pSAHandleSnapshot->tnl.ipv4.total_length = pSAEntry->tunnel.ip4.TotalLength;
|
||||
/*printk(KERN_INFO "tunnel: %x:%x:%x:%x:%x\n", pSAEntry->tunnel.ip4.DestinationAddress, pSAEntry->tunnel.ip4.SourceAddress, pSAEntry->tunnel.ip4.TypeOfService, pSAEntry->tunnel.ip4.Protocol, pSAEntry->tunnel.ip4.TotalLength);*/
|
||||
}
|
||||
else
|
||||
{
|
||||
pSAHandleSnapshot->tunnel_proto_family = PROTO_FAMILY_IPV6;
|
||||
pSAHandleSnapshot->tnl.ipv6.daddr[0] = pSAEntry->tunnel.ip6.DestinationAddress[0];
|
||||
pSAHandleSnapshot->tnl.ipv6.daddr[1] = pSAEntry->tunnel.ip6.DestinationAddress[1];
|
||||
pSAHandleSnapshot->tnl.ipv6.daddr[2] = pSAEntry->tunnel.ip6.DestinationAddress[2];
|
||||
pSAHandleSnapshot->tnl.ipv6.daddr[3] = pSAEntry->tunnel.ip6.DestinationAddress[3];
|
||||
|
||||
pSAHandleSnapshot->tnl.ipv6.saddr[0] = pSAEntry->tunnel.ip6.SourceAddress[0];
|
||||
pSAHandleSnapshot->tnl.ipv6.saddr[1] = pSAEntry->tunnel.ip6.SourceAddress[1];
|
||||
pSAHandleSnapshot->tnl.ipv6.saddr[2] = pSAEntry->tunnel.ip6.SourceAddress[2];
|
||||
pSAHandleSnapshot->tnl.ipv6.saddr[3] = pSAEntry->tunnel.ip6.SourceAddress[3];
|
||||
//IPV6_SET_TRAFFIC_CLASS(&pSAHandleSnapshot->tnl.ipv6, IPV6_GET_TRAFFIC_CLASS(&pSAEntry->tunnel.ip6));
|
||||
//IPV6_SET_VERSION(&pSAHandleSnapshot->tnl.ipv6, IPV6_GET_VERSION(&pSAEntry->tunnel.ip6));
|
||||
//IPV6_COPY_FLOW_LABEL(&pSAHandleSnapshot->tnl.ipv6, &pSAEntry->tunnel.ip6);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pSAHandleSnapshot->soft_byte_limit = pSAEntry->lft_conf.soft_byte_limit;
|
||||
pSAHandleSnapshot->hard_byte_limit = pSAEntry->lft_conf.hard_byte_limit;
|
||||
pSAHandleSnapshot->soft_packet_limit = pSAEntry->lft_conf.soft_packet_limit;
|
||||
pSAHandleSnapshot->hard_packet_limit = pSAEntry->lft_conf.hard_packet_limit;
|
||||
|
||||
pSAHandleSnapshot++;
|
||||
tot_sa_entries++;
|
||||
if(--sa_entries <= 0)
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return tot_sa_entries;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void __reset_stats_of_sa(PSAEntry pEntry, U32 pkts_processed, U64 bytes_processed)
|
||||
{
|
||||
pEntry->stats.last_pkts_processed = pkts_processed;
|
||||
pEntry->stats.last_bytes_processed = bytes_processed;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void reset_stats_of_sa(PSAEntry pEntry)
|
||||
{
|
||||
u32 pkts_processed;
|
||||
u64 bytes_processed;
|
||||
get_stats_from_sa(pEntry, &pkts_processed, &bytes_processed, NULL);
|
||||
__reset_stats_of_sa(pEntry, pkts_processed, bytes_processed);
|
||||
}
|
||||
/* This function creates the snapshot memory for SAs and returns the
|
||||
next SA entry from the snapshot of the SA entries of a
|
||||
single handle to the caller */
|
||||
|
||||
int IPsec_Get_Next_SAEntry(PSAQueryCommand pSAQueryCmd, int reset_action)
|
||||
{
|
||||
int ipsec_sa_hash_entries;
|
||||
PSAQueryCommand pSACmd;
|
||||
static PSAQueryCommand pSASnapshot = NULL;
|
||||
static int sa_hash_index = 0, sa_snapshot_entries =0, sa_snapshot_index=0 , sa_snapshot_buf_entries = 0;
|
||||
|
||||
if(reset_action)
|
||||
{
|
||||
sa_hash_index = 0;
|
||||
sa_snapshot_entries =0;
|
||||
sa_snapshot_index=0;
|
||||
if(pSASnapshot)
|
||||
{
|
||||
Heap_Free(pSASnapshot);
|
||||
pSASnapshot = NULL;
|
||||
}
|
||||
sa_snapshot_buf_entries = 0;
|
||||
}
|
||||
|
||||
if (sa_snapshot_index == 0)
|
||||
{
|
||||
while( sa_hash_index < NUM_SA_ENTRIES)
|
||||
{
|
||||
|
||||
ipsec_sa_hash_entries = IPsec_Get_Hash_SAEntries(sa_hash_index);
|
||||
if(ipsec_sa_hash_entries == 0)
|
||||
{
|
||||
sa_hash_index++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(ipsec_sa_hash_entries > sa_snapshot_buf_entries)
|
||||
{
|
||||
if(pSASnapshot)
|
||||
Heap_Free(pSASnapshot);
|
||||
|
||||
pSASnapshot = Heap_Alloc(ipsec_sa_hash_entries * sizeof(SAQueryCommand));
|
||||
|
||||
if (!pSASnapshot)
|
||||
{
|
||||
sa_hash_index = 0;
|
||||
sa_snapshot_buf_entries = 0;
|
||||
return ERR_NOT_ENOUGH_MEMORY;
|
||||
}
|
||||
sa_snapshot_buf_entries = ipsec_sa_hash_entries;
|
||||
}
|
||||
|
||||
sa_snapshot_entries = IPsec_SA_Get_Handle_Snapshot(sa_hash_index , ipsec_sa_hash_entries,pSASnapshot);
|
||||
break;
|
||||
}
|
||||
|
||||
if (sa_hash_index >= NUM_SA_ENTRIES)
|
||||
{
|
||||
sa_hash_index = 0;
|
||||
if (pSASnapshot)
|
||||
{
|
||||
Heap_Free(pSASnapshot);
|
||||
pSASnapshot = NULL;
|
||||
}
|
||||
sa_snapshot_buf_entries = 0;
|
||||
return ERR_SA_ENTRY_NOT_FOUND;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
pSACmd = &pSASnapshot[sa_snapshot_index++];
|
||||
memcpy(pSAQueryCmd, pSACmd, sizeof(SAQueryCommand));
|
||||
if (sa_snapshot_index == sa_snapshot_entries)
|
||||
{
|
||||
sa_snapshot_index = 0;
|
||||
sa_hash_index ++;
|
||||
}
|
||||
|
||||
|
||||
return NO_ERR;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* This function fills the snapshot of SA entries
|
||||
in a given hash index, statReset = 1 resets the stats of SA. */
|
||||
|
||||
static int stat_Get_SA_Hash_Snapshot(int sa_hash_index, int sa_entries,
|
||||
PStatIpsecEntryResponse pStatSASnapshot)
|
||||
{
|
||||
|
||||
int tot_sa_entries=0;
|
||||
PSAEntry pSAEntry;
|
||||
struct slist_entry *entry;
|
||||
u32 pkts_processed;
|
||||
u64 bytes_processed, bytes_64;
|
||||
|
||||
|
||||
slist_for_each(pSAEntry, entry, &sa_cache_by_h[sa_hash_index], list_h)
|
||||
{
|
||||
pStatSASnapshot->spi = ntohl(pSAEntry->id.spi);
|
||||
pStatSASnapshot->proto = pSAEntry->id.proto;
|
||||
pStatSASnapshot->family = pSAEntry->family;
|
||||
pStatSASnapshot->dstIP[0] = pSAEntry->id.daddr.a6[0];
|
||||
pStatSASnapshot->dstIP[1] = pSAEntry->id.daddr.a6[1];
|
||||
pStatSASnapshot->dstIP[2] = pSAEntry->id.daddr.a6[2];
|
||||
pStatSASnapshot->dstIP[3] = pSAEntry->id.daddr.a6[3];
|
||||
|
||||
pStatSASnapshot->seqOverflow = 0;
|
||||
if(pSAEntry->ct){
|
||||
get_stats_from_sa(pSAEntry, &pkts_processed, &bytes_processed,
|
||||
&pStatSASnapshot->seqOverflow);
|
||||
|
||||
pStatSASnapshot->total_pkts_processed = pkts_processed - pSAEntry->stats.last_pkts_processed;
|
||||
bytes_64 = bytes_processed - pSAEntry->stats.last_bytes_processed;
|
||||
pStatSASnapshot->total_bytes_processed[0] = bytes_64 & 0xffffffff;
|
||||
pStatSASnapshot->total_bytes_processed[1] = (bytes_64 >> 32) & 0xffffffff;
|
||||
|
||||
if (gStatIpsecQueryStatus)
|
||||
__reset_stats_of_sa(pSAEntry, pkts_processed, bytes_processed);
|
||||
}
|
||||
pStatSASnapshot->eof = 0;
|
||||
pStatSASnapshot->sagd = pSAEntry->handle;
|
||||
pStatSASnapshot++;
|
||||
tot_sa_entries++;
|
||||
|
||||
if(--sa_entries <= 0)
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
return tot_sa_entries;
|
||||
|
||||
}
|
||||
|
||||
/* This function creates the snapshot memory and returns the
|
||||
next SA entry from the snapshop of the SA entries of a
|
||||
single hash to the caller (statReset = 1 resets the stats) */
|
||||
|
||||
int stat_Get_Next_SAEntry(PStatIpsecEntryResponse pSACmd, int reset_action)
|
||||
{
|
||||
int stat_sa_hash_entries;
|
||||
PStatIpsecEntryResponse pSA;
|
||||
static PStatIpsecEntryResponse pStatSASnapshot = NULL;
|
||||
static int stat_sa_hash_index = 0, stat_sa_snapshot_entries =0, stat_sa_snapshot_index=0, stat_sa_snapshot_buf_entries = 0;
|
||||
|
||||
if(reset_action)
|
||||
{
|
||||
stat_sa_hash_index = 0;
|
||||
stat_sa_snapshot_entries =0;
|
||||
stat_sa_snapshot_index=0;
|
||||
if(pStatSASnapshot)
|
||||
{
|
||||
Heap_Free(pStatSASnapshot);
|
||||
pStatSASnapshot = NULL;
|
||||
}
|
||||
stat_sa_snapshot_buf_entries = 0;
|
||||
return NO_ERR;
|
||||
}
|
||||
|
||||
if (stat_sa_snapshot_index == 0)
|
||||
{
|
||||
while( stat_sa_hash_index < NUM_SA_ENTRIES)
|
||||
{
|
||||
|
||||
stat_sa_hash_entries = IPsec_Get_Hash_SAEntries(stat_sa_hash_index);
|
||||
if(stat_sa_hash_entries == 0)
|
||||
{
|
||||
stat_sa_hash_index++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (stat_sa_hash_entries > stat_sa_snapshot_buf_entries)
|
||||
{
|
||||
if(pStatSASnapshot)
|
||||
Heap_Free(pStatSASnapshot);
|
||||
pStatSASnapshot = Heap_Alloc(stat_sa_hash_entries * sizeof(StatIpsecEntryResponse));
|
||||
|
||||
if (!pStatSASnapshot)
|
||||
{
|
||||
stat_sa_hash_index = 0;
|
||||
stat_sa_snapshot_buf_entries = 0;
|
||||
return ERR_NOT_ENOUGH_MEMORY;
|
||||
}
|
||||
stat_sa_snapshot_buf_entries = stat_sa_hash_entries;
|
||||
}
|
||||
stat_sa_snapshot_entries = stat_Get_SA_Hash_Snapshot(stat_sa_hash_index , stat_sa_hash_entries, pStatSASnapshot);
|
||||
break;
|
||||
}
|
||||
|
||||
if (stat_sa_hash_index >= NUM_SA_ENTRIES)
|
||||
{
|
||||
stat_sa_hash_index = 0;
|
||||
if(pStatSASnapshot)
|
||||
{
|
||||
Heap_Free(pStatSASnapshot);
|
||||
pStatSASnapshot = NULL;
|
||||
}
|
||||
stat_sa_snapshot_buf_entries = 0;
|
||||
return ERR_SA_ENTRY_NOT_FOUND;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pSA = &pStatSASnapshot[stat_sa_snapshot_index++];
|
||||
memcpy(pSACmd, pSA, sizeof(StatIpsecEntryResponse));
|
||||
if (stat_sa_snapshot_index == stat_sa_snapshot_entries)
|
||||
{
|
||||
stat_sa_snapshot_index = 0;
|
||||
stat_sa_hash_index ++;
|
||||
}
|
||||
|
||||
|
||||
return NO_ERR;
|
||||
}
|
||||
|
||||
#endif // DPA_IPSEC_OFFLOAD
|
||||
Loading…
Add table
Reference in a new issue