1 // SPDX-License-Identifier: GPL-2.0-or-later
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.
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>
27 #include <net/route.h>
29 #include <net/ip_fib.h>
30 #include <net/nexthop.h>
31 #include <net/fib_rules.h>
34 struct fib_rule common;
42 #ifdef CONFIG_IP_ROUTE_CLASSID
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)
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 ||
61 if (rule->table != RT_TABLE_LOCAL && rule->table != RT_TABLE_MAIN &&
62 rule->table != RT_TABLE_DEFAULT)
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 = {
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
94 res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid;
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)
110 struct fib_table *tbl;
113 switch (rule->action) {
117 case FR_ACT_UNREACHABLE:
120 case FR_ACT_PROHIBIT:
123 case FR_ACT_BLACKHOLE:
130 tb_id = fib_rule_get_table(rule, arg);
131 tbl = fib_get_table(rule->fr_net, tb_id);
133 err = fib_table_lookup(tbl, &flp->u.ip4,
134 (struct fib_result *)arg->result,
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;
147 struct fib_nh_common *nhc = fib_info_nhc(result->fi, 0);
152 /* do not accept result if the route does
153 * not meet the required prefix length
155 if (result->prefixlen <= rule->suppress_prefixlen)
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)
167 if (!(arg->flags & FIB_LOOKUP_NOREF))
168 fib_info_put(result->fi);
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))
183 if (r->tos && (r->tos != fl4->flowi4_tos))
186 if (rule->ip_proto && (rule->ip_proto != fl4->flowi4_proto))
189 if (fib_rule_port_range_set(&rule->sport_range) &&
190 !fib_rule_port_inrange(&rule->sport_range, fl4->fl4_sport))
193 if (fib_rule_port_range_set(&rule->dport_range) &&
194 !fib_rule_port_inrange(&rule->dport_range, fl4->fl4_dport))
200 static struct fib_table *fib_empty_table(struct net *net)
205 if (!fib_get_table(net, id))
206 return fib_new_table(net, id);
208 if (id++ == RT_TABLE_MAX)
214 static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
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,
222 struct netlink_ext_ack *extack)
224 struct net *net = sock_net(skb->sk);
226 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
228 if (frh->tos & ~IPTOS_TOS_MASK) {
229 NL_SET_ERR_MSG(extack, "Invalid tos");
233 /* split local/main if they are not already split */
234 err = fib_unmerge(net);
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);
248 rule->table = table->tb_id;
253 rule4->src = nla_get_in_addr(tb[FRA_SRC]);
256 rule4->dst = nla_get_in_addr(tb[FRA_DST]);
258 #ifdef CONFIG_IP_ROUTE_CLASSID
260 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
262 net->ipv4.fib_num_tclassid_users++;
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;
282 static int fib4_rule_delete(struct fib_rule *rule)
284 struct net *net = rule->fr_net;
287 /* split local/main if they are not already split */
288 err = fib_unmerge(net);
292 #ifdef CONFIG_IP_ROUTE_CLASSID
293 if (((struct fib4_rule *)rule)->tclassid)
294 net->ipv4.fib_num_tclassid_users--;
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--;
305 static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
308 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
310 if (frh->src_len && (rule4->src_len != frh->src_len))
313 if (frh->dst_len && (rule4->dst_len != frh->dst_len))
316 if (frh->tos && (rule4->tos != frh->tos))
319 #ifdef CONFIG_IP_ROUTE_CLASSID
320 if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
324 if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC])))
327 if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST])))
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)) ||
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;
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 = {
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)
392 err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
395 err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
398 err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
404 int __net_init fib4_rules_init(struct net *net)
407 struct fib_rules_ops *ops;
409 ops = fib_rules_register(&fib4_rules_ops_template, net);
413 err = fib_default_rules_init(ops);
416 net->ipv4.rules_ops = ops;
417 net->ipv4.fib_has_custom_rules = false;
418 net->ipv4.fib_rules_require_fldissect = 0;
422 /* also cleans all rules already added */
423 fib_rules_unregister(ops);
427 void __net_exit fib4_rules_exit(struct net *net)
429 fib_rules_unregister(net->ipv4.rules_ops);