1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_FIB_RULES_H
3 #define __NET_FIB_RULES_H
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>
11 #include <net/rtnetlink.h>
12 #include <net/fib_notifier.h>
13 #include <linux/indirect_call_wrapper.h>
15 struct fib_kuid_range {
21 struct list_head list;
34 struct fib_rule __rcu *ctarget;
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;
53 struct fib_lookup_arg {
55 const void *lookup_data;
57 struct fib_rule *rule;
60 #define FIB_LOOKUP_NOREF 1
61 #define FIB_LOOKUP_IGNORE_LINKSTATE 2
64 struct fib_rules_ops {
66 struct list_head list;
71 unsigned int fib_rules_seq;
73 int (*action)(struct fib_rule *,
75 struct fib_lookup_arg *);
76 bool (*suppress)(struct fib_rule *, int,
77 struct fib_lookup_arg *);
78 int (*match)(struct fib_rule *,
80 int (*configure)(struct fib_rule *,
82 struct fib_rule_hdr *,
84 struct netlink_ext_ack *);
85 int (*delete)(struct fib_rule *);
86 int (*compare)(struct fib_rule *,
87 struct fib_rule_hdr *,
89 int (*fill)(struct fib_rule *, struct sk_buff *,
90 struct fib_rule_hdr *);
91 size_t (*nlmsg_payload)(struct fib_rule *);
93 /* Called after modifications to the rules set, must flush
94 * the route cache if one exists. */
95 void (*flush_cache)(struct fib_rules_ops *ops);
98 struct list_head rules_list;
104 struct fib_rule_notifier_info {
105 struct fib_notifier_info info; /* must be first */
106 struct fib_rule *rule;
109 static inline void fib_rule_get(struct fib_rule *rule)
111 refcount_inc(&rule->refcnt);
114 static inline void fib_rule_put(struct fib_rule *rule)
116 if (refcount_dec_and_test(&rule->refcnt))
117 kfree_rcu(rule, rcu);
120 #ifdef CONFIG_NET_L3_MASTER_DEV
121 static inline u32 fib_rule_get_table(struct fib_rule *rule,
122 struct fib_lookup_arg *arg)
124 return rule->l3mdev ? arg->table : rule->table;
127 static inline u32 fib_rule_get_table(struct fib_rule *rule,
128 struct fib_lookup_arg *arg)
134 static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
137 return nla_get_u32(nla[FRA_TABLE]);
141 static inline bool fib_rule_port_range_set(const struct fib_rule_port_range *range)
143 return range->start != 0 && range->end != 0;
146 static inline bool fib_rule_port_inrange(const struct fib_rule_port_range *a,
149 return ntohs(port) >= a->start &&
150 ntohs(port) <= a->end;
153 static inline bool fib_rule_port_match(const struct fib_rule_port_range *range,
154 u16 port_mask, __be16 port)
156 if ((range->start ^ ntohs(port)) & port_mask)
158 if (!port_mask && fib_rule_port_range_set(range) &&
159 !fib_rule_port_inrange(range, port))
164 static inline bool fib_rule_port_range_valid(const struct fib_rule_port_range *a)
166 return a->start != 0 && a->end != 0 && a->end < 0xffff &&
170 static inline bool fib_rule_port_range_compare(struct fib_rule_port_range *a,
171 struct fib_rule_port_range *b)
173 return a->start == b->start &&
178 fib_rule_port_is_range(const struct fib_rule_port_range *range)
180 return range->start != range->end;
183 static inline bool fib_rule_requires_fldissect(struct fib_rule *rule)
185 return rule->iifindex != LOOPBACK_IFINDEX && (rule->ip_proto ||
186 fib_rule_port_range_set(&rule->sport_range) ||
187 fib_rule_port_range_set(&rule->dport_range));
190 struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
192 void fib_rules_unregister(struct fib_rules_ops *);
194 int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
195 struct fib_lookup_arg *);
196 int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table);
197 bool fib_rule_matchall(const struct fib_rule *rule);
198 int fib_rules_dump(struct net *net, struct notifier_block *nb, int family,
199 struct netlink_ext_ack *extack);
200 unsigned int fib_rules_seq_read(const struct net *net, int family);
202 int fib_newrule(struct net *net, struct sk_buff *skb, struct nlmsghdr *nlh,
203 struct netlink_ext_ack *extack, bool rtnl_held);
204 int fib_delrule(struct net *net, struct sk_buff *skb, struct nlmsghdr *nlh,
205 struct netlink_ext_ack *extack, bool rtnl_held);
207 INDIRECT_CALLABLE_DECLARE(int fib6_rule_match(struct fib_rule *rule,
208 struct flowi *fl, int flags));
209 INDIRECT_CALLABLE_DECLARE(int fib4_rule_match(struct fib_rule *rule,
210 struct flowi *fl, int flags));
212 INDIRECT_CALLABLE_DECLARE(int fib6_rule_action(struct fib_rule *rule,
213 struct flowi *flp, int flags,
214 struct fib_lookup_arg *arg));
215 INDIRECT_CALLABLE_DECLARE(int fib4_rule_action(struct fib_rule *rule,
216 struct flowi *flp, int flags,
217 struct fib_lookup_arg *arg));
219 INDIRECT_CALLABLE_DECLARE(bool fib6_rule_suppress(struct fib_rule *rule,
221 struct fib_lookup_arg *arg));
222 INDIRECT_CALLABLE_DECLARE(bool fib4_rule_suppress(struct fib_rule *rule,
224 struct fib_lookup_arg *arg));