版权声明:如有需要,可供转载,但请注明出处:https://blog.youkuaiyun.com/City_of_skey/article/details/86562770
目录
1、简介
ipsec对数据加密是在内核IP层实现的,通过PF_INET类型套接字和应用层通信文件在/net/key目录下,安全联盟SA和安全策略SP通过xfrm数据库实现。ESP、AH协议下/net/ipv4目录下实现。加密算法、认证算法在crypto目录中实现。
以下是各文件实现的功能:
net/xfrm目录下的文件实现功能个:
xfrm_state.c:xfrm状态管理
xfrm_policy.c:xfrm策略管理
xfrm_algo.c:算法管理
xfrm_hash.c:哈希表管理
xfrm_input.c:安全路径处理,ipsec入包处理
xfrm_user.c:netlink接口的SA和SP处理
net/ipv4目录下个文件功能:
ah4.c:ah协议处理
esp4.c:esp协议处理
ipcomp.c:ip压缩协议处理
xfrm4_input.c:Ipv4层接受ipsec包处理
xfrm4_outp.c:接受ipv4的ipsec包处理
xfrm4_state.c:ipv4的SA处理
xfrm4_policy.c:ipv4的SP处理
xfrm4_tunnel.c:ipv4通道处理
xfrm4_mode_transport.c:传输模式处理
xfrm4_mode_tunnel.c:隧道模式处理
xfrm4_mode_beet.c:beet模式处理
2、xfrm_state
struct xfrm_state结构体管理SA状态,各元素定义如下
/* Full description of state of transformer. */
struct xfrm_state { /*SA状态结构体*/
#ifdef CONFIG_NET_NS
struct net *xs_net;
#endif
union {
struct hlist_node gclist;
struct hlist_node bydst; /*目的地址哈希表*/
};
struct hlist_node bysrc; /*源地址哈希表*/
struct hlist_node byspi; /*spi哈希表*/
atomic_t refcnt;
spinlock_t lock;
struct xfrm_id id; /*id标志,也就是协议、目的ip、spi三元组*/
struct xfrm_selector sel; /*状态选择子*/
struct xfrm_mark mark; /*状态的标准值*/
u32 genid;
/* Key manager bits */
struct xfrm_state_walk km;
/* Parameters of this state. */
struct {
u32 reqid; /*请求id*/
u8 mode; /*模式,传输模式、隧道模式*/
u8 replay_window; /*回放窗口*/
u8 aalgo, ealgo, calgo;/*认证、加密、压缩算法id*/
u8 flags; /*一些标志*/
u16 family; /*协议族*/
xfrm_address_t saddr; /*源地址*/
int header_len; /*添加的协议长度*/
int trailer_len;
} props; /*SA参数*/
struct xfrm_lifetime_cfg lft; /*生成时间配置*/
/* Data for transformer */
struct xfrm_algo_auth *aalg; /*哈希算法*/
struct xfrm_algo *ealg; /*加密算法*/
struct xfrm_algo *calg; /*压缩算法*/
struct xfrm_algo_aead *aead;
/* Data for encapsulator */
struct xfrm_encap_tmpl *encap; /*NAT封装信息*/
/* Data for care-of address */
xfrm_address_t *coaddr;
/* IPComp needs an IPIP tunnel for handling uncompressed packets */
struct xfrm_state *tunnel; /*通道*/
/* If a tunnel, number of users + 1 */
atomic_t tunnel_users; /*通道数量*/
/* State for replay detection */
struct xfrm_replay_state replay; /*回放检测结构*/
/* Replay detection state at the time we sent the last notification */
struct xfrm_replay_state preplay;/*上次的回放结构*/
/* internal flag that only holds state for delayed aevent at the
* moment
*/
u32 xflags; /*标志*/
/* Replay detection notification settings */
u32 replay_maxage; /*回放最大时间间隔*/
u32 replay_maxdiff; /*回放最大差值*/
/* Replay detection notification timer */
struct timer_list rtimer; /*回放检测定时器*/
/* Statistics */
struct xfrm_stats stats; /*统计值*/
struct xfrm_lifetime_cur curlft; /*当前定时器*/
struct tasklet_hrtimer mtimer;
/* Last used time */
unsigned long lastused;
/* Reference to data common to all the instances of this
* transformer. */
const struct xfrm_type *type; /*协议 ESP、AH、IPCOMP*/
struct xfrm_mode *inner_mode;/*模式,隧道、传输*/
struct xfrm_mode *inner_mode_iaf;
struct xfrm_mode *outer_mode;
/* Security context */
struct xfrm_sec_ctx *security; /*安全上下文*/
/* Private data of this transformer, format is opaque,
* interpreted by xfrm_type methods. */
void *data; /*内部数据*/
};
3、xfrm_policy
struct xfrm_policy结构体定义了SP,各元素定义如下:
struct xfrm_policy {
#ifdef CONFIG_NET_NS
struct net *xp_net;
#endif
struct hlist_node bydst;/*按目的地址链表*/
struct hlist_node byidx;/*按id号链表*/
/* This lock only affects elements except for entry. */
rwlock_t lock;
atomic_t refcnt;/*引用计数*/
struct timer_list timer;/*策略定时器*/
struct flow_cache_object flo;
atomic_t genid;
u32 priority;/*策略优先级*/
u32 index;/*策略索引号*/
struct xfrm_mark mark;
struct xfrm_selector selector;/*选择子*/
struct xfrm_lifetime_cfg lft; /*策略生命周期*/
struct xfrm_lifetime_cur curlft; /*当前生命期数据*/
struct xfrm_policy_walk_entry walk;
u8 type; /*类型*/
u8 action; /*策略动作,接受、加密、阻塞*/
u8 flags; /*标志*/
u8 xfrm_nr;
u16 family;/*协议*/
struct xfrm_sec_ctx *security;/*安全上下文*/
struct xfrm_tmpl xfrm_vec[XFRM_MAX_DEPTH];/*状态模板*/
};
4、xfrm_tmpl
struct xfrm_tmpl结构体是模板结构体,用于SA和SP的查询
struct xfrm_tmpl {
/* id in template is interpreted as:
* daddr - destination of tunnel, may be zero for transport mode.
* spi - zero to acquire spi. Not zero if spi is static, then
* daddr must be fixed too.
* proto - AH/ESP/IPCOMP
*/
struct xfrm_id id; /*SA三元组:目的ip、协议、spi*/
/* Source address of tunnel. Ignored, if it is not a tunnel. */
xfrm_address_t saddr; /*源地址*/
unsigned short encap_family;
u32 reqid; /*请求id*/
/* Mode: transport, tunnel etc. */
u8 mode;
/* Sharing mode: unique, this session only, this user only etc. */
u8 share;
/* May skip this transfomration if no SA is found */
u8 optional;
/* Skip aalgos/ealgos/calgos checks. */
u8 allalgs;
/* Bit mask of algos allowed for acquisition */
u32 aalgos;
u32 ealgos;
u32 calgos;
};