fib_rules.h 4.2 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
2 3 4 5
#ifndef __NET_FIB_RULES_H
#define __NET_FIB_RULES_H

#include <linux/types.h>
6
#include <linux/slab.h>
7 8
#include <linux/netdevice.h>
#include <linux/fib_rules.h>
9
#include <linux/refcount.h>
10
#include <net/flow.h>
11
#include <net/rtnetlink.h>
12
#include <net/fib_notifier.h>
13

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

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

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

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

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

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

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

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

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

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

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

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

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

139 140 141 142 143 144 145
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;
}

146 147 148
struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
					 struct net *);
void fib_rules_unregister(struct fib_rules_ops *);
149

150 151 152 153
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);
154
bool fib_rule_matchall(const struct fib_rule *rule);
155 156
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 已提交
157

158 159 160 161
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);
162
#endif