octeontx2-pf: Fix error return code in otx2_probe()
[linux/fpc-iii.git] / net / ipv4 / fib_rules.c
blobf99e3bac5cab20951d150b30efc438a7c1e4d404
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * IPv4 Forwarding Information Base: policy rules.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * Thomas Graf <tgraf@suug.ch>
12 * Fixes:
13 * Rani Assaf : local_rule cannot be deleted
14 * Marc Boucher : routing by fwmark
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/netdevice.h>
20 #include <linux/netlink.h>
21 #include <linux/inetdevice.h>
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/rcupdate.h>
25 #include <linux/export.h>
26 #include <net/ip.h>
27 #include <net/route.h>
28 #include <net/tcp.h>
29 #include <net/ip_fib.h>
30 #include <net/nexthop.h>
31 #include <net/fib_rules.h>
33 struct fib4_rule {
34 struct fib_rule common;
35 u8 dst_len;
36 u8 src_len;
37 u8 tos;
38 __be32 src;
39 __be32 srcmask;
40 __be32 dst;
41 __be32 dstmask;
42 #ifdef CONFIG_IP_ROUTE_CLASSID
43 u32 tclassid;
44 #endif
47 static bool fib4_rule_matchall(const struct fib_rule *rule)
49 struct fib4_rule *r = container_of(rule, struct fib4_rule, common);
51 if (r->dst_len || r->src_len || r->tos)
52 return false;
53 return fib_rule_matchall(rule);
56 bool fib4_rule_default(const struct fib_rule *rule)
58 if (!fib4_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL ||
59 rule->l3mdev)
60 return false;
61 if (rule->table != RT_TABLE_LOCAL && rule->table != RT_TABLE_MAIN &&
62 rule->table != RT_TABLE_DEFAULT)
63 return false;
64 return true;
66 EXPORT_SYMBOL_GPL(fib4_rule_default);
68 int fib4_rules_dump(struct net *net, struct notifier_block *nb,
69 struct netlink_ext_ack *extack)
71 return fib_rules_dump(net, nb, AF_INET, extack);
74 unsigned int fib4_rules_seq_read(struct net *net)
76 return fib_rules_seq_read(net, AF_INET);
79 int __fib_lookup(struct net *net, struct flowi4 *flp,
80 struct fib_result *res, unsigned int flags)
82 struct fib_lookup_arg arg = {
83 .result = res,
84 .flags = flags,
86 int err;
88 /* update flow if oif or iif point to device enslaved to l3mdev */
89 l3mdev_update_flow(net, flowi4_to_flowi(flp));
91 err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
92 #ifdef CONFIG_IP_ROUTE_CLASSID
93 if (arg.rule)
94 res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid;
95 else
96 res->tclassid = 0;
97 #endif
99 if (err == -ESRCH)
100 err = -ENETUNREACH;
102 return err;
104 EXPORT_SYMBOL_GPL(__fib_lookup);
106 static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
107 int flags, struct fib_lookup_arg *arg)
109 int err = -EAGAIN;
110 struct fib_table *tbl;
111 u32 tb_id;
113 switch (rule->action) {
114 case FR_ACT_TO_TBL:
115 break;
117 case FR_ACT_UNREACHABLE:
118 return -ENETUNREACH;
120 case FR_ACT_PROHIBIT:
121 return -EACCES;
123 case FR_ACT_BLACKHOLE:
124 default:
125 return -EINVAL;
128 rcu_read_lock();
130 tb_id = fib_rule_get_table(rule, arg);
131 tbl = fib_get_table(rule->fr_net, tb_id);
132 if (tbl)
133 err = fib_table_lookup(tbl, &flp->u.ip4,
134 (struct fib_result *)arg->result,
135 arg->flags);
137 rcu_read_unlock();
138 return err;
141 static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
143 struct fib_result *result = (struct fib_result *) arg->result;
144 struct net_device *dev = NULL;
146 if (result->fi) {
147 struct fib_nh_common *nhc = fib_info_nhc(result->fi, 0);
149 dev = nhc->nhc_dev;
152 /* do not accept result if the route does
153 * not meet the required prefix length
155 if (result->prefixlen <= rule->suppress_prefixlen)
156 goto suppress_route;
158 /* do not accept result if the route uses a device
159 * belonging to a forbidden interface group
161 if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
162 goto suppress_route;
164 return false;
166 suppress_route:
167 if (!(arg->flags & FIB_LOOKUP_NOREF))
168 fib_info_put(result->fi);
169 return true;
172 static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
174 struct fib4_rule *r = (struct fib4_rule *) rule;
175 struct flowi4 *fl4 = &fl->u.ip4;
176 __be32 daddr = fl4->daddr;
177 __be32 saddr = fl4->saddr;
179 if (((saddr ^ r->src) & r->srcmask) ||
180 ((daddr ^ r->dst) & r->dstmask))
181 return 0;
183 if (r->tos && (r->tos != fl4->flowi4_tos))
184 return 0;
186 if (rule->ip_proto && (rule->ip_proto != fl4->flowi4_proto))
187 return 0;
189 if (fib_rule_port_range_set(&rule->sport_range) &&
190 !fib_rule_port_inrange(&rule->sport_range, fl4->fl4_sport))
191 return 0;
193 if (fib_rule_port_range_set(&rule->dport_range) &&
194 !fib_rule_port_inrange(&rule->dport_range, fl4->fl4_dport))
195 return 0;
197 return 1;
200 static struct fib_table *fib_empty_table(struct net *net)
202 u32 id = 1;
204 while (1) {
205 if (!fib_get_table(net, id))
206 return fib_new_table(net, id);
208 if (id++ == RT_TABLE_MAX)
209 break;
211 return NULL;
214 static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
215 FRA_GENERIC_POLICY,
216 [FRA_FLOW] = { .type = NLA_U32 },
219 static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
220 struct fib_rule_hdr *frh,
221 struct nlattr **tb,
222 struct netlink_ext_ack *extack)
224 struct net *net = sock_net(skb->sk);
225 int err = -EINVAL;
226 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
228 if (frh->tos & ~IPTOS_TOS_MASK) {
229 NL_SET_ERR_MSG(extack, "Invalid tos");
230 goto errout;
233 /* split local/main if they are not already split */
234 err = fib_unmerge(net);
235 if (err)
236 goto errout;
238 if (rule->table == RT_TABLE_UNSPEC && !rule->l3mdev) {
239 if (rule->action == FR_ACT_TO_TBL) {
240 struct fib_table *table;
242 table = fib_empty_table(net);
243 if (!table) {
244 err = -ENOBUFS;
245 goto errout;
248 rule->table = table->tb_id;
252 if (frh->src_len)
253 rule4->src = nla_get_in_addr(tb[FRA_SRC]);
255 if (frh->dst_len)
256 rule4->dst = nla_get_in_addr(tb[FRA_DST]);
258 #ifdef CONFIG_IP_ROUTE_CLASSID
259 if (tb[FRA_FLOW]) {
260 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
261 if (rule4->tclassid)
262 net->ipv4.fib_num_tclassid_users++;
264 #endif
266 if (fib_rule_requires_fldissect(rule))
267 net->ipv4.fib_rules_require_fldissect++;
269 rule4->src_len = frh->src_len;
270 rule4->srcmask = inet_make_mask(rule4->src_len);
271 rule4->dst_len = frh->dst_len;
272 rule4->dstmask = inet_make_mask(rule4->dst_len);
273 rule4->tos = frh->tos;
275 net->ipv4.fib_has_custom_rules = true;
277 err = 0;
278 errout:
279 return err;
282 static int fib4_rule_delete(struct fib_rule *rule)
284 struct net *net = rule->fr_net;
285 int err;
287 /* split local/main if they are not already split */
288 err = fib_unmerge(net);
289 if (err)
290 goto errout;
292 #ifdef CONFIG_IP_ROUTE_CLASSID
293 if (((struct fib4_rule *)rule)->tclassid)
294 net->ipv4.fib_num_tclassid_users--;
295 #endif
296 net->ipv4.fib_has_custom_rules = true;
298 if (net->ipv4.fib_rules_require_fldissect &&
299 fib_rule_requires_fldissect(rule))
300 net->ipv4.fib_rules_require_fldissect--;
301 errout:
302 return err;
305 static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
306 struct nlattr **tb)
308 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
310 if (frh->src_len && (rule4->src_len != frh->src_len))
311 return 0;
313 if (frh->dst_len && (rule4->dst_len != frh->dst_len))
314 return 0;
316 if (frh->tos && (rule4->tos != frh->tos))
317 return 0;
319 #ifdef CONFIG_IP_ROUTE_CLASSID
320 if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
321 return 0;
322 #endif
324 if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC])))
325 return 0;
327 if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST])))
328 return 0;
330 return 1;
333 static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
334 struct fib_rule_hdr *frh)
336 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
338 frh->dst_len = rule4->dst_len;
339 frh->src_len = rule4->src_len;
340 frh->tos = rule4->tos;
342 if ((rule4->dst_len &&
343 nla_put_in_addr(skb, FRA_DST, rule4->dst)) ||
344 (rule4->src_len &&
345 nla_put_in_addr(skb, FRA_SRC, rule4->src)))
346 goto nla_put_failure;
347 #ifdef CONFIG_IP_ROUTE_CLASSID
348 if (rule4->tclassid &&
349 nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
350 goto nla_put_failure;
351 #endif
352 return 0;
354 nla_put_failure:
355 return -ENOBUFS;
358 static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
360 return nla_total_size(4) /* dst */
361 + nla_total_size(4) /* src */
362 + nla_total_size(4); /* flow */
365 static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
367 rt_cache_flush(ops->fro_net);
370 static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
371 .family = AF_INET,
372 .rule_size = sizeof(struct fib4_rule),
373 .addr_size = sizeof(u32),
374 .action = fib4_rule_action,
375 .suppress = fib4_rule_suppress,
376 .match = fib4_rule_match,
377 .configure = fib4_rule_configure,
378 .delete = fib4_rule_delete,
379 .compare = fib4_rule_compare,
380 .fill = fib4_rule_fill,
381 .nlmsg_payload = fib4_rule_nlmsg_payload,
382 .flush_cache = fib4_rule_flush_cache,
383 .nlgroup = RTNLGRP_IPV4_RULE,
384 .policy = fib4_rule_policy,
385 .owner = THIS_MODULE,
388 static int fib_default_rules_init(struct fib_rules_ops *ops)
390 int err;
392 err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
393 if (err < 0)
394 return err;
395 err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
396 if (err < 0)
397 return err;
398 err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
399 if (err < 0)
400 return err;
401 return 0;
404 int __net_init fib4_rules_init(struct net *net)
406 int err;
407 struct fib_rules_ops *ops;
409 ops = fib_rules_register(&fib4_rules_ops_template, net);
410 if (IS_ERR(ops))
411 return PTR_ERR(ops);
413 err = fib_default_rules_init(ops);
414 if (err < 0)
415 goto fail;
416 net->ipv4.rules_ops = ops;
417 net->ipv4.fib_has_custom_rules = false;
418 net->ipv4.fib_rules_require_fldissect = 0;
419 return 0;
421 fail:
422 /* also cleans all rules already added */
423 fib_rules_unregister(ops);
424 return err;
427 void __net_exit fib4_rules_exit(struct net *net)
429 fib_rules_unregister(net->ipv4.rules_ops);