xref: /openbmc/linux/net/core/fib_rules.c (revision 547840bd)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * net/core/fib_rules.c		Generic Routing Rules
4  *
5  * Authors:	Thomas Graf <tgraf@suug.ch>
6  */
7 
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
13 #include <net/net_namespace.h>
14 #include <net/sock.h>
15 #include <net/fib_rules.h>
16 #include <net/ip_tunnels.h>
17 
18 static const struct fib_kuid_range fib_kuid_range_unset = {
19 	KUIDT_INIT(0),
20 	KUIDT_INIT(~0),
21 };
22 
23 bool fib_rule_matchall(const struct fib_rule *rule)
24 {
25 	if (rule->iifindex || rule->oifindex || rule->mark || rule->tun_id ||
26 	    rule->flags)
27 		return false;
28 	if (rule->suppress_ifgroup != -1 || rule->suppress_prefixlen != -1)
29 		return false;
30 	if (!uid_eq(rule->uid_range.start, fib_kuid_range_unset.start) ||
31 	    !uid_eq(rule->uid_range.end, fib_kuid_range_unset.end))
32 		return false;
33 	if (fib_rule_port_range_set(&rule->sport_range))
34 		return false;
35 	if (fib_rule_port_range_set(&rule->dport_range))
36 		return false;
37 	return true;
38 }
39 EXPORT_SYMBOL_GPL(fib_rule_matchall);
40 
41 int fib_default_rule_add(struct fib_rules_ops *ops,
42 			 u32 pref, u32 table, u32 flags)
43 {
44 	struct fib_rule *r;
45 
46 	r = kzalloc(ops->rule_size, GFP_KERNEL);
47 	if (r == NULL)
48 		return -ENOMEM;
49 
50 	refcount_set(&r->refcnt, 1);
51 	r->action = FR_ACT_TO_TBL;
52 	r->pref = pref;
53 	r->table = table;
54 	r->flags = flags;
55 	r->proto = RTPROT_KERNEL;
56 	r->fr_net = ops->fro_net;
57 	r->uid_range = fib_kuid_range_unset;
58 
59 	r->suppress_prefixlen = -1;
60 	r->suppress_ifgroup = -1;
61 
62 	/* The lock is not required here, the list in unreacheable
63 	 * at the moment this function is called */
64 	list_add_tail(&r->list, &ops->rules_list);
65 	return 0;
66 }
67 EXPORT_SYMBOL(fib_default_rule_add);
68 
69 static u32 fib_default_rule_pref(struct fib_rules_ops *ops)
70 {
71 	struct list_head *pos;
72 	struct fib_rule *rule;
73 
74 	if (!list_empty(&ops->rules_list)) {
75 		pos = ops->rules_list.next;
76 		if (pos->next != &ops->rules_list) {
77 			rule = list_entry(pos->next, struct fib_rule, list);
78 			if (rule->pref)
79 				return rule->pref - 1;
80 		}
81 	}
82 
83 	return 0;
84 }
85 
86 static void notify_rule_change(int event, struct fib_rule *rule,
87 			       struct fib_rules_ops *ops, struct nlmsghdr *nlh,
88 			       u32 pid);
89 
90 static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
91 {
92 	struct fib_rules_ops *ops;
93 
94 	rcu_read_lock();
95 	list_for_each_entry_rcu(ops, &net->rules_ops, list) {
96 		if (ops->family == family) {
97 			if (!try_module_get(ops->owner))
98 				ops = NULL;
99 			rcu_read_unlock();
100 			return ops;
101 		}
102 	}
103 	rcu_read_unlock();
104 
105 	return NULL;
106 }
107 
108 static void rules_ops_put(struct fib_rules_ops *ops)
109 {
110 	if (ops)
111 		module_put(ops->owner);
112 }
113 
114 static void flush_route_cache(struct fib_rules_ops *ops)
115 {
116 	if (ops->flush_cache)
117 		ops->flush_cache(ops);
118 }
119 
120 static int __fib_rules_register(struct fib_rules_ops *ops)
121 {
122 	int err = -EEXIST;
123 	struct fib_rules_ops *o;
124 	struct net *net;
125 
126 	net = ops->fro_net;
127 
128 	if (ops->rule_size < sizeof(struct fib_rule))
129 		return -EINVAL;
130 
131 	if (ops->match == NULL || ops->configure == NULL ||
132 	    ops->compare == NULL || ops->fill == NULL ||
133 	    ops->action == NULL)
134 		return -EINVAL;
135 
136 	spin_lock(&net->rules_mod_lock);
137 	list_for_each_entry(o, &net->rules_ops, list)
138 		if (ops->family == o->family)
139 			goto errout;
140 
141 	list_add_tail_rcu(&ops->list, &net->rules_ops);
142 	err = 0;
143 errout:
144 	spin_unlock(&net->rules_mod_lock);
145 
146 	return err;
147 }
148 
149 struct fib_rules_ops *
150 fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
151 {
152 	struct fib_rules_ops *ops;
153 	int err;
154 
155 	ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
156 	if (ops == NULL)
157 		return ERR_PTR(-ENOMEM);
158 
159 	INIT_LIST_HEAD(&ops->rules_list);
160 	ops->fro_net = net;
161 
162 	err = __fib_rules_register(ops);
163 	if (err) {
164 		kfree(ops);
165 		ops = ERR_PTR(err);
166 	}
167 
168 	return ops;
169 }
170 EXPORT_SYMBOL_GPL(fib_rules_register);
171 
172 static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
173 {
174 	struct fib_rule *rule, *tmp;
175 
176 	list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
177 		list_del_rcu(&rule->list);
178 		if (ops->delete)
179 			ops->delete(rule);
180 		fib_rule_put(rule);
181 	}
182 }
183 
184 void fib_rules_unregister(struct fib_rules_ops *ops)
185 {
186 	struct net *net = ops->fro_net;
187 
188 	spin_lock(&net->rules_mod_lock);
189 	list_del_rcu(&ops->list);
190 	spin_unlock(&net->rules_mod_lock);
191 
192 	fib_rules_cleanup_ops(ops);
193 	kfree_rcu(ops, rcu);
194 }
195 EXPORT_SYMBOL_GPL(fib_rules_unregister);
196 
197 static int uid_range_set(struct fib_kuid_range *range)
198 {
199 	return uid_valid(range->start) && uid_valid(range->end);
200 }
201 
202 static struct fib_kuid_range nla_get_kuid_range(struct nlattr **tb)
203 {
204 	struct fib_rule_uid_range *in;
205 	struct fib_kuid_range out;
206 
207 	in = (struct fib_rule_uid_range *)nla_data(tb[FRA_UID_RANGE]);
208 
209 	out.start = make_kuid(current_user_ns(), in->start);
210 	out.end = make_kuid(current_user_ns(), in->end);
211 
212 	return out;
213 }
214 
215 static int nla_put_uid_range(struct sk_buff *skb, struct fib_kuid_range *range)
216 {
217 	struct fib_rule_uid_range out = {
218 		from_kuid_munged(current_user_ns(), range->start),
219 		from_kuid_munged(current_user_ns(), range->end)
220 	};
221 
222 	return nla_put(skb, FRA_UID_RANGE, sizeof(out), &out);
223 }
224 
225 static int nla_get_port_range(struct nlattr *pattr,
226 			      struct fib_rule_port_range *port_range)
227 {
228 	const struct fib_rule_port_range *pr = nla_data(pattr);
229 
230 	if (!fib_rule_port_range_valid(pr))
231 		return -EINVAL;
232 
233 	port_range->start = pr->start;
234 	port_range->end = pr->end;
235 
236 	return 0;
237 }
238 
239 static int nla_put_port_range(struct sk_buff *skb, int attrtype,
240 			      struct fib_rule_port_range *range)
241 {
242 	return nla_put(skb, attrtype, sizeof(*range), range);
243 }
244 
245 static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
246 			  struct flowi *fl, int flags,
247 			  struct fib_lookup_arg *arg)
248 {
249 	int ret = 0;
250 
251 	if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
252 		goto out;
253 
254 	if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
255 		goto out;
256 
257 	if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
258 		goto out;
259 
260 	if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
261 		goto out;
262 
263 	if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg))
264 		goto out;
265 
266 	if (uid_lt(fl->flowi_uid, rule->uid_range.start) ||
267 	    uid_gt(fl->flowi_uid, rule->uid_range.end))
268 		goto out;
269 
270 	ret = ops->match(rule, fl, flags);
271 out:
272 	return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
273 }
274 
275 int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
276 		     int flags, struct fib_lookup_arg *arg)
277 {
278 	struct fib_rule *rule;
279 	int err;
280 
281 	rcu_read_lock();
282 
283 	list_for_each_entry_rcu(rule, &ops->rules_list, list) {
284 jumped:
285 		if (!fib_rule_match(rule, ops, fl, flags, arg))
286 			continue;
287 
288 		if (rule->action == FR_ACT_GOTO) {
289 			struct fib_rule *target;
290 
291 			target = rcu_dereference(rule->ctarget);
292 			if (target == NULL) {
293 				continue;
294 			} else {
295 				rule = target;
296 				goto jumped;
297 			}
298 		} else if (rule->action == FR_ACT_NOP)
299 			continue;
300 		else
301 			err = ops->action(rule, fl, flags, arg);
302 
303 		if (!err && ops->suppress && ops->suppress(rule, arg))
304 			continue;
305 
306 		if (err != -EAGAIN) {
307 			if ((arg->flags & FIB_LOOKUP_NOREF) ||
308 			    likely(refcount_inc_not_zero(&rule->refcnt))) {
309 				arg->rule = rule;
310 				goto out;
311 			}
312 			break;
313 		}
314 	}
315 
316 	err = -ESRCH;
317 out:
318 	rcu_read_unlock();
319 
320 	return err;
321 }
322 EXPORT_SYMBOL_GPL(fib_rules_lookup);
323 
324 static int call_fib_rule_notifier(struct notifier_block *nb,
325 				  enum fib_event_type event_type,
326 				  struct fib_rule *rule, int family,
327 				  struct netlink_ext_ack *extack)
328 {
329 	struct fib_rule_notifier_info info = {
330 		.info.family = family,
331 		.info.extack = extack,
332 		.rule = rule,
333 	};
334 
335 	return call_fib_notifier(nb, event_type, &info.info);
336 }
337 
338 static int call_fib_rule_notifiers(struct net *net,
339 				   enum fib_event_type event_type,
340 				   struct fib_rule *rule,
341 				   struct fib_rules_ops *ops,
342 				   struct netlink_ext_ack *extack)
343 {
344 	struct fib_rule_notifier_info info = {
345 		.info.family = ops->family,
346 		.info.extack = extack,
347 		.rule = rule,
348 	};
349 
350 	ops->fib_rules_seq++;
351 	return call_fib_notifiers(net, event_type, &info.info);
352 }
353 
354 /* Called with rcu_read_lock() */
355 int fib_rules_dump(struct net *net, struct notifier_block *nb, int family,
356 		   struct netlink_ext_ack *extack)
357 {
358 	struct fib_rules_ops *ops;
359 	struct fib_rule *rule;
360 	int err = 0;
361 
362 	ops = lookup_rules_ops(net, family);
363 	if (!ops)
364 		return -EAFNOSUPPORT;
365 	list_for_each_entry_rcu(rule, &ops->rules_list, list) {
366 		err = call_fib_rule_notifier(nb, FIB_EVENT_RULE_ADD,
367 					     rule, family, extack);
368 		if (err)
369 			break;
370 	}
371 	rules_ops_put(ops);
372 
373 	return err;
374 }
375 EXPORT_SYMBOL_GPL(fib_rules_dump);
376 
377 unsigned int fib_rules_seq_read(struct net *net, int family)
378 {
379 	unsigned int fib_rules_seq;
380 	struct fib_rules_ops *ops;
381 
382 	ASSERT_RTNL();
383 
384 	ops = lookup_rules_ops(net, family);
385 	if (!ops)
386 		return 0;
387 	fib_rules_seq = ops->fib_rules_seq;
388 	rules_ops_put(ops);
389 
390 	return fib_rules_seq;
391 }
392 EXPORT_SYMBOL_GPL(fib_rules_seq_read);
393 
394 static struct fib_rule *rule_find(struct fib_rules_ops *ops,
395 				  struct fib_rule_hdr *frh,
396 				  struct nlattr **tb,
397 				  struct fib_rule *rule,
398 				  bool user_priority)
399 {
400 	struct fib_rule *r;
401 
402 	list_for_each_entry(r, &ops->rules_list, list) {
403 		if (rule->action && r->action != rule->action)
404 			continue;
405 
406 		if (rule->table && r->table != rule->table)
407 			continue;
408 
409 		if (user_priority && r->pref != rule->pref)
410 			continue;
411 
412 		if (rule->iifname[0] &&
413 		    memcmp(r->iifname, rule->iifname, IFNAMSIZ))
414 			continue;
415 
416 		if (rule->oifname[0] &&
417 		    memcmp(r->oifname, rule->oifname, IFNAMSIZ))
418 			continue;
419 
420 		if (rule->mark && r->mark != rule->mark)
421 			continue;
422 
423 		if (rule->suppress_ifgroup != -1 &&
424 		    r->suppress_ifgroup != rule->suppress_ifgroup)
425 			continue;
426 
427 		if (rule->suppress_prefixlen != -1 &&
428 		    r->suppress_prefixlen != rule->suppress_prefixlen)
429 			continue;
430 
431 		if (rule->mark_mask && r->mark_mask != rule->mark_mask)
432 			continue;
433 
434 		if (rule->tun_id && r->tun_id != rule->tun_id)
435 			continue;
436 
437 		if (r->fr_net != rule->fr_net)
438 			continue;
439 
440 		if (rule->l3mdev && r->l3mdev != rule->l3mdev)
441 			continue;
442 
443 		if (uid_range_set(&rule->uid_range) &&
444 		    (!uid_eq(r->uid_range.start, rule->uid_range.start) ||
445 		    !uid_eq(r->uid_range.end, rule->uid_range.end)))
446 			continue;
447 
448 		if (rule->ip_proto && r->ip_proto != rule->ip_proto)
449 			continue;
450 
451 		if (rule->proto && r->proto != rule->proto)
452 			continue;
453 
454 		if (fib_rule_port_range_set(&rule->sport_range) &&
455 		    !fib_rule_port_range_compare(&r->sport_range,
456 						 &rule->sport_range))
457 			continue;
458 
459 		if (fib_rule_port_range_set(&rule->dport_range) &&
460 		    !fib_rule_port_range_compare(&r->dport_range,
461 						 &rule->dport_range))
462 			continue;
463 
464 		if (!ops->compare(r, frh, tb))
465 			continue;
466 		return r;
467 	}
468 
469 	return NULL;
470 }
471 
472 #ifdef CONFIG_NET_L3_MASTER_DEV
473 static int fib_nl2rule_l3mdev(struct nlattr *nla, struct fib_rule *nlrule,
474 			      struct netlink_ext_ack *extack)
475 {
476 	nlrule->l3mdev = nla_get_u8(nla);
477 	if (nlrule->l3mdev != 1) {
478 		NL_SET_ERR_MSG(extack, "Invalid l3mdev attribute");
479 		return -1;
480 	}
481 
482 	return 0;
483 }
484 #else
485 static int fib_nl2rule_l3mdev(struct nlattr *nla, struct fib_rule *nlrule,
486 			      struct netlink_ext_ack *extack)
487 {
488 	NL_SET_ERR_MSG(extack, "l3mdev support is not enabled in kernel");
489 	return -1;
490 }
491 #endif
492 
493 static int fib_nl2rule(struct sk_buff *skb, struct nlmsghdr *nlh,
494 		       struct netlink_ext_ack *extack,
495 		       struct fib_rules_ops *ops,
496 		       struct nlattr *tb[],
497 		       struct fib_rule **rule,
498 		       bool *user_priority)
499 {
500 	struct net *net = sock_net(skb->sk);
501 	struct fib_rule_hdr *frh = nlmsg_data(nlh);
502 	struct fib_rule *nlrule = NULL;
503 	int err = -EINVAL;
504 
505 	if (frh->src_len)
506 		if (!tb[FRA_SRC] ||
507 		    frh->src_len > (ops->addr_size * 8) ||
508 		    nla_len(tb[FRA_SRC]) != ops->addr_size) {
509 			NL_SET_ERR_MSG(extack, "Invalid source address");
510 			goto errout;
511 	}
512 
513 	if (frh->dst_len)
514 		if (!tb[FRA_DST] ||
515 		    frh->dst_len > (ops->addr_size * 8) ||
516 		    nla_len(tb[FRA_DST]) != ops->addr_size) {
517 			NL_SET_ERR_MSG(extack, "Invalid dst address");
518 			goto errout;
519 	}
520 
521 	nlrule = kzalloc(ops->rule_size, GFP_KERNEL);
522 	if (!nlrule) {
523 		err = -ENOMEM;
524 		goto errout;
525 	}
526 	refcount_set(&nlrule->refcnt, 1);
527 	nlrule->fr_net = net;
528 
529 	if (tb[FRA_PRIORITY]) {
530 		nlrule->pref = nla_get_u32(tb[FRA_PRIORITY]);
531 		*user_priority = true;
532 	} else {
533 		nlrule->pref = fib_default_rule_pref(ops);
534 	}
535 
536 	nlrule->proto = tb[FRA_PROTOCOL] ?
537 		nla_get_u8(tb[FRA_PROTOCOL]) : RTPROT_UNSPEC;
538 
539 	if (tb[FRA_IIFNAME]) {
540 		struct net_device *dev;
541 
542 		nlrule->iifindex = -1;
543 		nla_strlcpy(nlrule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
544 		dev = __dev_get_by_name(net, nlrule->iifname);
545 		if (dev)
546 			nlrule->iifindex = dev->ifindex;
547 	}
548 
549 	if (tb[FRA_OIFNAME]) {
550 		struct net_device *dev;
551 
552 		nlrule->oifindex = -1;
553 		nla_strlcpy(nlrule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
554 		dev = __dev_get_by_name(net, nlrule->oifname);
555 		if (dev)
556 			nlrule->oifindex = dev->ifindex;
557 	}
558 
559 	if (tb[FRA_FWMARK]) {
560 		nlrule->mark = nla_get_u32(tb[FRA_FWMARK]);
561 		if (nlrule->mark)
562 			/* compatibility: if the mark value is non-zero all bits
563 			 * are compared unless a mask is explicitly specified.
564 			 */
565 			nlrule->mark_mask = 0xFFFFFFFF;
566 	}
567 
568 	if (tb[FRA_FWMASK])
569 		nlrule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
570 
571 	if (tb[FRA_TUN_ID])
572 		nlrule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
573 
574 	err = -EINVAL;
575 	if (tb[FRA_L3MDEV] &&
576 	    fib_nl2rule_l3mdev(tb[FRA_L3MDEV], nlrule, extack) < 0)
577 		goto errout_free;
578 
579 	nlrule->action = frh->action;
580 	nlrule->flags = frh->flags;
581 	nlrule->table = frh_get_table(frh, tb);
582 	if (tb[FRA_SUPPRESS_PREFIXLEN])
583 		nlrule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
584 	else
585 		nlrule->suppress_prefixlen = -1;
586 
587 	if (tb[FRA_SUPPRESS_IFGROUP])
588 		nlrule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
589 	else
590 		nlrule->suppress_ifgroup = -1;
591 
592 	if (tb[FRA_GOTO]) {
593 		if (nlrule->action != FR_ACT_GOTO) {
594 			NL_SET_ERR_MSG(extack, "Unexpected goto");
595 			goto errout_free;
596 		}
597 
598 		nlrule->target = nla_get_u32(tb[FRA_GOTO]);
599 		/* Backward jumps are prohibited to avoid endless loops */
600 		if (nlrule->target <= nlrule->pref) {
601 			NL_SET_ERR_MSG(extack, "Backward goto not supported");
602 			goto errout_free;
603 		}
604 	} else if (nlrule->action == FR_ACT_GOTO) {
605 		NL_SET_ERR_MSG(extack, "Missing goto target for action goto");
606 		goto errout_free;
607 	}
608 
609 	if (nlrule->l3mdev && nlrule->table) {
610 		NL_SET_ERR_MSG(extack, "l3mdev and table are mutually exclusive");
611 		goto errout_free;
612 	}
613 
614 	if (tb[FRA_UID_RANGE]) {
615 		if (current_user_ns() != net->user_ns) {
616 			err = -EPERM;
617 			NL_SET_ERR_MSG(extack, "No permission to set uid");
618 			goto errout_free;
619 		}
620 
621 		nlrule->uid_range = nla_get_kuid_range(tb);
622 
623 		if (!uid_range_set(&nlrule->uid_range) ||
624 		    !uid_lte(nlrule->uid_range.start, nlrule->uid_range.end)) {
625 			NL_SET_ERR_MSG(extack, "Invalid uid range");
626 			goto errout_free;
627 		}
628 	} else {
629 		nlrule->uid_range = fib_kuid_range_unset;
630 	}
631 
632 	if (tb[FRA_IP_PROTO])
633 		nlrule->ip_proto = nla_get_u8(tb[FRA_IP_PROTO]);
634 
635 	if (tb[FRA_SPORT_RANGE]) {
636 		err = nla_get_port_range(tb[FRA_SPORT_RANGE],
637 					 &nlrule->sport_range);
638 		if (err) {
639 			NL_SET_ERR_MSG(extack, "Invalid sport range");
640 			goto errout_free;
641 		}
642 	}
643 
644 	if (tb[FRA_DPORT_RANGE]) {
645 		err = nla_get_port_range(tb[FRA_DPORT_RANGE],
646 					 &nlrule->dport_range);
647 		if (err) {
648 			NL_SET_ERR_MSG(extack, "Invalid dport range");
649 			goto errout_free;
650 		}
651 	}
652 
653 	*rule = nlrule;
654 
655 	return 0;
656 
657 errout_free:
658 	kfree(nlrule);
659 errout:
660 	return err;
661 }
662 
663 static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh,
664 		       struct nlattr **tb, struct fib_rule *rule)
665 {
666 	struct fib_rule *r;
667 
668 	list_for_each_entry(r, &ops->rules_list, list) {
669 		if (r->action != rule->action)
670 			continue;
671 
672 		if (r->table != rule->table)
673 			continue;
674 
675 		if (r->pref != rule->pref)
676 			continue;
677 
678 		if (memcmp(r->iifname, rule->iifname, IFNAMSIZ))
679 			continue;
680 
681 		if (memcmp(r->oifname, rule->oifname, IFNAMSIZ))
682 			continue;
683 
684 		if (r->mark != rule->mark)
685 			continue;
686 
687 		if (r->suppress_ifgroup != rule->suppress_ifgroup)
688 			continue;
689 
690 		if (r->suppress_prefixlen != rule->suppress_prefixlen)
691 			continue;
692 
693 		if (r->mark_mask != rule->mark_mask)
694 			continue;
695 
696 		if (r->tun_id != rule->tun_id)
697 			continue;
698 
699 		if (r->fr_net != rule->fr_net)
700 			continue;
701 
702 		if (r->l3mdev != rule->l3mdev)
703 			continue;
704 
705 		if (!uid_eq(r->uid_range.start, rule->uid_range.start) ||
706 		    !uid_eq(r->uid_range.end, rule->uid_range.end))
707 			continue;
708 
709 		if (r->ip_proto != rule->ip_proto)
710 			continue;
711 
712 		if (r->proto != rule->proto)
713 			continue;
714 
715 		if (!fib_rule_port_range_compare(&r->sport_range,
716 						 &rule->sport_range))
717 			continue;
718 
719 		if (!fib_rule_port_range_compare(&r->dport_range,
720 						 &rule->dport_range))
721 			continue;
722 
723 		if (!ops->compare(r, frh, tb))
724 			continue;
725 		return 1;
726 	}
727 	return 0;
728 }
729 
730 int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
731 		   struct netlink_ext_ack *extack)
732 {
733 	struct net *net = sock_net(skb->sk);
734 	struct fib_rule_hdr *frh = nlmsg_data(nlh);
735 	struct fib_rules_ops *ops = NULL;
736 	struct fib_rule *rule = NULL, *r, *last = NULL;
737 	struct nlattr *tb[FRA_MAX + 1];
738 	int err = -EINVAL, unresolved = 0;
739 	bool user_priority = false;
740 
741 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
742 		NL_SET_ERR_MSG(extack, "Invalid msg length");
743 		goto errout;
744 	}
745 
746 	ops = lookup_rules_ops(net, frh->family);
747 	if (!ops) {
748 		err = -EAFNOSUPPORT;
749 		NL_SET_ERR_MSG(extack, "Rule family not supported");
750 		goto errout;
751 	}
752 
753 	err = nlmsg_parse_deprecated(nlh, sizeof(*frh), tb, FRA_MAX,
754 				     ops->policy, extack);
755 	if (err < 0) {
756 		NL_SET_ERR_MSG(extack, "Error parsing msg");
757 		goto errout;
758 	}
759 
760 	err = fib_nl2rule(skb, nlh, extack, ops, tb, &rule, &user_priority);
761 	if (err)
762 		goto errout;
763 
764 	if ((nlh->nlmsg_flags & NLM_F_EXCL) &&
765 	    rule_exists(ops, frh, tb, rule)) {
766 		err = -EEXIST;
767 		goto errout_free;
768 	}
769 
770 	err = ops->configure(rule, skb, frh, tb, extack);
771 	if (err < 0)
772 		goto errout_free;
773 
774 	err = call_fib_rule_notifiers(net, FIB_EVENT_RULE_ADD, rule, ops,
775 				      extack);
776 	if (err < 0)
777 		goto errout_free;
778 
779 	list_for_each_entry(r, &ops->rules_list, list) {
780 		if (r->pref == rule->target) {
781 			RCU_INIT_POINTER(rule->ctarget, r);
782 			break;
783 		}
784 	}
785 
786 	if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
787 		unresolved = 1;
788 
789 	list_for_each_entry(r, &ops->rules_list, list) {
790 		if (r->pref > rule->pref)
791 			break;
792 		last = r;
793 	}
794 
795 	if (last)
796 		list_add_rcu(&rule->list, &last->list);
797 	else
798 		list_add_rcu(&rule->list, &ops->rules_list);
799 
800 	if (ops->unresolved_rules) {
801 		/*
802 		 * There are unresolved goto rules in the list, check if
803 		 * any of them are pointing to this new rule.
804 		 */
805 		list_for_each_entry(r, &ops->rules_list, list) {
806 			if (r->action == FR_ACT_GOTO &&
807 			    r->target == rule->pref &&
808 			    rtnl_dereference(r->ctarget) == NULL) {
809 				rcu_assign_pointer(r->ctarget, rule);
810 				if (--ops->unresolved_rules == 0)
811 					break;
812 			}
813 		}
814 	}
815 
816 	if (rule->action == FR_ACT_GOTO)
817 		ops->nr_goto_rules++;
818 
819 	if (unresolved)
820 		ops->unresolved_rules++;
821 
822 	if (rule->tun_id)
823 		ip_tunnel_need_metadata();
824 
825 	notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
826 	flush_route_cache(ops);
827 	rules_ops_put(ops);
828 	return 0;
829 
830 errout_free:
831 	kfree(rule);
832 errout:
833 	rules_ops_put(ops);
834 	return err;
835 }
836 EXPORT_SYMBOL_GPL(fib_nl_newrule);
837 
838 int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
839 		   struct netlink_ext_ack *extack)
840 {
841 	struct net *net = sock_net(skb->sk);
842 	struct fib_rule_hdr *frh = nlmsg_data(nlh);
843 	struct fib_rules_ops *ops = NULL;
844 	struct fib_rule *rule = NULL, *r, *nlrule = NULL;
845 	struct nlattr *tb[FRA_MAX+1];
846 	int err = -EINVAL;
847 	bool user_priority = false;
848 
849 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
850 		NL_SET_ERR_MSG(extack, "Invalid msg length");
851 		goto errout;
852 	}
853 
854 	ops = lookup_rules_ops(net, frh->family);
855 	if (ops == NULL) {
856 		err = -EAFNOSUPPORT;
857 		NL_SET_ERR_MSG(extack, "Rule family not supported");
858 		goto errout;
859 	}
860 
861 	err = nlmsg_parse_deprecated(nlh, sizeof(*frh), tb, FRA_MAX,
862 				     ops->policy, extack);
863 	if (err < 0) {
864 		NL_SET_ERR_MSG(extack, "Error parsing msg");
865 		goto errout;
866 	}
867 
868 	err = fib_nl2rule(skb, nlh, extack, ops, tb, &nlrule, &user_priority);
869 	if (err)
870 		goto errout;
871 
872 	rule = rule_find(ops, frh, tb, nlrule, user_priority);
873 	if (!rule) {
874 		err = -ENOENT;
875 		goto errout;
876 	}
877 
878 	if (rule->flags & FIB_RULE_PERMANENT) {
879 		err = -EPERM;
880 		goto errout;
881 	}
882 
883 	if (ops->delete) {
884 		err = ops->delete(rule);
885 		if (err)
886 			goto errout;
887 	}
888 
889 	if (rule->tun_id)
890 		ip_tunnel_unneed_metadata();
891 
892 	list_del_rcu(&rule->list);
893 
894 	if (rule->action == FR_ACT_GOTO) {
895 		ops->nr_goto_rules--;
896 		if (rtnl_dereference(rule->ctarget) == NULL)
897 			ops->unresolved_rules--;
898 	}
899 
900 	/*
901 	 * Check if this rule is a target to any of them. If so,
902 	 * adjust to the next one with the same preference or
903 	 * disable them. As this operation is eventually very
904 	 * expensive, it is only performed if goto rules, except
905 	 * current if it is goto rule, have actually been added.
906 	 */
907 	if (ops->nr_goto_rules > 0) {
908 		struct fib_rule *n;
909 
910 		n = list_next_entry(rule, list);
911 		if (&n->list == &ops->rules_list || n->pref != rule->pref)
912 			n = NULL;
913 		list_for_each_entry(r, &ops->rules_list, list) {
914 			if (rtnl_dereference(r->ctarget) != rule)
915 				continue;
916 			rcu_assign_pointer(r->ctarget, n);
917 			if (!n)
918 				ops->unresolved_rules++;
919 		}
920 	}
921 
922 	call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule, ops,
923 				NULL);
924 	notify_rule_change(RTM_DELRULE, rule, ops, nlh,
925 			   NETLINK_CB(skb).portid);
926 	fib_rule_put(rule);
927 	flush_route_cache(ops);
928 	rules_ops_put(ops);
929 	kfree(nlrule);
930 	return 0;
931 
932 errout:
933 	kfree(nlrule);
934 	rules_ops_put(ops);
935 	return err;
936 }
937 EXPORT_SYMBOL_GPL(fib_nl_delrule);
938 
939 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
940 					 struct fib_rule *rule)
941 {
942 	size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
943 			 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
944 			 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
945 			 + nla_total_size(4) /* FRA_PRIORITY */
946 			 + nla_total_size(4) /* FRA_TABLE */
947 			 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
948 			 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
949 			 + nla_total_size(4) /* FRA_FWMARK */
950 			 + nla_total_size(4) /* FRA_FWMASK */
951 			 + nla_total_size_64bit(8) /* FRA_TUN_ID */
952 			 + nla_total_size(sizeof(struct fib_kuid_range))
953 			 + nla_total_size(1) /* FRA_PROTOCOL */
954 			 + nla_total_size(1) /* FRA_IP_PROTO */
955 			 + nla_total_size(sizeof(struct fib_rule_port_range)) /* FRA_SPORT_RANGE */
956 			 + nla_total_size(sizeof(struct fib_rule_port_range)); /* FRA_DPORT_RANGE */
957 
958 	if (ops->nlmsg_payload)
959 		payload += ops->nlmsg_payload(rule);
960 
961 	return payload;
962 }
963 
964 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
965 			    u32 pid, u32 seq, int type, int flags,
966 			    struct fib_rules_ops *ops)
967 {
968 	struct nlmsghdr *nlh;
969 	struct fib_rule_hdr *frh;
970 
971 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
972 	if (nlh == NULL)
973 		return -EMSGSIZE;
974 
975 	frh = nlmsg_data(nlh);
976 	frh->family = ops->family;
977 	frh->table = rule->table < 256 ? rule->table : RT_TABLE_COMPAT;
978 	if (nla_put_u32(skb, FRA_TABLE, rule->table))
979 		goto nla_put_failure;
980 	if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
981 		goto nla_put_failure;
982 	frh->res1 = 0;
983 	frh->res2 = 0;
984 	frh->action = rule->action;
985 	frh->flags = rule->flags;
986 
987 	if (nla_put_u8(skb, FRA_PROTOCOL, rule->proto))
988 		goto nla_put_failure;
989 
990 	if (rule->action == FR_ACT_GOTO &&
991 	    rcu_access_pointer(rule->ctarget) == NULL)
992 		frh->flags |= FIB_RULE_UNRESOLVED;
993 
994 	if (rule->iifname[0]) {
995 		if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
996 			goto nla_put_failure;
997 		if (rule->iifindex == -1)
998 			frh->flags |= FIB_RULE_IIF_DETACHED;
999 	}
1000 
1001 	if (rule->oifname[0]) {
1002 		if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
1003 			goto nla_put_failure;
1004 		if (rule->oifindex == -1)
1005 			frh->flags |= FIB_RULE_OIF_DETACHED;
1006 	}
1007 
1008 	if ((rule->pref &&
1009 	     nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
1010 	    (rule->mark &&
1011 	     nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
1012 	    ((rule->mark_mask || rule->mark) &&
1013 	     nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
1014 	    (rule->target &&
1015 	     nla_put_u32(skb, FRA_GOTO, rule->target)) ||
1016 	    (rule->tun_id &&
1017 	     nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
1018 	    (rule->l3mdev &&
1019 	     nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)) ||
1020 	    (uid_range_set(&rule->uid_range) &&
1021 	     nla_put_uid_range(skb, &rule->uid_range)) ||
1022 	    (fib_rule_port_range_set(&rule->sport_range) &&
1023 	     nla_put_port_range(skb, FRA_SPORT_RANGE, &rule->sport_range)) ||
1024 	    (fib_rule_port_range_set(&rule->dport_range) &&
1025 	     nla_put_port_range(skb, FRA_DPORT_RANGE, &rule->dport_range)) ||
1026 	    (rule->ip_proto && nla_put_u8(skb, FRA_IP_PROTO, rule->ip_proto)))
1027 		goto nla_put_failure;
1028 
1029 	if (rule->suppress_ifgroup != -1) {
1030 		if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
1031 			goto nla_put_failure;
1032 	}
1033 
1034 	if (ops->fill(rule, skb, frh) < 0)
1035 		goto nla_put_failure;
1036 
1037 	nlmsg_end(skb, nlh);
1038 	return 0;
1039 
1040 nla_put_failure:
1041 	nlmsg_cancel(skb, nlh);
1042 	return -EMSGSIZE;
1043 }
1044 
1045 static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
1046 		      struct fib_rules_ops *ops)
1047 {
1048 	int idx = 0;
1049 	struct fib_rule *rule;
1050 	int err = 0;
1051 
1052 	rcu_read_lock();
1053 	list_for_each_entry_rcu(rule, &ops->rules_list, list) {
1054 		if (idx < cb->args[1])
1055 			goto skip;
1056 
1057 		err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
1058 				       cb->nlh->nlmsg_seq, RTM_NEWRULE,
1059 				       NLM_F_MULTI, ops);
1060 		if (err)
1061 			break;
1062 skip:
1063 		idx++;
1064 	}
1065 	rcu_read_unlock();
1066 	cb->args[1] = idx;
1067 	rules_ops_put(ops);
1068 
1069 	return err;
1070 }
1071 
1072 static int fib_valid_dumprule_req(const struct nlmsghdr *nlh,
1073 				   struct netlink_ext_ack *extack)
1074 {
1075 	struct fib_rule_hdr *frh;
1076 
1077 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
1078 		NL_SET_ERR_MSG(extack, "Invalid header for fib rule dump request");
1079 		return -EINVAL;
1080 	}
1081 
1082 	frh = nlmsg_data(nlh);
1083 	if (frh->dst_len || frh->src_len || frh->tos || frh->table ||
1084 	    frh->res1 || frh->res2 || frh->action || frh->flags) {
1085 		NL_SET_ERR_MSG(extack,
1086 			       "Invalid values in header for fib rule dump request");
1087 		return -EINVAL;
1088 	}
1089 
1090 	if (nlmsg_attrlen(nlh, sizeof(*frh))) {
1091 		NL_SET_ERR_MSG(extack, "Invalid data after header in fib rule dump request");
1092 		return -EINVAL;
1093 	}
1094 
1095 	return 0;
1096 }
1097 
1098 static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
1099 {
1100 	const struct nlmsghdr *nlh = cb->nlh;
1101 	struct net *net = sock_net(skb->sk);
1102 	struct fib_rules_ops *ops;
1103 	int idx = 0, family;
1104 
1105 	if (cb->strict_check) {
1106 		int err = fib_valid_dumprule_req(nlh, cb->extack);
1107 
1108 		if (err < 0)
1109 			return err;
1110 	}
1111 
1112 	family = rtnl_msg_family(nlh);
1113 	if (family != AF_UNSPEC) {
1114 		/* Protocol specific dump request */
1115 		ops = lookup_rules_ops(net, family);
1116 		if (ops == NULL)
1117 			return -EAFNOSUPPORT;
1118 
1119 		dump_rules(skb, cb, ops);
1120 
1121 		return skb->len;
1122 	}
1123 
1124 	rcu_read_lock();
1125 	list_for_each_entry_rcu(ops, &net->rules_ops, list) {
1126 		if (idx < cb->args[0] || !try_module_get(ops->owner))
1127 			goto skip;
1128 
1129 		if (dump_rules(skb, cb, ops) < 0)
1130 			break;
1131 
1132 		cb->args[1] = 0;
1133 skip:
1134 		idx++;
1135 	}
1136 	rcu_read_unlock();
1137 	cb->args[0] = idx;
1138 
1139 	return skb->len;
1140 }
1141 
1142 static void notify_rule_change(int event, struct fib_rule *rule,
1143 			       struct fib_rules_ops *ops, struct nlmsghdr *nlh,
1144 			       u32 pid)
1145 {
1146 	struct net *net;
1147 	struct sk_buff *skb;
1148 	int err = -ENOBUFS;
1149 
1150 	net = ops->fro_net;
1151 	skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
1152 	if (skb == NULL)
1153 		goto errout;
1154 
1155 	err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
1156 	if (err < 0) {
1157 		/* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
1158 		WARN_ON(err == -EMSGSIZE);
1159 		kfree_skb(skb);
1160 		goto errout;
1161 	}
1162 
1163 	rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
1164 	return;
1165 errout:
1166 	if (err < 0)
1167 		rtnl_set_sk_err(net, ops->nlgroup, err);
1168 }
1169 
1170 static void attach_rules(struct list_head *rules, struct net_device *dev)
1171 {
1172 	struct fib_rule *rule;
1173 
1174 	list_for_each_entry(rule, rules, list) {
1175 		if (rule->iifindex == -1 &&
1176 		    strcmp(dev->name, rule->iifname) == 0)
1177 			rule->iifindex = dev->ifindex;
1178 		if (rule->oifindex == -1 &&
1179 		    strcmp(dev->name, rule->oifname) == 0)
1180 			rule->oifindex = dev->ifindex;
1181 	}
1182 }
1183 
1184 static void detach_rules(struct list_head *rules, struct net_device *dev)
1185 {
1186 	struct fib_rule *rule;
1187 
1188 	list_for_each_entry(rule, rules, list) {
1189 		if (rule->iifindex == dev->ifindex)
1190 			rule->iifindex = -1;
1191 		if (rule->oifindex == dev->ifindex)
1192 			rule->oifindex = -1;
1193 	}
1194 }
1195 
1196 
1197 static int fib_rules_event(struct notifier_block *this, unsigned long event,
1198 			   void *ptr)
1199 {
1200 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1201 	struct net *net = dev_net(dev);
1202 	struct fib_rules_ops *ops;
1203 
1204 	ASSERT_RTNL();
1205 
1206 	switch (event) {
1207 	case NETDEV_REGISTER:
1208 		list_for_each_entry(ops, &net->rules_ops, list)
1209 			attach_rules(&ops->rules_list, dev);
1210 		break;
1211 
1212 	case NETDEV_CHANGENAME:
1213 		list_for_each_entry(ops, &net->rules_ops, list) {
1214 			detach_rules(&ops->rules_list, dev);
1215 			attach_rules(&ops->rules_list, dev);
1216 		}
1217 		break;
1218 
1219 	case NETDEV_UNREGISTER:
1220 		list_for_each_entry(ops, &net->rules_ops, list)
1221 			detach_rules(&ops->rules_list, dev);
1222 		break;
1223 	}
1224 
1225 	return NOTIFY_DONE;
1226 }
1227 
1228 static struct notifier_block fib_rules_notifier = {
1229 	.notifier_call = fib_rules_event,
1230 };
1231 
1232 static int __net_init fib_rules_net_init(struct net *net)
1233 {
1234 	INIT_LIST_HEAD(&net->rules_ops);
1235 	spin_lock_init(&net->rules_mod_lock);
1236 	return 0;
1237 }
1238 
1239 static void __net_exit fib_rules_net_exit(struct net *net)
1240 {
1241 	WARN_ON_ONCE(!list_empty(&net->rules_ops));
1242 }
1243 
1244 static struct pernet_operations fib_rules_net_ops = {
1245 	.init = fib_rules_net_init,
1246 	.exit = fib_rules_net_exit,
1247 };
1248 
1249 static int __init fib_rules_init(void)
1250 {
1251 	int err;
1252 	rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, 0);
1253 	rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, 0);
1254 	rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, 0);
1255 
1256 	err = register_pernet_subsys(&fib_rules_net_ops);
1257 	if (err < 0)
1258 		goto fail;
1259 
1260 	err = register_netdevice_notifier(&fib_rules_notifier);
1261 	if (err < 0)
1262 		goto fail_unregister;
1263 
1264 	return 0;
1265 
1266 fail_unregister:
1267 	unregister_pernet_subsys(&fib_rules_net_ops);
1268 fail:
1269 	rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
1270 	rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
1271 	rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
1272 	return err;
1273 }
1274 
1275 subsys_initcall(fib_rules_init);
1276