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.
6 *
7 * IPv4 Forwarding Information Base: policy rules.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * Thomas Graf <tgraf@suug.ch>
11 *
12 * Fixes:
13 * Rani Assaf : local_rule cannot be deleted
14 * Marc Boucher : routing by fwmark
15 */
16
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/flow.h>
27 #include <net/inet_dscp.h>
28 #include <net/ip.h>
29 #include <net/route.h>
30 #include <net/tcp.h>
31 #include <net/ip_fib.h>
32 #include <net/nexthop.h>
33 #include <net/fib_rules.h>
34 #include <linux/indirect_call_wrapper.h>
35
36 struct fib4_rule {
37 struct fib_rule common;
38 u8 dst_len;
39 u8 src_len;
40 dscp_t dscp;
41 dscp_t dscp_mask;
42 u8 dscp_full:1; /* DSCP or TOS selector */
43 __be32 src;
44 __be32 srcmask;
45 __be32 dst;
46 __be32 dstmask;
47 #ifdef CONFIG_IP_ROUTE_CLASSID
48 u32 tclassid;
49 #endif
50 };
51
fib4_rule_matchall(const struct fib_rule * rule)52 static bool fib4_rule_matchall(const struct fib_rule *rule)
53 {
54 struct fib4_rule *r = container_of(rule, struct fib4_rule, common);
55
56 if (r->dst_len || r->src_len || r->dscp)
57 return false;
58 return fib_rule_matchall(rule);
59 }
60
fib4_rule_default(const struct fib_rule * rule)61 bool fib4_rule_default(const struct fib_rule *rule)
62 {
63 if (!fib4_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL ||
64 rule->l3mdev)
65 return false;
66 if (rule->table != RT_TABLE_LOCAL && rule->table != RT_TABLE_MAIN &&
67 rule->table != RT_TABLE_DEFAULT)
68 return false;
69 return true;
70 }
71 EXPORT_SYMBOL_GPL(fib4_rule_default);
72
fib4_rules_dump(struct net * net,struct notifier_block * nb,struct netlink_ext_ack * extack)73 int fib4_rules_dump(struct net *net, struct notifier_block *nb,
74 struct netlink_ext_ack *extack)
75 {
76 return fib_rules_dump(net, nb, AF_INET, extack);
77 }
78
fib4_rules_seq_read(const struct net * net)79 unsigned int fib4_rules_seq_read(const struct net *net)
80 {
81 return fib_rules_seq_read(net, AF_INET);
82 }
83
__fib_lookup(struct net * net,struct flowi4 * flp,struct fib_result * res,unsigned int flags)84 int __fib_lookup(struct net *net, struct flowi4 *flp,
85 struct fib_result *res, unsigned int flags)
86 {
87 struct fib_lookup_arg arg = {
88 .result = res,
89 .flags = flags,
90 };
91 int err;
92
93 /* update flow if oif or iif point to device enslaved to l3mdev */
94 l3mdev_update_flow(net, flowi4_to_flowi(flp));
95
96 err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
97 #ifdef CONFIG_IP_ROUTE_CLASSID
98 if (arg.rule)
99 res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid;
100 else
101 res->tclassid = 0;
102 #endif
103
104 if (err == -ESRCH)
105 err = -ENETUNREACH;
106
107 return err;
108 }
109 EXPORT_SYMBOL_GPL(__fib_lookup);
110
fib4_rule_action(struct fib_rule * rule,struct flowi * flp,int flags,struct fib_lookup_arg * arg)111 INDIRECT_CALLABLE_SCOPE int fib4_rule_action(struct fib_rule *rule,
112 struct flowi *flp, int flags,
113 struct fib_lookup_arg *arg)
114 {
115 int err = -EAGAIN;
116 struct fib_table *tbl;
117 u32 tb_id;
118
119 switch (rule->action) {
120 case FR_ACT_TO_TBL:
121 break;
122
123 case FR_ACT_UNREACHABLE:
124 return -ENETUNREACH;
125
126 case FR_ACT_PROHIBIT:
127 return -EACCES;
128
129 case FR_ACT_BLACKHOLE:
130 default:
131 return -EINVAL;
132 }
133
134 rcu_read_lock();
135
136 tb_id = fib_rule_get_table(rule, arg);
137 tbl = fib_get_table(rule->fr_net, tb_id);
138 if (tbl)
139 err = fib_table_lookup(tbl, &flp->u.ip4,
140 (struct fib_result *)arg->result,
141 arg->flags);
142
143 rcu_read_unlock();
144 return err;
145 }
146
fib4_rule_suppress(struct fib_rule * rule,int flags,struct fib_lookup_arg * arg)147 INDIRECT_CALLABLE_SCOPE bool fib4_rule_suppress(struct fib_rule *rule,
148 int flags,
149 struct fib_lookup_arg *arg)
150 {
151 struct fib_result *result = arg->result;
152 struct net_device *dev = NULL;
153
154 if (result->fi) {
155 struct fib_nh_common *nhc = fib_info_nhc(result->fi, 0);
156
157 dev = nhc->nhc_dev;
158 }
159
160 /* do not accept result if the route does
161 * not meet the required prefix length
162 */
163 if (result->prefixlen <= rule->suppress_prefixlen)
164 goto suppress_route;
165
166 /* do not accept result if the route uses a device
167 * belonging to a forbidden interface group
168 */
169 if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
170 goto suppress_route;
171
172 return false;
173
174 suppress_route:
175 if (!(arg->flags & FIB_LOOKUP_NOREF))
176 fib_info_put(result->fi);
177 return true;
178 }
179
fib4_rule_match(struct fib_rule * rule,struct flowi * fl,int flags)180 INDIRECT_CALLABLE_SCOPE int fib4_rule_match(struct fib_rule *rule,
181 struct flowi *fl, int flags)
182 {
183 struct fib4_rule *r = (struct fib4_rule *) rule;
184 struct flowi4 *fl4 = &fl->u.ip4;
185 __be32 daddr = fl4->daddr;
186 __be32 saddr = fl4->saddr;
187
188 if (((saddr ^ r->src) & r->srcmask) ||
189 ((daddr ^ r->dst) & r->dstmask))
190 return 0;
191
192 /* When DSCP selector is used we need to match on the entire DSCP field
193 * in the flow information structure. When TOS selector is used we need
194 * to mask the upper three DSCP bits prior to matching to maintain
195 * legacy behavior.
196 */
197 if (r->dscp_full && (r->dscp ^ fl4->flowi4_dscp) & r->dscp_mask)
198 return 0;
199 else if (!r->dscp_full && r->dscp &&
200 !fib_dscp_masked_match(r->dscp, fl4))
201 return 0;
202
203 if (rule->ip_proto && (rule->ip_proto != fl4->flowi4_proto))
204 return 0;
205
206 if (!fib_rule_port_match(&rule->sport_range, rule->sport_mask,
207 fl4->fl4_sport))
208 return 0;
209
210 if (!fib_rule_port_match(&rule->dport_range, rule->dport_mask,
211 fl4->fl4_dport))
212 return 0;
213
214 return 1;
215 }
216
fib_empty_table(struct net * net)217 static struct fib_table *fib_empty_table(struct net *net)
218 {
219 u32 id = 1;
220
221 while (1) {
222 if (!fib_get_table(net, id))
223 return fib_new_table(net, id);
224
225 if (id++ == RT_TABLE_MAX)
226 break;
227 }
228 return NULL;
229 }
230
fib4_nl2rule_dscp(const struct nlattr * nla,struct fib4_rule * rule4,struct netlink_ext_ack * extack)231 static int fib4_nl2rule_dscp(const struct nlattr *nla, struct fib4_rule *rule4,
232 struct netlink_ext_ack *extack)
233 {
234 if (rule4->dscp) {
235 NL_SET_ERR_MSG(extack, "Cannot specify both TOS and DSCP");
236 return -EINVAL;
237 }
238
239 rule4->dscp = inet_dsfield_to_dscp(nla_get_u8(nla) << 2);
240 rule4->dscp_mask = inet_dsfield_to_dscp(INET_DSCP_MASK);
241 rule4->dscp_full = true;
242
243 return 0;
244 }
245
fib4_nl2rule_dscp_mask(const struct nlattr * nla,struct fib4_rule * rule4,struct netlink_ext_ack * extack)246 static int fib4_nl2rule_dscp_mask(const struct nlattr *nla,
247 struct fib4_rule *rule4,
248 struct netlink_ext_ack *extack)
249 {
250 dscp_t dscp_mask;
251
252 if (!rule4->dscp_full) {
253 NL_SET_ERR_MSG_ATTR(extack, nla,
254 "Cannot specify DSCP mask without DSCP value");
255 return -EINVAL;
256 }
257
258 dscp_mask = inet_dsfield_to_dscp(nla_get_u8(nla) << 2);
259 if (rule4->dscp & ~dscp_mask) {
260 NL_SET_ERR_MSG_ATTR(extack, nla, "Invalid DSCP mask");
261 return -EINVAL;
262 }
263
264 rule4->dscp_mask = dscp_mask;
265
266 return 0;
267 }
268
fib4_rule_configure(struct fib_rule * rule,struct sk_buff * skb,struct fib_rule_hdr * frh,struct nlattr ** tb,struct netlink_ext_ack * extack)269 static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
270 struct fib_rule_hdr *frh,
271 struct nlattr **tb,
272 struct netlink_ext_ack *extack)
273 {
274 struct fib4_rule *rule4 = (struct fib4_rule *)rule;
275 struct net *net = rule->fr_net;
276 int err = -EINVAL;
277
278 if (tb[FRA_FLOWLABEL] || tb[FRA_FLOWLABEL_MASK]) {
279 NL_SET_ERR_MSG(extack,
280 "Flow label cannot be specified for IPv4 FIB rules");
281 goto errout;
282 }
283
284 if (!inet_validate_dscp(frh->tos)) {
285 NL_SET_ERR_MSG(extack,
286 "Invalid dsfield (tos): ECN bits must be 0");
287 goto errout;
288 }
289 /* IPv4 currently doesn't handle high order DSCP bits correctly */
290 if (frh->tos & ~IPTOS_TOS_MASK) {
291 NL_SET_ERR_MSG(extack, "Invalid tos");
292 goto errout;
293 }
294 rule4->dscp = inet_dsfield_to_dscp(frh->tos);
295
296 if (tb[FRA_DSCP] &&
297 fib4_nl2rule_dscp(tb[FRA_DSCP], rule4, extack) < 0)
298 goto errout;
299
300 if (tb[FRA_DSCP_MASK] &&
301 fib4_nl2rule_dscp_mask(tb[FRA_DSCP_MASK], rule4, extack) < 0)
302 goto errout;
303
304 if (!net->ipv4.fib_has_custom_rules) {
305 /* split local/main if they are not already split */
306 err = fib_unmerge(net);
307 if (err)
308 goto errout;
309 }
310
311 if (rule->table == RT_TABLE_UNSPEC && !rule->l3mdev) {
312 if (rule->action == FR_ACT_TO_TBL) {
313 struct fib_table *table;
314
315 table = fib_empty_table(net);
316 if (!table) {
317 err = -ENOBUFS;
318 goto errout;
319 }
320
321 rule->table = table->tb_id;
322 }
323 }
324
325 if (frh->src_len)
326 rule4->src = nla_get_in_addr(tb[FRA_SRC]);
327
328 if (frh->dst_len)
329 rule4->dst = nla_get_in_addr(tb[FRA_DST]);
330
331 #ifdef CONFIG_IP_ROUTE_CLASSID
332 if (tb[FRA_FLOW]) {
333 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
334 if (rule4->tclassid)
335 atomic_inc(&net->ipv4.fib_num_tclassid_users);
336 }
337 #endif
338
339 if (fib_rule_requires_fldissect(rule))
340 net->ipv4.fib_rules_require_fldissect++;
341
342 rule4->src_len = frh->src_len;
343 rule4->srcmask = inet_make_mask(rule4->src_len);
344 rule4->dst_len = frh->dst_len;
345 rule4->dstmask = inet_make_mask(rule4->dst_len);
346
347 net->ipv4.fib_has_custom_rules = true;
348
349 err = 0;
350 errout:
351 return err;
352 }
353
fib4_rule_delete(struct fib_rule * rule)354 static void fib4_rule_delete(struct fib_rule *rule)
355 {
356 struct net *net = rule->fr_net;
357
358 #ifdef CONFIG_IP_ROUTE_CLASSID
359 if (((struct fib4_rule *)rule)->tclassid)
360 atomic_dec(&net->ipv4.fib_num_tclassid_users);
361 #endif
362
363 if (net->ipv4.fib_rules_require_fldissect &&
364 fib_rule_requires_fldissect(rule))
365 net->ipv4.fib_rules_require_fldissect--;
366 }
367
fib4_rule_compare(struct fib_rule * rule,struct fib_rule_hdr * frh,struct nlattr ** tb)368 static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
369 struct nlattr **tb)
370 {
371 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
372
373 if (frh->src_len && (rule4->src_len != frh->src_len))
374 return 0;
375
376 if (frh->dst_len && (rule4->dst_len != frh->dst_len))
377 return 0;
378
379 if (frh->tos &&
380 (rule4->dscp_full ||
381 inet_dscp_to_dsfield(rule4->dscp) != frh->tos))
382 return 0;
383
384 if (tb[FRA_DSCP]) {
385 dscp_t dscp;
386
387 dscp = inet_dsfield_to_dscp(nla_get_u8(tb[FRA_DSCP]) << 2);
388 if (!rule4->dscp_full || rule4->dscp != dscp)
389 return 0;
390 }
391
392 if (tb[FRA_DSCP_MASK]) {
393 dscp_t dscp_mask;
394
395 dscp_mask = inet_dsfield_to_dscp(nla_get_u8(tb[FRA_DSCP_MASK]) << 2);
396 if (!rule4->dscp_full || rule4->dscp_mask != dscp_mask)
397 return 0;
398 }
399
400 #ifdef CONFIG_IP_ROUTE_CLASSID
401 if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
402 return 0;
403 #endif
404
405 if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC])))
406 return 0;
407
408 if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST])))
409 return 0;
410
411 return 1;
412 }
413
fib4_rule_fill(struct fib_rule * rule,struct sk_buff * skb,struct fib_rule_hdr * frh)414 static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
415 struct fib_rule_hdr *frh)
416 {
417 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
418
419 frh->dst_len = rule4->dst_len;
420 frh->src_len = rule4->src_len;
421
422 if (rule4->dscp_full) {
423 frh->tos = 0;
424 if (nla_put_u8(skb, FRA_DSCP,
425 inet_dscp_to_dsfield(rule4->dscp) >> 2) ||
426 nla_put_u8(skb, FRA_DSCP_MASK,
427 inet_dscp_to_dsfield(rule4->dscp_mask) >> 2))
428 goto nla_put_failure;
429 } else {
430 frh->tos = inet_dscp_to_dsfield(rule4->dscp);
431 }
432
433 if ((rule4->dst_len &&
434 nla_put_in_addr(skb, FRA_DST, rule4->dst)) ||
435 (rule4->src_len &&
436 nla_put_in_addr(skb, FRA_SRC, rule4->src)))
437 goto nla_put_failure;
438 #ifdef CONFIG_IP_ROUTE_CLASSID
439 if (rule4->tclassid &&
440 nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
441 goto nla_put_failure;
442 #endif
443 return 0;
444
445 nla_put_failure:
446 return -ENOBUFS;
447 }
448
fib4_rule_nlmsg_payload(struct fib_rule * rule)449 static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
450 {
451 return nla_total_size(4) /* dst */
452 + nla_total_size(4) /* src */
453 + nla_total_size(4) /* flow */
454 + nla_total_size(1) /* dscp */
455 + nla_total_size(1); /* dscp mask */
456 }
457
fib4_rule_flush_cache(struct fib_rules_ops * ops)458 static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
459 {
460 rt_cache_flush(ops->fro_net);
461 }
462
fib4_rule_need_rtnl(struct net * net)463 static bool fib4_rule_need_rtnl(struct net *net)
464 {
465 return !net->ipv4.fib_has_custom_rules;
466 }
467
468 static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
469 .family = AF_INET,
470 .rule_size = sizeof(struct fib4_rule),
471 .addr_size = sizeof(u32),
472 .action = fib4_rule_action,
473 .suppress = fib4_rule_suppress,
474 .match = fib4_rule_match,
475 .configure = fib4_rule_configure,
476 .delete = fib4_rule_delete,
477 .compare = fib4_rule_compare,
478 .fill = fib4_rule_fill,
479 .nlmsg_payload = fib4_rule_nlmsg_payload,
480 .flush_cache = fib4_rule_flush_cache,
481 .need_rtnl = fib4_rule_need_rtnl,
482 .nlgroup = RTNLGRP_IPV4_RULE,
483 .owner = THIS_MODULE,
484 };
485
fib_default_rules_init(struct fib_rules_ops * ops)486 static int fib_default_rules_init(struct fib_rules_ops *ops)
487 {
488 int err;
489
490 err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL);
491 if (err < 0)
492 return err;
493 err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN);
494 if (err < 0)
495 return err;
496 err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT);
497 if (err < 0)
498 return err;
499 return 0;
500 }
501
fib4_rules_init(struct net * net)502 int __net_init fib4_rules_init(struct net *net)
503 {
504 int err;
505 struct fib_rules_ops *ops;
506
507 ops = fib_rules_register(&fib4_rules_ops_template, net);
508 if (IS_ERR(ops))
509 return PTR_ERR(ops);
510
511 err = fib_default_rules_init(ops);
512 if (err < 0)
513 goto fail;
514 net->ipv4.rules_ops = ops;
515 net->ipv4.fib_has_custom_rules = false;
516 net->ipv4.fib_rules_require_fldissect = 0;
517 return 0;
518
519 fail:
520 /* also cleans all rules already added */
521 fib_rules_unregister(ops);
522 return err;
523 }
524
fib4_rules_exit(struct net * net)525 void __net_exit fib4_rules_exit(struct net *net)
526 {
527 fib_rules_unregister(net->ipv4.rules_ops);
528 }
529