Merge tag 'io_uring-6.16-20250630' of git://git.kernel.dk/linux
[linux-block.git] / include / net / fib_rules.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
14c0b97d
TG
2#ifndef __NET_FIB_RULES_H
3#define __NET_FIB_RULES_H
4
5#include <linux/types.h>
5a0e3ad6 6#include <linux/slab.h>
14c0b97d
TG
7#include <linux/netdevice.h>
8#include <linux/fib_rules.h>
717d1e99 9#include <linux/refcount.h>
14c0b97d 10#include <net/flow.h>
9d9e6a58 11#include <net/rtnetlink.h>
1b2a4440 12#include <net/fib_notifier.h>
b9aaec8f 13#include <linux/indirect_call_wrapper.h>
14c0b97d 14
622ec2c9
LC
15struct fib_kuid_range {
16 kuid_t start;
17 kuid_t end;
18};
19
fd2c3ef7 20struct fib_rule {
14c0b97d 21 struct list_head list;
491deb24 22 int iifindex;
1b038a5e 23 int oifindex;
b8964ed9
TG
24 u32 mark;
25 u32 mark_mask;
14c0b97d
TG
26 u32 flags;
27 u32 table;
28 u8 action;
96c63fa7 29 u8 l3mdev;
cac56209 30 u8 proto;
bfff4862 31 u8 ip_proto;
0947c9fe 32 u32 target;
e7030878 33 __be64 tun_id;
7a2b03c5 34 struct fib_rule __rcu *ctarget;
fba3679d
ED
35 struct net *fr_net;
36
717d1e99 37 refcount_t refcnt;
fba3679d
ED
38 u32 pref;
39 int suppress_ifgroup;
40 int suppress_prefixlen;
491deb24 41 char iifname[IFNAMSIZ];
1b038a5e 42 char oifname[IFNAMSIZ];
622ec2c9 43 struct fib_kuid_range uid_range;
bfff4862
RP
44 struct fib_rule_port_range sport_range;
45 struct fib_rule_port_range dport_range;
da766594
IS
46 u16 sport_mask;
47 u16 dport_mask;
2d300ce0
IS
48 u8 iif_is_l3_master;
49 u8 oif_is_l3_master;
14c0b97d
TG
50 struct rcu_head rcu;
51};
52
fd2c3ef7 53struct fib_lookup_arg {
14c0b97d 54 void *lookup_ptr;
b75cc8f9 55 const void *lookup_data;
14c0b97d
TG
56 void *result;
57 struct fib_rule *rule;
96c63fa7 58 u32 table;
ebc0ffae 59 int flags;
0eeb075f
AG
60#define FIB_LOOKUP_NOREF 1
61#define FIB_LOOKUP_IGNORE_LINKSTATE 2
14c0b97d
TG
62};
63
fd2c3ef7 64struct fib_rules_ops {
14c0b97d
TG
65 int family;
66 struct list_head list;
67 int rule_size;
e1701c68 68 int addr_size;
0947c9fe
TG
69 int unresolved_rules;
70 int nr_goto_rules;
1b2a4440 71 unsigned int fib_rules_seq;
14c0b97d
TG
72
73 int (*action)(struct fib_rule *,
74 struct flowi *, int,
75 struct fib_lookup_arg *);
cdef4852 76 bool (*suppress)(struct fib_rule *, int,
7764a45a 77 struct fib_lookup_arg *);
14c0b97d
TG
78 int (*match)(struct fib_rule *,
79 struct flowi *, int);
80 int (*configure)(struct fib_rule *,
81 struct sk_buff *,
14c0b97d 82 struct fib_rule_hdr *,
b16fb418
RP
83 struct nlattr **,
84 struct netlink_ext_ack *);
0ddcf43d 85 int (*delete)(struct fib_rule *);
14c0b97d
TG
86 int (*compare)(struct fib_rule *,
87 struct fib_rule_hdr *,
88 struct nlattr **);
89 int (*fill)(struct fib_rule *, struct sk_buff *,
14c0b97d 90 struct fib_rule_hdr *);
339bf98f 91 size_t (*nlmsg_payload)(struct fib_rule *);
14c0b97d 92
73417f61
TG
93 /* Called after modifications to the rules set, must flush
94 * the route cache if one exists. */
ae299fc0 95 void (*flush_cache)(struct fib_rules_ops *ops);
73417f61 96
14c0b97d 97 int nlgroup;
76c72d4f 98 struct list_head rules_list;
14c0b97d 99 struct module *owner;
03592383 100 struct net *fro_net;
e9c5158a 101 struct rcu_head rcu;
14c0b97d
TG
102};
103
1b2a4440
IS
104struct fib_rule_notifier_info {
105 struct fib_notifier_info info; /* must be first */
106 struct fib_rule *rule;
107};
108
14c0b97d
TG
109static inline void fib_rule_get(struct fib_rule *rule)
110{
717d1e99 111 refcount_inc(&rule->refcnt);
14c0b97d
TG
112}
113
14c0b97d
TG
114static inline void fib_rule_put(struct fib_rule *rule)
115{
717d1e99 116 if (refcount_dec_and_test(&rule->refcnt))
efd7ef1c 117 kfree_rcu(rule, rcu);
14c0b97d
TG
118}
119
96c63fa7
DA
120#ifdef CONFIG_NET_L3_MASTER_DEV
121static inline u32 fib_rule_get_table(struct fib_rule *rule,
122 struct fib_lookup_arg *arg)
123{
124 return rule->l3mdev ? arg->table : rule->table;
125}
126#else
127static inline u32 fib_rule_get_table(struct fib_rule *rule,
128 struct fib_lookup_arg *arg)
129{
130 return rule->table;
131}
132#endif
133
9e762a4a
PM
134static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
135{
136 if (nla[FRA_TABLE])
137 return nla_get_u32(nla[FRA_TABLE]);
138 return frh->table;
139}
140
bfff4862
RP
141static inline bool fib_rule_port_range_set(const struct fib_rule_port_range *range)
142{
143 return range->start != 0 && range->end != 0;
144}
145
146static inline bool fib_rule_port_inrange(const struct fib_rule_port_range *a,
147 __be16 port)
148{
149 return ntohs(port) >= a->start &&
150 ntohs(port) <= a->end;
151}
152
79a4e215
IS
153static inline bool fib_rule_port_match(const struct fib_rule_port_range *range,
154 u16 port_mask, __be16 port)
155{
156 if ((range->start ^ ntohs(port)) & port_mask)
157 return false;
158 if (!port_mask && fib_rule_port_range_set(range) &&
159 !fib_rule_port_inrange(range, port))
160 return false;
161 return true;
162}
163
bfff4862
RP
164static inline bool fib_rule_port_range_valid(const struct fib_rule_port_range *a)
165{
166 return a->start != 0 && a->end != 0 && a->end < 0xffff &&
167 a->start <= a->end;
168}
169
170static inline bool fib_rule_port_range_compare(struct fib_rule_port_range *a,
171 struct fib_rule_port_range *b)
172{
173 return a->start == b->start &&
174 a->end == b->end;
175}
176
da766594
IS
177static inline bool
178fib_rule_port_is_range(const struct fib_rule_port_range *range)
179{
180 return range->start != range->end;
181}
182
bfff4862
RP
183static inline bool fib_rule_requires_fldissect(struct fib_rule *rule)
184{
63f9ba1b 185 return rule->iifindex != LOOPBACK_IFINDEX && (rule->ip_proto ||
bfff4862 186 fib_rule_port_range_set(&rule->sport_range) ||
63f9ba1b 187 fib_rule_port_range_set(&rule->dport_range));
bfff4862
RP
188}
189
8de6879f
JP
190struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
191 struct net *);
192void fib_rules_unregister(struct fib_rules_ops *);
14c0b97d 193
8de6879f
JP
194int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
195 struct fib_lookup_arg *);
b4c1d4d9 196int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table);
3c71006d 197bool fib_rule_matchall(const struct fib_rule *rule);
b7a59557
JP
198int fib_rules_dump(struct net *net, struct notifier_block *nb, int family,
199 struct netlink_ext_ack *extack);
a716ff52 200unsigned int fib_rules_seq_read(const struct net *net, int family);
96c63fa7 201
a0596c2c
KI
202int fib_newrule(struct net *net, struct sk_buff *skb, struct nlmsghdr *nlh,
203 struct netlink_ext_ack *extack, bool rtnl_held);
204int fib_delrule(struct net *net, struct sk_buff *skb, struct nlmsghdr *nlh,
205 struct netlink_ext_ack *extack, bool rtnl_held);
b9aaec8f
BV
206
207INDIRECT_CALLABLE_DECLARE(int fib6_rule_match(struct fib_rule *rule,
208 struct flowi *fl, int flags));
209INDIRECT_CALLABLE_DECLARE(int fib4_rule_match(struct fib_rule *rule,
210 struct flowi *fl, int flags));
211
212INDIRECT_CALLABLE_DECLARE(int fib6_rule_action(struct fib_rule *rule,
213 struct flowi *flp, int flags,
214 struct fib_lookup_arg *arg));
215INDIRECT_CALLABLE_DECLARE(int fib4_rule_action(struct fib_rule *rule,
216 struct flowi *flp, int flags,
217 struct fib_lookup_arg *arg));
218
219INDIRECT_CALLABLE_DECLARE(bool fib6_rule_suppress(struct fib_rule *rule,
cdef4852 220 int flags,
b9aaec8f
BV
221 struct fib_lookup_arg *arg));
222INDIRECT_CALLABLE_DECLARE(bool fib4_rule_suppress(struct fib_rule *rule,
cdef4852 223 int flags,
b9aaec8f 224 struct fib_lookup_arg *arg));
14c0b97d 225#endif