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