fib_rules.h 4.1 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
#include <linux/netdevice.h>
#include <linux/fib_rules.h>
8
#include <linux/refcount.h>
9
#include <net/flow.h>
10
#include <net/rtnetlink.h>
11
#include <net/fib_notifier.h>
12

13 14 15 16 17
struct fib_kuid_range {
	kuid_t start;
	kuid_t end;
};

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

34
	refcount_t		refcnt;
35 36 37
	u32			pref;
	int			suppress_ifgroup;
	int			suppress_prefixlen;
38
	char			iifname[IFNAMSIZ];
39
	char			oifname[IFNAMSIZ];
40
	struct fib_kuid_range	uid_range;
41 42 43
	struct rcu_head		rcu;
};

E
Eric Dumazet 已提交
44
struct fib_lookup_arg {
45 46 47
	void			*lookup_ptr;
	void			*result;
	struct fib_rule		*rule;
D
David Ahern 已提交
48
	u32			table;
E
Eric Dumazet 已提交
49
	int			flags;
50 51
#define FIB_LOOKUP_NOREF		1
#define FIB_LOOKUP_IGNORE_LINKSTATE	2
52 53
};

E
Eric Dumazet 已提交
54
struct fib_rules_ops {
55 56 57
	int			family;
	struct list_head	list;
	int			rule_size;
58
	int			addr_size;
T
Thomas Graf 已提交
59 60
	int			unresolved_rules;
	int			nr_goto_rules;
61
	unsigned int		fib_rules_seq;
62 63 64 65

	int			(*action)(struct fib_rule *,
					  struct flowi *, int,
					  struct fib_lookup_arg *);
66 67
	bool			(*suppress)(struct fib_rule *,
					    struct fib_lookup_arg *);
68 69 70 71 72 73
	int			(*match)(struct fib_rule *,
					 struct flowi *, int);
	int			(*configure)(struct fib_rule *,
					     struct sk_buff *,
					     struct fib_rule_hdr *,
					     struct nlattr **);
74
	int			(*delete)(struct fib_rule *);
75 76 77 78 79
	int			(*compare)(struct fib_rule *,
					   struct fib_rule_hdr *,
					   struct nlattr **);
	int			(*fill)(struct fib_rule *, struct sk_buff *,
					struct fib_rule_hdr *);
80
	size_t			(*nlmsg_payload)(struct fib_rule *);
81

82 83
	/* Called after modifications to the rules set, must flush
	 * the route cache if one exists. */
84
	void			(*flush_cache)(struct fib_rules_ops *ops);
85

86
	int			nlgroup;
87
	const struct nla_policy	*policy;
88
	struct list_head	rules_list;
89
	struct module		*owner;
90
	struct net		*fro_net;
91
	struct rcu_head		rcu;
92 93
};

94 95 96 97 98
struct fib_rule_notifier_info {
	struct fib_notifier_info info; /* must be first */
	struct fib_rule *rule;
};

99
#define FRA_GENERIC_POLICY \
100
	[FRA_IIFNAME]	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
101
	[FRA_OIFNAME]	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
102 103 104
	[FRA_PRIORITY]	= { .type = NLA_U32 }, \
	[FRA_FWMARK]	= { .type = NLA_U32 }, \
	[FRA_FWMASK]	= { .type = NLA_U32 }, \
T
Thomas Graf 已提交
105
	[FRA_TABLE]     = { .type = NLA_U32 }, \
106
	[FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \
107
	[FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \
D
David Ahern 已提交
108
	[FRA_GOTO]	= { .type = NLA_U32 }, \
109 110
	[FRA_L3MDEV]	= { .type = NLA_U8 }, \
	[FRA_UID_RANGE]	= { .len = sizeof(struct fib_rule_uid_range) }
111

112 113
static inline void fib_rule_get(struct fib_rule *rule)
{
114
	refcount_inc(&rule->refcnt);
115 116 117 118
}

static inline void fib_rule_put(struct fib_rule *rule)
{
119
	if (refcount_dec_and_test(&rule->refcnt))
120
		kfree_rcu(rule, rcu);
121 122
}

D
David Ahern 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136
#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

137 138 139 140 141 142 143
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;
}

144 145 146
struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
					 struct net *);
void fib_rules_unregister(struct fib_rules_ops *);
147

148 149 150 151
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);
152
bool fib_rule_matchall(const struct fib_rule *rule);
153 154
int fib_rules_dump(struct net *net, struct notifier_block *nb, int family);
unsigned int fib_rules_seq_read(struct net *net, int family);
D
David Ahern 已提交
155

156 157 158 159
int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
		   struct netlink_ext_ack *extack);
int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
		   struct netlink_ext_ack *extack);
160
#endif