• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_FIB_RULES_H
3 #define __NET_FIB_RULES_H
4 
5 #include <linux/types.h>
6 #include <linux/slab.h>
7 #include <linux/netdevice.h>
8 #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 #include <linux/indirect_call_wrapper.h>
14 
15 struct fib_kuid_range {
16 	kuid_t start;
17 	kuid_t end;
18 };
19 
20 struct fib_rule {
21 	struct list_head	list;
22 	int			iifindex;
23 	int			oifindex;
24 	u32			mark;
25 	u32			mark_mask;
26 	u32			flags;
27 	u32			table;
28 	u8			action;
29 	u8			l3mdev;
30 	u8                      proto;
31 	u8			ip_proto;
32 	u32			target;
33 	__be64			tun_id;
34 	struct fib_rule __rcu	*ctarget;
35 	struct net		*fr_net;
36 
37 	refcount_t		refcnt;
38 	u32			pref;
39 	int			suppress_ifgroup;
40 	int			suppress_prefixlen;
41 	char			iifname[IFNAMSIZ];
42 	char			oifname[IFNAMSIZ];
43 	struct fib_kuid_range	uid_range;
44 	struct fib_rule_port_range	sport_range;
45 	struct fib_rule_port_range	dport_range;
46 	struct rcu_head		rcu;
47 };
48 
49 struct fib_lookup_arg {
50 	void			*lookup_ptr;
51 	const void		*lookup_data;
52 	void			*result;
53 	struct fib_rule		*rule;
54 	u32			table;
55 	int			flags;
56 #define FIB_LOOKUP_NOREF		1
57 #define FIB_LOOKUP_IGNORE_LINKSTATE	2
58 };
59 
60 struct fib_rules_ops {
61 	int			family;
62 	struct list_head	list;
63 	int			rule_size;
64 	int			addr_size;
65 	int			unresolved_rules;
66 	int			nr_goto_rules;
67 	unsigned int		fib_rules_seq;
68 
69 	int			(*action)(struct fib_rule *,
70 					  struct flowi *, int,
71 					  struct fib_lookup_arg *);
72 	/* __GENKSYMS__ hack to preserve the abi change that happened in
73 	 * cdef485217d3 ("ipv6: fix memory leak in fib6_rule_suppress")
74 	 */
75 #ifdef __GENKSYMS__
76 	bool			(*suppress)(struct fib_rule *,
77 #else
78 	bool			(*suppress)(struct fib_rule *, int,
79 #endif
80 					    struct fib_lookup_arg *);
81 	int			(*match)(struct fib_rule *,
82 					 struct flowi *, int);
83 	int			(*configure)(struct fib_rule *,
84 					     struct sk_buff *,
85 					     struct fib_rule_hdr *,
86 					     struct nlattr **,
87 					     struct netlink_ext_ack *);
88 	int			(*delete)(struct fib_rule *);
89 	int			(*compare)(struct fib_rule *,
90 					   struct fib_rule_hdr *,
91 					   struct nlattr **);
92 	int			(*fill)(struct fib_rule *, struct sk_buff *,
93 					struct fib_rule_hdr *);
94 	size_t			(*nlmsg_payload)(struct fib_rule *);
95 
96 	/* Called after modifications to the rules set, must flush
97 	 * the route cache if one exists. */
98 	void			(*flush_cache)(struct fib_rules_ops *ops);
99 
100 	int			nlgroup;
101 	const struct nla_policy	*policy;
102 	struct list_head	rules_list;
103 	struct module		*owner;
104 	struct net		*fro_net;
105 	struct rcu_head		rcu;
106 };
107 
108 struct fib_rule_notifier_info {
109 	struct fib_notifier_info info; /* must be first */
110 	struct fib_rule *rule;
111 };
112 
113 #define FRA_GENERIC_POLICY \
114 	[FRA_UNSPEC]	= { .strict_start_type = FRA_DPORT_RANGE + 1 }, \
115 	[FRA_IIFNAME]	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
116 	[FRA_OIFNAME]	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
117 	[FRA_PRIORITY]	= { .type = NLA_U32 }, \
118 	[FRA_FWMARK]	= { .type = NLA_U32 }, \
119 	[FRA_TUN_ID]	= { .type = NLA_U64 }, \
120 	[FRA_FWMASK]	= { .type = NLA_U32 }, \
121 	[FRA_TABLE]     = { .type = NLA_U32 }, \
122 	[FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \
123 	[FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \
124 	[FRA_GOTO]	= { .type = NLA_U32 }, \
125 	[FRA_L3MDEV]	= { .type = NLA_U8 }, \
126 	[FRA_UID_RANGE]	= { .len = sizeof(struct fib_rule_uid_range) }, \
127 	[FRA_PROTOCOL]  = { .type = NLA_U8 }, \
128 	[FRA_IP_PROTO]  = { .type = NLA_U8 }, \
129 	[FRA_SPORT_RANGE] = { .len = sizeof(struct fib_rule_port_range) }, \
130 	[FRA_DPORT_RANGE] = { .len = sizeof(struct fib_rule_port_range) }
131 
132 
fib_rule_get(struct fib_rule * rule)133 static inline void fib_rule_get(struct fib_rule *rule)
134 {
135 	refcount_inc(&rule->refcnt);
136 }
137 
fib_rule_put(struct fib_rule * rule)138 static inline void fib_rule_put(struct fib_rule *rule)
139 {
140 	if (refcount_dec_and_test(&rule->refcnt))
141 		kfree_rcu(rule, rcu);
142 }
143 
144 #ifdef CONFIG_NET_L3_MASTER_DEV
fib_rule_get_table(struct fib_rule * rule,struct fib_lookup_arg * arg)145 static inline u32 fib_rule_get_table(struct fib_rule *rule,
146 				     struct fib_lookup_arg *arg)
147 {
148 	return rule->l3mdev ? arg->table : rule->table;
149 }
150 #else
fib_rule_get_table(struct fib_rule * rule,struct fib_lookup_arg * arg)151 static inline u32 fib_rule_get_table(struct fib_rule *rule,
152 				     struct fib_lookup_arg *arg)
153 {
154 	return rule->table;
155 }
156 #endif
157 
frh_get_table(struct fib_rule_hdr * frh,struct nlattr ** nla)158 static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
159 {
160 	if (nla[FRA_TABLE])
161 		return nla_get_u32(nla[FRA_TABLE]);
162 	return frh->table;
163 }
164 
fib_rule_port_range_set(const struct fib_rule_port_range * range)165 static inline bool fib_rule_port_range_set(const struct fib_rule_port_range *range)
166 {
167 	return range->start != 0 && range->end != 0;
168 }
169 
fib_rule_port_inrange(const struct fib_rule_port_range * a,__be16 port)170 static inline bool fib_rule_port_inrange(const struct fib_rule_port_range *a,
171 					 __be16 port)
172 {
173 	return ntohs(port) >= a->start &&
174 		ntohs(port) <= a->end;
175 }
176 
fib_rule_port_range_valid(const struct fib_rule_port_range * a)177 static inline bool fib_rule_port_range_valid(const struct fib_rule_port_range *a)
178 {
179 	return a->start != 0 && a->end != 0 && a->end < 0xffff &&
180 		a->start <= a->end;
181 }
182 
fib_rule_port_range_compare(struct fib_rule_port_range * a,struct fib_rule_port_range * b)183 static inline bool fib_rule_port_range_compare(struct fib_rule_port_range *a,
184 					       struct fib_rule_port_range *b)
185 {
186 	return a->start == b->start &&
187 		a->end == b->end;
188 }
189 
fib_rule_requires_fldissect(struct fib_rule * rule)190 static inline bool fib_rule_requires_fldissect(struct fib_rule *rule)
191 {
192 	return rule->iifindex != LOOPBACK_IFINDEX && (rule->ip_proto ||
193 		fib_rule_port_range_set(&rule->sport_range) ||
194 		fib_rule_port_range_set(&rule->dport_range));
195 }
196 
197 struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
198 					 struct net *);
199 void fib_rules_unregister(struct fib_rules_ops *);
200 
201 int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
202 		     struct fib_lookup_arg *);
203 int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
204 			 u32 flags);
205 bool fib_rule_matchall(const struct fib_rule *rule);
206 int fib_rules_dump(struct net *net, struct notifier_block *nb, int family,
207 		   struct netlink_ext_ack *extack);
208 unsigned int fib_rules_seq_read(struct net *net, int family);
209 
210 int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
211 		   struct netlink_ext_ack *extack);
212 int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
213 		   struct netlink_ext_ack *extack);
214 
215 INDIRECT_CALLABLE_DECLARE(int fib6_rule_match(struct fib_rule *rule,
216 					    struct flowi *fl, int flags));
217 INDIRECT_CALLABLE_DECLARE(int fib4_rule_match(struct fib_rule *rule,
218 					    struct flowi *fl, int flags));
219 
220 INDIRECT_CALLABLE_DECLARE(int fib6_rule_action(struct fib_rule *rule,
221 			    struct flowi *flp, int flags,
222 			    struct fib_lookup_arg *arg));
223 INDIRECT_CALLABLE_DECLARE(int fib4_rule_action(struct fib_rule *rule,
224 			    struct flowi *flp, int flags,
225 			    struct fib_lookup_arg *arg));
226 
227 INDIRECT_CALLABLE_DECLARE(bool fib6_rule_suppress(struct fib_rule *rule,
228 						int flags,
229 						struct fib_lookup_arg *arg));
230 INDIRECT_CALLABLE_DECLARE(bool fib4_rule_suppress(struct fib_rule *rule,
231 						int flags,
232 						struct fib_lookup_arg *arg));
233 #endif
234