四、LINUX策略规则
4.1 基本结构体
4.1.1策略规则结构体fib_rule
4.1.2 策略操作函数fib_rules_ops
策略规则中协议相关的操作函数结构体fib_rules_ops
4.1.3 ipv4类型的策略规则
IPV4协议相关的fib4_rule结构,该结构包含了基础的fib_rule,增加源IP、目的IP、tos等相关IPV4成员
4.1 基本结构体
4.1.1策略规则结构体fib_rule
struct fib_rule
{
struct list_head list;//策 略 规 则 链 表
atomic_t refcnt;//引 用 计 数
int ifindex;//接 口 index
char ifname[IFNAMSIZ];//接 口 名 称
u32 mark;//mark 值
u32 mark_mask;//mark 掩 码 值
u32 pref;//优先级, 值越小优先级越大
u32 flags;
u32 table;//路由表id
u8 action;//规则
u32 target;
struct fib_rule * ctarget;
struct rcu_head rcu;
struct net * fr_net;
};
Action各宏含义
enum
{
FR_ACT_UNSPEC,
FR_ACT_TO_TBL, /* Pass to fixed table */
FR_ACT_GOTO, /* Jump to another rule */
FR_ACT_NOP, /* No operation */
FR_ACT_RES3,
FR_ACT_RES4,
FR_ACT_BLACKHOLE, /* Drop without notification */
FR_ACT_UNREACHABLE, /* Drop with ENETUNREACH */
FR_ACT_PROHIBIT, /* Drop with EACCES */
__FR_ACT_MAX,
};
4.1.2 策略操作函数fib_rules_ops
策略规则中协议相关的操作函数结构体fib_rules_ops
struct fib_rules_ops
{
int family;//对应协议簇
struct list_head list;//将注册到系统的fib_rules_ops链接到链表rules_ops
int rule_size;//一个策略规则所占用的内存大小
int addr_size;//协议相关的地址长度
int unresolved_rules;
int nr_goto_rules;
//action函数,策略规则匹配后,所调用的action函数,执行后续的操作。
//一般是获取到相应的路由表,查找符合要求的路由项
int (*action)(struct fib_rule *,
struct flowi *, int,
struct fib_lookup_arg *);
//规则匹配函数,对于策略规则的匹配,首先是通用匹配,待通用匹配完成后,
//则会调用该函数,进行协议相关参数(源、目的地址等)的匹配
int (*match)(struct fib_rule *,
struct flowi *, int);
//协议相关的配置参数
int (*configure)(struct fib_rule *,
struct sk_buff *,
struct fib_rule_hdr *,
struct nlattr **);
int (*compare)(struct fib_rule *,
struct fib_rule_hdr *,
struct nlattr **);
int (*fill)(struct fib_rule *, struct sk_buff *,
struct fib_rule_hdr *);
//获取默认优先级
u32 (*default_pref)(struct fib_rules_ops *ops);
size_t (*nlmsg_payload)(struct fib_rule *);
/* Called after modifications to the rules set, must flush
* the route cache if one exists. */
void (*flush_cache)(struct fib_rules_ops *ops);
//下面两个是netlink相关参数
int nlgroup;
const struct nla_policy *policy;
struct list_head rules_list;//将该协议簇已添加的所有fib_rule规则链接在一起
struct module *owner;
struct net *fro_net;
};
4.1.3 ipv4类型的策略规则
IPV4协议相关的fib4_rule结构,该结构包含了基础的fib_rule,增加源IP、目的IP、tos等相关IPV4成员
struct fib4_rule//ipv4相关的结构
{
struct fib_rule common;
u8 dst_len;
u8 src_len;
u8 tos;
__be32 src;//源ip
__be32 srcmask;
__be32 dst;//目的ip
__be32 dstmask;
#ifdef CONFIG_NET_CLS_ROUTE
u32 tclassid;
#endif
};