1 /*
2  * net/core/fib_rules.c		Generic Routing Rules
3  *
4  *	This program is free software; you can redistribute it and/or
5  *	modify it under the terms of the GNU General Public License as
6  *	published by the Free Software Foundation, version 2.
7  *
8  * Authors:	Thomas Graf <tgraf@suug.ch>
9  */
10 
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <net/net_namespace.h>
17 #include <net/sock.h>
18 #include <net/fib_rules.h>
19 
fib_default_rule_add(struct fib_rules_ops * ops,u32 pref,u32 table,u32 flags)20 int fib_default_rule_add(struct fib_rules_ops *ops,
21 			 u32 pref, u32 table, u32 flags)
22 {
23 	struct fib_rule *r;
24 
25 	r = kzalloc(ops->rule_size, GFP_KERNEL);
26 	if (r == NULL)
27 		return -ENOMEM;
28 
29 	atomic_set(&r->refcnt, 1);
30 	r->action = FR_ACT_TO_TBL;
31 	r->pref = pref;
32 	r->table = table;
33 	r->flags = flags;
34 	r->fr_net = hold_net(ops->fro_net);
35 
36 	/* The lock is not required here, the list in unreacheable
37 	 * at the moment this function is called */
38 	list_add_tail(&r->list, &ops->rules_list);
39 	return 0;
40 }
41 EXPORT_SYMBOL(fib_default_rule_add);
42 
fib_default_rule_pref(struct fib_rules_ops * ops)43 u32 fib_default_rule_pref(struct fib_rules_ops *ops)
44 {
45 	struct list_head *pos;
46 	struct fib_rule *rule;
47 
48 	if (!list_empty(&ops->rules_list)) {
49 		pos = ops->rules_list.next;
50 		if (pos->next != &ops->rules_list) {
51 			rule = list_entry(pos->next, struct fib_rule, list);
52 			if (rule->pref)
53 				return rule->pref - 1;
54 		}
55 	}
56 
57 	return 0;
58 }
59 EXPORT_SYMBOL(fib_default_rule_pref);
60 
61 static void notify_rule_change(int event, struct fib_rule *rule,
62 			       struct fib_rules_ops *ops, struct nlmsghdr *nlh,
63 			       u32 pid);
64 
lookup_rules_ops(struct net * net,int family)65 static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
66 {
67 	struct fib_rules_ops *ops;
68 
69 	rcu_read_lock();
70 	list_for_each_entry_rcu(ops, &net->rules_ops, list) {
71 		if (ops->family == family) {
72 			if (!try_module_get(ops->owner))
73 				ops = NULL;
74 			rcu_read_unlock();
75 			return ops;
76 		}
77 	}
78 	rcu_read_unlock();
79 
80 	return NULL;
81 }
82 
rules_ops_put(struct fib_rules_ops * ops)83 static void rules_ops_put(struct fib_rules_ops *ops)
84 {
85 	if (ops)
86 		module_put(ops->owner);
87 }
88 
flush_route_cache(struct fib_rules_ops * ops)89 static void flush_route_cache(struct fib_rules_ops *ops)
90 {
91 	if (ops->flush_cache)
92 		ops->flush_cache(ops);
93 }
94 
__fib_rules_register(struct fib_rules_ops * ops)95 static int __fib_rules_register(struct fib_rules_ops *ops)
96 {
97 	int err = -EEXIST;
98 	struct fib_rules_ops *o;
99 	struct net *net;
100 
101 	net = ops->fro_net;
102 
103 	if (ops->rule_size < sizeof(struct fib_rule))
104 		return -EINVAL;
105 
106 	if (ops->match == NULL || ops->configure == NULL ||
107 	    ops->compare == NULL || ops->fill == NULL ||
108 	    ops->action == NULL)
109 		return -EINVAL;
110 
111 	spin_lock(&net->rules_mod_lock);
112 	list_for_each_entry(o, &net->rules_ops, list)
113 		if (ops->family == o->family)
114 			goto errout;
115 
116 	hold_net(net);
117 	list_add_tail_rcu(&ops->list, &net->rules_ops);
118 	err = 0;
119 errout:
120 	spin_unlock(&net->rules_mod_lock);
121 
122 	return err;
123 }
124 
125 struct fib_rules_ops *
fib_rules_register(const struct fib_rules_ops * tmpl,struct net * net)126 fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
127 {
128 	struct fib_rules_ops *ops;
129 	int err;
130 
131 	ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
132 	if (ops == NULL)
133 		return ERR_PTR(-ENOMEM);
134 
135 	INIT_LIST_HEAD(&ops->rules_list);
136 	ops->fro_net = net;
137 
138 	err = __fib_rules_register(ops);
139 	if (err) {
140 		kfree(ops);
141 		ops = ERR_PTR(err);
142 	}
143 
144 	return ops;
145 }
146 EXPORT_SYMBOL_GPL(fib_rules_register);
147 
fib_rules_cleanup_ops(struct fib_rules_ops * ops)148 static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
149 {
150 	struct fib_rule *rule, *tmp;
151 
152 	list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
153 		list_del_rcu(&rule->list);
154 		fib_rule_put(rule);
155 	}
156 }
157 
fib_rules_put_rcu(struct rcu_head * head)158 static void fib_rules_put_rcu(struct rcu_head *head)
159 {
160 	struct fib_rules_ops *ops = container_of(head, struct fib_rules_ops, rcu);
161 	struct net *net = ops->fro_net;
162 
163 	release_net(net);
164 	kfree(ops);
165 }
166 
fib_rules_unregister(struct fib_rules_ops * ops)167 void fib_rules_unregister(struct fib_rules_ops *ops)
168 {
169 	struct net *net = ops->fro_net;
170 
171 	spin_lock(&net->rules_mod_lock);
172 	list_del_rcu(&ops->list);
173 	fib_rules_cleanup_ops(ops);
174 	spin_unlock(&net->rules_mod_lock);
175 
176 	call_rcu(&ops->rcu, fib_rules_put_rcu);
177 }
178 EXPORT_SYMBOL_GPL(fib_rules_unregister);
179 
fib_rule_match(struct fib_rule * rule,struct fib_rules_ops * ops,struct flowi * fl,int flags)180 static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
181 			  struct flowi *fl, int flags)
182 {
183 	int ret = 0;
184 
185 	if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
186 		goto out;
187 
188 	if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
189 		goto out;
190 
191 	if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
192 		goto out;
193 
194 	ret = ops->match(rule, fl, flags);
195 out:
196 	return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
197 }
198 
fib_rules_lookup(struct fib_rules_ops * ops,struct flowi * fl,int flags,struct fib_lookup_arg * arg)199 int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
200 		     int flags, struct fib_lookup_arg *arg)
201 {
202 	struct fib_rule *rule;
203 	int err;
204 
205 	rcu_read_lock();
206 
207 	list_for_each_entry_rcu(rule, &ops->rules_list, list) {
208 jumped:
209 		if (!fib_rule_match(rule, ops, fl, flags))
210 			continue;
211 
212 		if (rule->action == FR_ACT_GOTO) {
213 			struct fib_rule *target;
214 
215 			target = rcu_dereference(rule->ctarget);
216 			if (target == NULL) {
217 				continue;
218 			} else {
219 				rule = target;
220 				goto jumped;
221 			}
222 		} else if (rule->action == FR_ACT_NOP)
223 			continue;
224 		else
225 			err = ops->action(rule, fl, flags, arg);
226 
227 		if (err != -EAGAIN) {
228 			if ((arg->flags & FIB_LOOKUP_NOREF) ||
229 			    likely(atomic_inc_not_zero(&rule->refcnt))) {
230 				arg->rule = rule;
231 				goto out;
232 			}
233 			break;
234 		}
235 	}
236 
237 	err = -ESRCH;
238 out:
239 	rcu_read_unlock();
240 
241 	return err;
242 }
243 EXPORT_SYMBOL_GPL(fib_rules_lookup);
244 
validate_rulemsg(struct fib_rule_hdr * frh,struct nlattr ** tb,struct fib_rules_ops * ops)245 static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
246 			    struct fib_rules_ops *ops)
247 {
248 	int err = -EINVAL;
249 
250 	if (frh->src_len)
251 		if (tb[FRA_SRC] == NULL ||
252 		    frh->src_len > (ops->addr_size * 8) ||
253 		    nla_len(tb[FRA_SRC]) != ops->addr_size)
254 			goto errout;
255 
256 	if (frh->dst_len)
257 		if (tb[FRA_DST] == NULL ||
258 		    frh->dst_len > (ops->addr_size * 8) ||
259 		    nla_len(tb[FRA_DST]) != ops->addr_size)
260 			goto errout;
261 
262 	err = 0;
263 errout:
264 	return err;
265 }
266 
fib_nl_newrule(struct sk_buff * skb,struct nlmsghdr * nlh,void * arg)267 static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
268 {
269 	struct net *net = sock_net(skb->sk);
270 	struct fib_rule_hdr *frh = nlmsg_data(nlh);
271 	struct fib_rules_ops *ops = NULL;
272 	struct fib_rule *rule, *r, *last = NULL;
273 	struct nlattr *tb[FRA_MAX+1];
274 	int err = -EINVAL, unresolved = 0;
275 
276 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
277 		goto errout;
278 
279 	ops = lookup_rules_ops(net, frh->family);
280 	if (ops == NULL) {
281 		err = -EAFNOSUPPORT;
282 		goto errout;
283 	}
284 
285 	err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
286 	if (err < 0)
287 		goto errout;
288 
289 	err = validate_rulemsg(frh, tb, ops);
290 	if (err < 0)
291 		goto errout;
292 
293 	rule = kzalloc(ops->rule_size, GFP_KERNEL);
294 	if (rule == NULL) {
295 		err = -ENOMEM;
296 		goto errout;
297 	}
298 	rule->fr_net = hold_net(net);
299 
300 	if (tb[FRA_PRIORITY])
301 		rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
302 
303 	if (tb[FRA_IIFNAME]) {
304 		struct net_device *dev;
305 
306 		rule->iifindex = -1;
307 		nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
308 		dev = __dev_get_by_name(net, rule->iifname);
309 		if (dev)
310 			rule->iifindex = dev->ifindex;
311 	}
312 
313 	if (tb[FRA_OIFNAME]) {
314 		struct net_device *dev;
315 
316 		rule->oifindex = -1;
317 		nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
318 		dev = __dev_get_by_name(net, rule->oifname);
319 		if (dev)
320 			rule->oifindex = dev->ifindex;
321 	}
322 
323 	if (tb[FRA_FWMARK]) {
324 		rule->mark = nla_get_u32(tb[FRA_FWMARK]);
325 		if (rule->mark)
326 			/* compatibility: if the mark value is non-zero all bits
327 			 * are compared unless a mask is explicitly specified.
328 			 */
329 			rule->mark_mask = 0xFFFFFFFF;
330 	}
331 
332 	if (tb[FRA_FWMASK])
333 		rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
334 
335 	rule->action = frh->action;
336 	rule->flags = frh->flags;
337 	rule->table = frh_get_table(frh, tb);
338 
339 	if (!tb[FRA_PRIORITY] && ops->default_pref)
340 		rule->pref = ops->default_pref(ops);
341 
342 	err = -EINVAL;
343 	if (tb[FRA_GOTO]) {
344 		if (rule->action != FR_ACT_GOTO)
345 			goto errout_free;
346 
347 		rule->target = nla_get_u32(tb[FRA_GOTO]);
348 		/* Backward jumps are prohibited to avoid endless loops */
349 		if (rule->target <= rule->pref)
350 			goto errout_free;
351 
352 		list_for_each_entry(r, &ops->rules_list, list) {
353 			if (r->pref == rule->target) {
354 				RCU_INIT_POINTER(rule->ctarget, r);
355 				break;
356 			}
357 		}
358 
359 		if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
360 			unresolved = 1;
361 	} else if (rule->action == FR_ACT_GOTO)
362 		goto errout_free;
363 
364 	err = ops->configure(rule, skb, frh, tb);
365 	if (err < 0)
366 		goto errout_free;
367 
368 	list_for_each_entry(r, &ops->rules_list, list) {
369 		if (r->pref > rule->pref)
370 			break;
371 		last = r;
372 	}
373 
374 	fib_rule_get(rule);
375 
376 	if (last)
377 		list_add_rcu(&rule->list, &last->list);
378 	else
379 		list_add_rcu(&rule->list, &ops->rules_list);
380 
381 	if (ops->unresolved_rules) {
382 		/*
383 		 * There are unresolved goto rules in the list, check if
384 		 * any of them are pointing to this new rule.
385 		 */
386 		list_for_each_entry(r, &ops->rules_list, list) {
387 			if (r->action == FR_ACT_GOTO &&
388 			    r->target == rule->pref &&
389 			    rtnl_dereference(r->ctarget) == NULL) {
390 				rcu_assign_pointer(r->ctarget, rule);
391 				if (--ops->unresolved_rules == 0)
392 					break;
393 			}
394 		}
395 	}
396 
397 	if (rule->action == FR_ACT_GOTO)
398 		ops->nr_goto_rules++;
399 
400 	if (unresolved)
401 		ops->unresolved_rules++;
402 
403 	notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
404 	flush_route_cache(ops);
405 	rules_ops_put(ops);
406 	return 0;
407 
408 errout_free:
409 	release_net(rule->fr_net);
410 	kfree(rule);
411 errout:
412 	rules_ops_put(ops);
413 	return err;
414 }
415 
fib_nl_delrule(struct sk_buff * skb,struct nlmsghdr * nlh,void * arg)416 static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
417 {
418 	struct net *net = sock_net(skb->sk);
419 	struct fib_rule_hdr *frh = nlmsg_data(nlh);
420 	struct fib_rules_ops *ops = NULL;
421 	struct fib_rule *rule, *tmp;
422 	struct nlattr *tb[FRA_MAX+1];
423 	int err = -EINVAL;
424 
425 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
426 		goto errout;
427 
428 	ops = lookup_rules_ops(net, frh->family);
429 	if (ops == NULL) {
430 		err = -EAFNOSUPPORT;
431 		goto errout;
432 	}
433 
434 	err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
435 	if (err < 0)
436 		goto errout;
437 
438 	err = validate_rulemsg(frh, tb, ops);
439 	if (err < 0)
440 		goto errout;
441 
442 	list_for_each_entry(rule, &ops->rules_list, list) {
443 		if (frh->action && (frh->action != rule->action))
444 			continue;
445 
446 		if (frh_get_table(frh, tb) &&
447 		    (frh_get_table(frh, tb) != rule->table))
448 			continue;
449 
450 		if (tb[FRA_PRIORITY] &&
451 		    (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
452 			continue;
453 
454 		if (tb[FRA_IIFNAME] &&
455 		    nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
456 			continue;
457 
458 		if (tb[FRA_OIFNAME] &&
459 		    nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
460 			continue;
461 
462 		if (tb[FRA_FWMARK] &&
463 		    (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
464 			continue;
465 
466 		if (tb[FRA_FWMASK] &&
467 		    (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
468 			continue;
469 
470 		if (!ops->compare(rule, frh, tb))
471 			continue;
472 
473 		if (rule->flags & FIB_RULE_PERMANENT) {
474 			err = -EPERM;
475 			goto errout;
476 		}
477 
478 		list_del_rcu(&rule->list);
479 
480 		if (rule->action == FR_ACT_GOTO) {
481 			ops->nr_goto_rules--;
482 			if (rtnl_dereference(rule->ctarget) == NULL)
483 				ops->unresolved_rules--;
484 		}
485 
486 		/*
487 		 * Check if this rule is a target to any of them. If so,
488 		 * disable them. As this operation is eventually very
489 		 * expensive, it is only performed if goto rules have
490 		 * actually been added.
491 		 */
492 		if (ops->nr_goto_rules > 0) {
493 			list_for_each_entry(tmp, &ops->rules_list, list) {
494 				if (rtnl_dereference(tmp->ctarget) == rule) {
495 					RCU_INIT_POINTER(tmp->ctarget, NULL);
496 					ops->unresolved_rules++;
497 				}
498 			}
499 		}
500 
501 		notify_rule_change(RTM_DELRULE, rule, ops, nlh,
502 				   NETLINK_CB(skb).pid);
503 		fib_rule_put(rule);
504 		flush_route_cache(ops);
505 		rules_ops_put(ops);
506 		return 0;
507 	}
508 
509 	err = -ENOENT;
510 errout:
511 	rules_ops_put(ops);
512 	return err;
513 }
514 
fib_rule_nlmsg_size(struct fib_rules_ops * ops,struct fib_rule * rule)515 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
516 					 struct fib_rule *rule)
517 {
518 	size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
519 			 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
520 			 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
521 			 + nla_total_size(4) /* FRA_PRIORITY */
522 			 + nla_total_size(4) /* FRA_TABLE */
523 			 + nla_total_size(4) /* FRA_FWMARK */
524 			 + nla_total_size(4); /* FRA_FWMASK */
525 
526 	if (ops->nlmsg_payload)
527 		payload += ops->nlmsg_payload(rule);
528 
529 	return payload;
530 }
531 
fib_nl_fill_rule(struct sk_buff * skb,struct fib_rule * rule,u32 pid,u32 seq,int type,int flags,struct fib_rules_ops * ops)532 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
533 			    u32 pid, u32 seq, int type, int flags,
534 			    struct fib_rules_ops *ops)
535 {
536 	struct nlmsghdr *nlh;
537 	struct fib_rule_hdr *frh;
538 
539 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
540 	if (nlh == NULL)
541 		return -EMSGSIZE;
542 
543 	frh = nlmsg_data(nlh);
544 	frh->family = ops->family;
545 	frh->table = rule->table;
546 	NLA_PUT_U32(skb, FRA_TABLE, rule->table);
547 	frh->res1 = 0;
548 	frh->res2 = 0;
549 	frh->action = rule->action;
550 	frh->flags = rule->flags;
551 
552 	if (rule->action == FR_ACT_GOTO &&
553 	    rcu_access_pointer(rule->ctarget) == NULL)
554 		frh->flags |= FIB_RULE_UNRESOLVED;
555 
556 	if (rule->iifname[0]) {
557 		NLA_PUT_STRING(skb, FRA_IIFNAME, rule->iifname);
558 
559 		if (rule->iifindex == -1)
560 			frh->flags |= FIB_RULE_IIF_DETACHED;
561 	}
562 
563 	if (rule->oifname[0]) {
564 		NLA_PUT_STRING(skb, FRA_OIFNAME, rule->oifname);
565 
566 		if (rule->oifindex == -1)
567 			frh->flags |= FIB_RULE_OIF_DETACHED;
568 	}
569 
570 	if (rule->pref)
571 		NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
572 
573 	if (rule->mark)
574 		NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
575 
576 	if (rule->mark_mask || rule->mark)
577 		NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
578 
579 	if (rule->target)
580 		NLA_PUT_U32(skb, FRA_GOTO, rule->target);
581 
582 	if (ops->fill(rule, skb, frh) < 0)
583 		goto nla_put_failure;
584 
585 	return nlmsg_end(skb, nlh);
586 
587 nla_put_failure:
588 	nlmsg_cancel(skb, nlh);
589 	return -EMSGSIZE;
590 }
591 
dump_rules(struct sk_buff * skb,struct netlink_callback * cb,struct fib_rules_ops * ops)592 static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
593 		      struct fib_rules_ops *ops)
594 {
595 	int idx = 0;
596 	struct fib_rule *rule;
597 
598 	rcu_read_lock();
599 	list_for_each_entry_rcu(rule, &ops->rules_list, list) {
600 		if (idx < cb->args[1])
601 			goto skip;
602 
603 		if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
604 				     cb->nlh->nlmsg_seq, RTM_NEWRULE,
605 				     NLM_F_MULTI, ops) < 0)
606 			break;
607 skip:
608 		idx++;
609 	}
610 	rcu_read_unlock();
611 	cb->args[1] = idx;
612 	rules_ops_put(ops);
613 
614 	return skb->len;
615 }
616 
fib_nl_dumprule(struct sk_buff * skb,struct netlink_callback * cb)617 static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
618 {
619 	struct net *net = sock_net(skb->sk);
620 	struct fib_rules_ops *ops;
621 	int idx = 0, family;
622 
623 	family = rtnl_msg_family(cb->nlh);
624 	if (family != AF_UNSPEC) {
625 		/* Protocol specific dump request */
626 		ops = lookup_rules_ops(net, family);
627 		if (ops == NULL)
628 			return -EAFNOSUPPORT;
629 
630 		return dump_rules(skb, cb, ops);
631 	}
632 
633 	rcu_read_lock();
634 	list_for_each_entry_rcu(ops, &net->rules_ops, list) {
635 		if (idx < cb->args[0] || !try_module_get(ops->owner))
636 			goto skip;
637 
638 		if (dump_rules(skb, cb, ops) < 0)
639 			break;
640 
641 		cb->args[1] = 0;
642 skip:
643 		idx++;
644 	}
645 	rcu_read_unlock();
646 	cb->args[0] = idx;
647 
648 	return skb->len;
649 }
650 
notify_rule_change(int event,struct fib_rule * rule,struct fib_rules_ops * ops,struct nlmsghdr * nlh,u32 pid)651 static void notify_rule_change(int event, struct fib_rule *rule,
652 			       struct fib_rules_ops *ops, struct nlmsghdr *nlh,
653 			       u32 pid)
654 {
655 	struct net *net;
656 	struct sk_buff *skb;
657 	int err = -ENOBUFS;
658 
659 	net = ops->fro_net;
660 	skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
661 	if (skb == NULL)
662 		goto errout;
663 
664 	err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
665 	if (err < 0) {
666 		/* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
667 		WARN_ON(err == -EMSGSIZE);
668 		kfree_skb(skb);
669 		goto errout;
670 	}
671 
672 	rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
673 	return;
674 errout:
675 	if (err < 0)
676 		rtnl_set_sk_err(net, ops->nlgroup, err);
677 }
678 
attach_rules(struct list_head * rules,struct net_device * dev)679 static void attach_rules(struct list_head *rules, struct net_device *dev)
680 {
681 	struct fib_rule *rule;
682 
683 	list_for_each_entry(rule, rules, list) {
684 		if (rule->iifindex == -1 &&
685 		    strcmp(dev->name, rule->iifname) == 0)
686 			rule->iifindex = dev->ifindex;
687 		if (rule->oifindex == -1 &&
688 		    strcmp(dev->name, rule->oifname) == 0)
689 			rule->oifindex = dev->ifindex;
690 	}
691 }
692 
detach_rules(struct list_head * rules,struct net_device * dev)693 static void detach_rules(struct list_head *rules, struct net_device *dev)
694 {
695 	struct fib_rule *rule;
696 
697 	list_for_each_entry(rule, rules, list) {
698 		if (rule->iifindex == dev->ifindex)
699 			rule->iifindex = -1;
700 		if (rule->oifindex == dev->ifindex)
701 			rule->oifindex = -1;
702 	}
703 }
704 
705 
fib_rules_event(struct notifier_block * this,unsigned long event,void * ptr)706 static int fib_rules_event(struct notifier_block *this, unsigned long event,
707 			    void *ptr)
708 {
709 	struct net_device *dev = ptr;
710 	struct net *net = dev_net(dev);
711 	struct fib_rules_ops *ops;
712 
713 	ASSERT_RTNL();
714 
715 	switch (event) {
716 	case NETDEV_REGISTER:
717 		list_for_each_entry(ops, &net->rules_ops, list)
718 			attach_rules(&ops->rules_list, dev);
719 		break;
720 
721 	case NETDEV_CHANGENAME:
722 		list_for_each_entry(ops, &net->rules_ops, list) {
723 			detach_rules(&ops->rules_list, dev);
724 			attach_rules(&ops->rules_list, dev);
725 		}
726 		break;
727 
728 	case NETDEV_UNREGISTER:
729 		list_for_each_entry(ops, &net->rules_ops, list)
730 			detach_rules(&ops->rules_list, dev);
731 		break;
732 	}
733 
734 	return NOTIFY_DONE;
735 }
736 
737 static struct notifier_block fib_rules_notifier = {
738 	.notifier_call = fib_rules_event,
739 };
740 
fib_rules_net_init(struct net * net)741 static int __net_init fib_rules_net_init(struct net *net)
742 {
743 	INIT_LIST_HEAD(&net->rules_ops);
744 	spin_lock_init(&net->rules_mod_lock);
745 	return 0;
746 }
747 
748 static struct pernet_operations fib_rules_net_ops = {
749 	.init = fib_rules_net_init,
750 };
751 
fib_rules_init(void)752 static int __init fib_rules_init(void)
753 {
754 	int err;
755 	rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
756 	rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
757 	rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
758 
759 	err = register_pernet_subsys(&fib_rules_net_ops);
760 	if (err < 0)
761 		goto fail;
762 
763 	err = register_netdevice_notifier(&fib_rules_notifier);
764 	if (err < 0)
765 		goto fail_unregister;
766 
767 	return 0;
768 
769 fail_unregister:
770 	unregister_pernet_subsys(&fib_rules_net_ops);
771 fail:
772 	rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
773 	rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
774 	rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
775 	return err;
776 }
777 
778 subsys_initcall(fib_rules_init);
779