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