2023-11-30 02:02:09 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
* Copyright Red Hat
|
|
|
|
* Author: David Gibson <david@gibson.dropbear.id.au>
|
|
|
|
*
|
|
|
|
* Definitions for the global table of packet flows.
|
|
|
|
*/
|
|
|
|
#ifndef FLOW_TABLE_H
|
|
|
|
#define FLOW_TABLE_H
|
|
|
|
|
2024-01-16 00:50:31 +00:00
|
|
|
#include "tcp_conn.h"
|
2024-02-29 04:15:32 +00:00
|
|
|
#include "icmp_flow.h"
|
2024-07-18 05:26:46 +00:00
|
|
|
#include "udp_flow.h"
|
2024-01-16 00:50:31 +00:00
|
|
|
|
2024-01-16 00:50:43 +00:00
|
|
|
/**
|
|
|
|
* struct flow_free_cluster - Information about a cluster of free entries
|
|
|
|
* @f: Generic flow information
|
|
|
|
* @n: Number of entries in the free cluster (including this one)
|
|
|
|
* @next: Index of next free cluster
|
|
|
|
*/
|
|
|
|
struct flow_free_cluster {
|
|
|
|
/* Must be first element */
|
|
|
|
struct flow_common f;
|
|
|
|
unsigned n;
|
|
|
|
unsigned next;
|
|
|
|
};
|
|
|
|
|
2023-11-30 02:02:09 +00:00
|
|
|
/**
|
|
|
|
* union flow - Descriptor for a logical packet flow (e.g. connection)
|
|
|
|
* @f: Fields common between all variants
|
|
|
|
* @tcp: Fields for non-spliced TCP connections
|
|
|
|
* @tcp_splice: Fields for spliced TCP connections
|
|
|
|
*/
|
|
|
|
union flow {
|
|
|
|
struct flow_common f;
|
2024-01-16 00:50:43 +00:00
|
|
|
struct flow_free_cluster free;
|
2023-11-30 02:02:09 +00:00
|
|
|
struct tcp_tap_conn tcp;
|
|
|
|
struct tcp_splice_conn tcp_splice;
|
2024-02-29 04:15:32 +00:00
|
|
|
struct icmp_ping_flow ping;
|
2024-07-18 05:26:46 +00:00
|
|
|
struct udp_flow udp;
|
2023-11-30 02:02:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Global Flow Table */
|
2024-01-16 00:50:43 +00:00
|
|
|
extern unsigned flow_first_free;
|
2023-11-30 02:02:09 +00:00
|
|
|
extern union flow flowtab[];
|
|
|
|
|
2024-07-17 04:52:20 +00:00
|
|
|
/**
|
|
|
|
* flow_foreach_sidei() - 'for' type macro to step through each side of flow
|
|
|
|
* @sidei_: Takes value INISIDE, then TGTSIDE
|
|
|
|
*/
|
|
|
|
#define flow_foreach_sidei(sidei_) \
|
|
|
|
for ((sidei_) = INISIDE; (sidei_) < SIDES; (sidei_)++)
|
2023-11-30 02:02:10 +00:00
|
|
|
|
2024-07-17 04:52:18 +00:00
|
|
|
/** flow_idx() - Index of flow from common structure
|
2023-11-30 02:02:10 +00:00
|
|
|
* @f: Common flow fields pointer
|
|
|
|
*
|
|
|
|
* Return: index of @f in the flow table
|
|
|
|
*/
|
|
|
|
static inline unsigned flow_idx(const struct flow_common *f)
|
|
|
|
{
|
|
|
|
return (union flow *)f - flowtab;
|
|
|
|
}
|
|
|
|
|
2024-07-17 04:52:18 +00:00
|
|
|
/** FLOW_IDX() - Find the index of a flow
|
2023-11-30 02:02:10 +00:00
|
|
|
* @f_: Flow pointer, either union flow * or protocol specific
|
|
|
|
*
|
|
|
|
* Return: index of @f in the flow table
|
|
|
|
*/
|
|
|
|
#define FLOW_IDX(f_) (flow_idx(&(f_)->f))
|
|
|
|
|
2024-07-17 04:52:18 +00:00
|
|
|
/** FLOW() - Flow entry at a given index
|
2023-11-30 02:02:10 +00:00
|
|
|
* @idx: Flow index
|
|
|
|
*
|
|
|
|
* Return: pointer to entry @idx in the flow table
|
|
|
|
*/
|
|
|
|
#define FLOW(idx) (&flowtab[(idx)])
|
|
|
|
|
2024-07-17 04:52:18 +00:00
|
|
|
/** flow_at_sidx() - Flow entry for a given sidx
|
2023-11-30 02:02:14 +00:00
|
|
|
* @sidx: Flow & side index
|
|
|
|
*
|
|
|
|
* Return: pointer to the corresponding flow entry, or NULL
|
|
|
|
*/
|
|
|
|
static inline union flow *flow_at_sidx(flow_sidx_t sidx)
|
|
|
|
{
|
2024-07-05 10:44:00 +00:00
|
|
|
if (!flow_sidx_valid(sidx))
|
2023-11-30 02:02:14 +00:00
|
|
|
return NULL;
|
2024-07-17 04:52:19 +00:00
|
|
|
return FLOW(sidx.flowi);
|
2023-11-30 02:02:14 +00:00
|
|
|
}
|
|
|
|
|
2024-07-17 04:52:18 +00:00
|
|
|
/** pif_at_sidx() - Interface for a given flow and side
|
|
|
|
* @sidx: Flow & side index
|
|
|
|
*
|
|
|
|
* Return: pif for the flow & side given by @sidx
|
|
|
|
*/
|
|
|
|
static inline uint8_t pif_at_sidx(flow_sidx_t sidx)
|
|
|
|
{
|
|
|
|
const union flow *flow = flow_at_sidx(sidx);
|
|
|
|
|
|
|
|
if (!flow)
|
|
|
|
return PIF_NONE;
|
2024-07-17 04:52:19 +00:00
|
|
|
return flow->f.pif[sidx.sidei];
|
2024-07-18 05:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** flowside_at_sidx() - Retrieve a specific flowside
|
|
|
|
* @sidx: Flow & side index
|
|
|
|
*
|
|
|
|
* Return: Flowside for the flow & side given by @sidx
|
|
|
|
*/
|
|
|
|
static inline const struct flowside *flowside_at_sidx(flow_sidx_t sidx)
|
|
|
|
{
|
|
|
|
const union flow *flow = flow_at_sidx(sidx);
|
|
|
|
|
|
|
|
if (!flow)
|
|
|
|
return PIF_NONE;
|
|
|
|
|
|
|
|
return &flow->f.side[sidx.sidei];
|
2024-07-17 04:52:18 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 05:26:46 +00:00
|
|
|
/** flow_sidx_opposite() - Get the other side of the same flow
|
|
|
|
* @sidx: Flow & side index
|
|
|
|
*
|
|
|
|
* Return: sidx for the other side of the same flow as @sidx
|
|
|
|
*/
|
|
|
|
static inline flow_sidx_t flow_sidx_opposite(flow_sidx_t sidx)
|
|
|
|
{
|
|
|
|
if (!flow_sidx_valid(sidx))
|
|
|
|
return FLOW_SIDX_NONE;
|
|
|
|
|
|
|
|
return (flow_sidx_t){.flowi = sidx.flowi, .sidei = !sidx.sidei};
|
|
|
|
}
|
|
|
|
|
2024-07-17 04:52:18 +00:00
|
|
|
/** flow_sidx() - Index of one side of a flow from common structure
|
2023-11-30 02:02:14 +00:00
|
|
|
* @f: Common flow fields pointer
|
2024-07-17 04:52:19 +00:00
|
|
|
* @sidei: Which side to refer to (0 or 1)
|
2023-11-30 02:02:14 +00:00
|
|
|
*
|
|
|
|
* Return: index of @f and @side in the flow table
|
|
|
|
*/
|
|
|
|
static inline flow_sidx_t flow_sidx(const struct flow_common *f,
|
2024-07-17 04:52:19 +00:00
|
|
|
unsigned sidei)
|
2023-11-30 02:02:14 +00:00
|
|
|
{
|
|
|
|
/* cppcheck-suppress [knownConditionTrueFalse, unmatchedSuppression] */
|
2024-07-17 04:52:19 +00:00
|
|
|
ASSERT(sidei == !!sidei);
|
2023-11-30 02:02:14 +00:00
|
|
|
|
|
|
|
return (flow_sidx_t){
|
2024-07-17 04:52:19 +00:00
|
|
|
.sidei = sidei,
|
|
|
|
.flowi = flow_idx(f),
|
2023-11-30 02:02:14 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-07-17 04:52:18 +00:00
|
|
|
/** FLOW_SIDX() - Find the index of one side of a flow
|
2023-11-30 02:02:14 +00:00
|
|
|
* @f_: Flow pointer, either union flow * or protocol specific
|
2024-07-17 04:52:19 +00:00
|
|
|
* @sidei: Which side to index (0 or 1)
|
2023-11-30 02:02:14 +00:00
|
|
|
*
|
|
|
|
* Return: index of @f and @side in the flow table
|
|
|
|
*/
|
2024-07-17 04:52:19 +00:00
|
|
|
#define FLOW_SIDX(f_, sidei) (flow_sidx(&(f_)->f, (sidei)))
|
2023-11-30 02:02:14 +00:00
|
|
|
|
2024-01-16 00:50:41 +00:00
|
|
|
union flow *flow_alloc(void);
|
|
|
|
void flow_alloc_cancel(union flow *flow);
|
|
|
|
|
2024-07-18 05:26:27 +00:00
|
|
|
const struct flowside *flow_initiate_af(union flow *flow, uint8_t pif,
|
|
|
|
sa_family_t af,
|
|
|
|
const void *saddr, in_port_t sport,
|
|
|
|
const void *daddr, in_port_t dport);
|
|
|
|
const struct flowside *flow_initiate_sa(union flow *flow, uint8_t pif,
|
|
|
|
const union sockaddr_inany *ssa,
|
|
|
|
in_port_t dport);
|
2024-07-18 05:26:28 +00:00
|
|
|
const struct flowside *flow_target_af(union flow *flow, uint8_t pif,
|
|
|
|
sa_family_t af,
|
|
|
|
const void *saddr, in_port_t sport,
|
|
|
|
const void *daddr, in_port_t dport);
|
2024-07-18 05:26:43 +00:00
|
|
|
const struct flowside *flow_target(const struct ctx *c, union flow *flow,
|
|
|
|
uint8_t proto);
|
2024-05-21 05:57:07 +00:00
|
|
|
|
2024-05-21 05:57:06 +00:00
|
|
|
union flow *flow_set_type(union flow *flow, enum flow_type type);
|
|
|
|
#define FLOW_SET_TYPE(flow_, t_, var_) (&flow_set_type((flow_), (t_))->var_)
|
2024-05-21 05:57:05 +00:00
|
|
|
|
|
|
|
void flow_activate(struct flow_common *f);
|
|
|
|
#define FLOW_ACTIVATE(flow_) \
|
|
|
|
(flow_activate(&(flow_)->f))
|
|
|
|
|
2023-11-30 02:02:09 +00:00
|
|
|
#endif /* FLOW_TABLE_H */
|