fib_rules.h 3.5 KB
Newer Older
1 2 3 4
#ifndef __NET_FIB_RULES_H
#define __NET_FIB_RULES_H

#include <linux/types.h>
5
#include <linux/slab.h>
6 7 8
#include <linux/netdevice.h>
#include <linux/fib_rules.h>
#include <net/flow.h>
9
#include <net/rtnetlink.h>
10

E
Eric Dumazet 已提交
11
struct fib_rule {
12
	struct list_head	list;
13
	int			iifindex;
14
	int			oifindex;
15 16
	u32			mark;
	u32			mark_mask;
17 18 19
	u32			flags;
	u32			table;
	u8			action;
D
David Ahern 已提交
20 21
	u8			l3mdev;
	/* 2 bytes hole, try to use */
T
Thomas Graf 已提交
22
	u32			target;
23
	__be64			tun_id;
E
Eric Dumazet 已提交
24
	struct fib_rule __rcu	*ctarget;
25 26 27 28 29 30
	struct net		*fr_net;

	atomic_t		refcnt;
	u32			pref;
	int			suppress_ifgroup;
	int			suppress_prefixlen;
31
	char			iifname[IFNAMSIZ];
32
	char			oifname[IFNAMSIZ];
33 34 35
	struct rcu_head		rcu;
};

E
Eric Dumazet 已提交
36
struct fib_lookup_arg {
37 38 39
	void			*lookup_ptr;
	void			*result;
	struct fib_rule		*rule;
D
David Ahern 已提交
40
	u32			table;
E
Eric Dumazet 已提交
41
	int			flags;
42 43
#define FIB_LOOKUP_NOREF		1
#define FIB_LOOKUP_IGNORE_LINKSTATE	2
44 45
};

E
Eric Dumazet 已提交
46
struct fib_rules_ops {
47 48 49
	int			family;
	struct list_head	list;
	int			rule_size;
50
	int			addr_size;
T
Thomas Graf 已提交
51 52
	int			unresolved_rules;
	int			nr_goto_rules;
53 54 55 56

	int			(*action)(struct fib_rule *,
					  struct flowi *, int,
					  struct fib_lookup_arg *);
57 58
	bool			(*suppress)(struct fib_rule *,
					    struct fib_lookup_arg *);
59 60 61 62 63 64
	int			(*match)(struct fib_rule *,
					 struct flowi *, int);
	int			(*configure)(struct fib_rule *,
					     struct sk_buff *,
					     struct fib_rule_hdr *,
					     struct nlattr **);
65
	int			(*delete)(struct fib_rule *);
66 67 68 69 70
	int			(*compare)(struct fib_rule *,
					   struct fib_rule_hdr *,
					   struct nlattr **);
	int			(*fill)(struct fib_rule *, struct sk_buff *,
					struct fib_rule_hdr *);
71
	size_t			(*nlmsg_payload)(struct fib_rule *);
72

73 74
	/* Called after modifications to the rules set, must flush
	 * the route cache if one exists. */
75
	void			(*flush_cache)(struct fib_rules_ops *ops);
76

77
	int			nlgroup;
78
	const struct nla_policy	*policy;
79
	struct list_head	rules_list;
80
	struct module		*owner;
81
	struct net		*fro_net;
82
	struct rcu_head		rcu;
83 84
};

85
#define FRA_GENERIC_POLICY \
86
	[FRA_IIFNAME]	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
87
	[FRA_OIFNAME]	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
88 89 90
	[FRA_PRIORITY]	= { .type = NLA_U32 }, \
	[FRA_FWMARK]	= { .type = NLA_U32 }, \
	[FRA_FWMASK]	= { .type = NLA_U32 }, \
T
Thomas Graf 已提交
91
	[FRA_TABLE]     = { .type = NLA_U32 }, \
92
	[FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \
93
	[FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \
D
David Ahern 已提交
94 95
	[FRA_GOTO]	= { .type = NLA_U32 }, \
	[FRA_L3MDEV]	= { .type = NLA_U8 }
96

97 98 99 100 101 102 103 104
static inline void fib_rule_get(struct fib_rule *rule)
{
	atomic_inc(&rule->refcnt);
}

static inline void fib_rule_put(struct fib_rule *rule)
{
	if (atomic_dec_and_test(&rule->refcnt))
105
		kfree_rcu(rule, rcu);
106 107
}

D
David Ahern 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121
#ifdef CONFIG_NET_L3_MASTER_DEV
static inline u32 fib_rule_get_table(struct fib_rule *rule,
				     struct fib_lookup_arg *arg)
{
	return rule->l3mdev ? arg->table : rule->table;
}
#else
static inline u32 fib_rule_get_table(struct fib_rule *rule,
				     struct fib_lookup_arg *arg)
{
	return rule->table;
}
#endif

122 123 124 125 126 127 128
static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
{
	if (nla[FRA_TABLE])
		return nla_get_u32(nla[FRA_TABLE]);
	return frh->table;
}

129 130 131
struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
					 struct net *);
void fib_rules_unregister(struct fib_rules_ops *);
132

133 134 135 136
int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
		     struct fib_lookup_arg *);
int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
			 u32 flags);
D
David Ahern 已提交
137 138 139

int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh);
int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh);
140
#endif