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