xref: /linux/include/net/fib_rules.h (revision d7484babd2c4dcfa1ca02e7e303fab3fab529d75)
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 	u16			sport_mask;
47 	u16			dport_mask;
48 	struct rcu_head		rcu;
49 };
50 
51 struct fib_lookup_arg {
52 	void			*lookup_ptr;
53 	const void		*lookup_data;
54 	void			*result;
55 	struct fib_rule		*rule;
56 	u32			table;
57 	int			flags;
58 #define FIB_LOOKUP_NOREF		1
59 #define FIB_LOOKUP_IGNORE_LINKSTATE	2
60 };
61 
62 struct fib_rules_ops {
63 	int			family;
64 	struct list_head	list;
65 	int			rule_size;
66 	int			addr_size;
67 	int			unresolved_rules;
68 	int			nr_goto_rules;
69 	unsigned int		fib_rules_seq;
70 
71 	int			(*action)(struct fib_rule *,
72 					  struct flowi *, int,
73 					  struct fib_lookup_arg *);
74 	bool			(*suppress)(struct fib_rule *, int,
75 					    struct fib_lookup_arg *);
76 	int			(*match)(struct fib_rule *,
77 					 struct flowi *, int);
78 	int			(*configure)(struct fib_rule *,
79 					     struct sk_buff *,
80 					     struct fib_rule_hdr *,
81 					     struct nlattr **,
82 					     struct netlink_ext_ack *);
83 	int			(*delete)(struct fib_rule *);
84 	int			(*compare)(struct fib_rule *,
85 					   struct fib_rule_hdr *,
86 					   struct nlattr **);
87 	int			(*fill)(struct fib_rule *, struct sk_buff *,
88 					struct fib_rule_hdr *);
89 	size_t			(*nlmsg_payload)(struct fib_rule *);
90 
91 	/* Called after modifications to the rules set, must flush
92 	 * the route cache if one exists. */
93 	void			(*flush_cache)(struct fib_rules_ops *ops);
94 
95 	int			nlgroup;
96 	struct list_head	rules_list;
97 	struct module		*owner;
98 	struct net		*fro_net;
99 	struct rcu_head		rcu;
100 };
101 
102 struct fib_rule_notifier_info {
103 	struct fib_notifier_info info; /* must be first */
104 	struct fib_rule *rule;
105 };
106 
107 static inline void fib_rule_get(struct fib_rule *rule)
108 {
109 	refcount_inc(&rule->refcnt);
110 }
111 
112 static inline void fib_rule_put(struct fib_rule *rule)
113 {
114 	if (refcount_dec_and_test(&rule->refcnt))
115 		kfree_rcu(rule, rcu);
116 }
117 
118 #ifdef CONFIG_NET_L3_MASTER_DEV
119 static inline u32 fib_rule_get_table(struct fib_rule *rule,
120 				     struct fib_lookup_arg *arg)
121 {
122 	return rule->l3mdev ? arg->table : rule->table;
123 }
124 #else
125 static inline u32 fib_rule_get_table(struct fib_rule *rule,
126 				     struct fib_lookup_arg *arg)
127 {
128 	return rule->table;
129 }
130 #endif
131 
132 static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
133 {
134 	if (nla[FRA_TABLE])
135 		return nla_get_u32(nla[FRA_TABLE]);
136 	return frh->table;
137 }
138 
139 static inline bool fib_rule_port_range_set(const struct fib_rule_port_range *range)
140 {
141 	return range->start != 0 && range->end != 0;
142 }
143 
144 static inline bool fib_rule_port_inrange(const struct fib_rule_port_range *a,
145 					 __be16 port)
146 {
147 	return ntohs(port) >= a->start &&
148 		ntohs(port) <= a->end;
149 }
150 
151 static inline bool fib_rule_port_match(const struct fib_rule_port_range *range,
152 				       u16 port_mask, __be16 port)
153 {
154 	if ((range->start ^ ntohs(port)) & port_mask)
155 		return false;
156 	if (!port_mask && fib_rule_port_range_set(range) &&
157 	    !fib_rule_port_inrange(range, port))
158 		return false;
159 	return true;
160 }
161 
162 static inline bool fib_rule_port_range_valid(const struct fib_rule_port_range *a)
163 {
164 	return a->start != 0 && a->end != 0 && a->end < 0xffff &&
165 		a->start <= a->end;
166 }
167 
168 static inline bool fib_rule_port_range_compare(struct fib_rule_port_range *a,
169 					       struct fib_rule_port_range *b)
170 {
171 	return a->start == b->start &&
172 		a->end == b->end;
173 }
174 
175 static inline bool
176 fib_rule_port_is_range(const struct fib_rule_port_range *range)
177 {
178 	return range->start != range->end;
179 }
180 
181 static inline bool fib_rule_requires_fldissect(struct fib_rule *rule)
182 {
183 	return rule->iifindex != LOOPBACK_IFINDEX && (rule->ip_proto ||
184 		fib_rule_port_range_set(&rule->sport_range) ||
185 		fib_rule_port_range_set(&rule->dport_range));
186 }
187 
188 struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
189 					 struct net *);
190 void fib_rules_unregister(struct fib_rules_ops *);
191 
192 int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
193 		     struct fib_lookup_arg *);
194 int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table);
195 bool fib_rule_matchall(const struct fib_rule *rule);
196 int fib_rules_dump(struct net *net, struct notifier_block *nb, int family,
197 		   struct netlink_ext_ack *extack);
198 unsigned int fib_rules_seq_read(const struct net *net, int family);
199 
200 int fib_newrule(struct net *net, struct sk_buff *skb, struct nlmsghdr *nlh,
201 		struct netlink_ext_ack *extack, bool rtnl_held);
202 int fib_delrule(struct net *net, struct sk_buff *skb, struct nlmsghdr *nlh,
203 		struct netlink_ext_ack *extack, bool rtnl_held);
204 
205 INDIRECT_CALLABLE_DECLARE(int fib6_rule_match(struct fib_rule *rule,
206 					    struct flowi *fl, int flags));
207 INDIRECT_CALLABLE_DECLARE(int fib4_rule_match(struct fib_rule *rule,
208 					    struct flowi *fl, int flags));
209 
210 INDIRECT_CALLABLE_DECLARE(int fib6_rule_action(struct fib_rule *rule,
211 			    struct flowi *flp, int flags,
212 			    struct fib_lookup_arg *arg));
213 INDIRECT_CALLABLE_DECLARE(int fib4_rule_action(struct fib_rule *rule,
214 			    struct flowi *flp, int flags,
215 			    struct fib_lookup_arg *arg));
216 
217 INDIRECT_CALLABLE_DECLARE(bool fib6_rule_suppress(struct fib_rule *rule,
218 						int flags,
219 						struct fib_lookup_arg *arg));
220 INDIRECT_CALLABLE_DECLARE(bool fib4_rule_suppress(struct fib_rule *rule,
221 						int flags,
222 						struct fib_lookup_arg *arg));
223 #endif
224