2 * net/core/fib_rules.c Generic Routing Rules
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.
8 * Authors: Thomas Graf <tgraf@suug.ch>
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>
18 #include <net/fib_rules.h>
20 int fib_default_rule_add(struct fib_rules_ops
*ops
,
21 u32 pref
, u32 table
, u32 flags
)
25 r
= kzalloc(ops
->rule_size
, GFP_KERNEL
);
29 atomic_set(&r
->refcnt
, 1);
30 r
->action
= FR_ACT_TO_TBL
;
34 r
->fr_net
= hold_net(ops
->fro_net
);
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
);
41 EXPORT_SYMBOL(fib_default_rule_add
);
43 u32
fib_default_rule_pref(struct fib_rules_ops
*ops
)
45 struct list_head
*pos
;
46 struct fib_rule
*rule
;
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
);
53 return rule
->pref
- 1;
59 EXPORT_SYMBOL(fib_default_rule_pref
);
61 static void notify_rule_change(int event
, struct fib_rule
*rule
,
62 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
65 static struct fib_rules_ops
*lookup_rules_ops(struct net
*net
, int family
)
67 struct fib_rules_ops
*ops
;
70 list_for_each_entry_rcu(ops
, &net
->rules_ops
, list
) {
71 if (ops
->family
== family
) {
72 if (!try_module_get(ops
->owner
))
83 static void rules_ops_put(struct fib_rules_ops
*ops
)
86 module_put(ops
->owner
);
89 static void flush_route_cache(struct fib_rules_ops
*ops
)
92 ops
->flush_cache(ops
);
95 static int __fib_rules_register(struct fib_rules_ops
*ops
)
98 struct fib_rules_ops
*o
;
103 if (ops
->rule_size
< sizeof(struct fib_rule
))
106 if (ops
->match
== NULL
|| ops
->configure
== NULL
||
107 ops
->compare
== NULL
|| ops
->fill
== NULL
||
111 spin_lock(&net
->rules_mod_lock
);
112 list_for_each_entry(o
, &net
->rules_ops
, list
)
113 if (ops
->family
== o
->family
)
117 list_add_tail_rcu(&ops
->list
, &net
->rules_ops
);
120 spin_unlock(&net
->rules_mod_lock
);
125 struct fib_rules_ops
*
126 fib_rules_register(const struct fib_rules_ops
*tmpl
, struct net
*net
)
128 struct fib_rules_ops
*ops
;
131 ops
= kmemdup(tmpl
, sizeof(*ops
), GFP_KERNEL
);
133 return ERR_PTR(-ENOMEM
);
135 INIT_LIST_HEAD(&ops
->rules_list
);
138 err
= __fib_rules_register(ops
);
146 EXPORT_SYMBOL_GPL(fib_rules_register
);
148 static void fib_rules_cleanup_ops(struct fib_rules_ops
*ops
)
150 struct fib_rule
*rule
, *tmp
;
152 list_for_each_entry_safe(rule
, tmp
, &ops
->rules_list
, list
) {
153 list_del_rcu(&rule
->list
);
160 static void fib_rules_put_rcu(struct rcu_head
*head
)
162 struct fib_rules_ops
*ops
= container_of(head
, struct fib_rules_ops
, rcu
);
163 struct net
*net
= ops
->fro_net
;
169 void fib_rules_unregister(struct fib_rules_ops
*ops
)
171 struct net
*net
= ops
->fro_net
;
173 spin_lock(&net
->rules_mod_lock
);
174 list_del_rcu(&ops
->list
);
175 fib_rules_cleanup_ops(ops
);
176 spin_unlock(&net
->rules_mod_lock
);
178 call_rcu(&ops
->rcu
, fib_rules_put_rcu
);
180 EXPORT_SYMBOL_GPL(fib_rules_unregister
);
182 static int fib_rule_match(struct fib_rule
*rule
, struct fib_rules_ops
*ops
,
183 struct flowi
*fl
, int flags
)
187 if (rule
->iifindex
&& (rule
->iifindex
!= fl
->flowi_iif
))
190 if (rule
->oifindex
&& (rule
->oifindex
!= fl
->flowi_oif
))
193 if ((rule
->mark
^ fl
->flowi_mark
) & rule
->mark_mask
)
196 ret
= ops
->match(rule
, fl
, flags
);
198 return (rule
->flags
& FIB_RULE_INVERT
) ? !ret
: ret
;
201 int fib_rules_lookup(struct fib_rules_ops
*ops
, struct flowi
*fl
,
202 int flags
, struct fib_lookup_arg
*arg
)
204 struct fib_rule
*rule
;
209 list_for_each_entry_rcu(rule
, &ops
->rules_list
, list
) {
211 if (!fib_rule_match(rule
, ops
, fl
, flags
))
214 if (rule
->action
== FR_ACT_GOTO
) {
215 struct fib_rule
*target
;
217 target
= rcu_dereference(rule
->ctarget
);
218 if (target
== NULL
) {
224 } else if (rule
->action
== FR_ACT_NOP
)
227 err
= ops
->action(rule
, fl
, flags
, arg
);
229 if (err
!= -EAGAIN
) {
230 if ((arg
->flags
& FIB_LOOKUP_NOREF
) ||
231 likely(atomic_inc_not_zero(&rule
->refcnt
))) {
245 EXPORT_SYMBOL_GPL(fib_rules_lookup
);
247 static int validate_rulemsg(struct fib_rule_hdr
*frh
, struct nlattr
**tb
,
248 struct fib_rules_ops
*ops
)
253 if (tb
[FRA_SRC
] == NULL
||
254 frh
->src_len
> (ops
->addr_size
* 8) ||
255 nla_len(tb
[FRA_SRC
]) != ops
->addr_size
)
259 if (tb
[FRA_DST
] == NULL
||
260 frh
->dst_len
> (ops
->addr_size
* 8) ||
261 nla_len(tb
[FRA_DST
]) != ops
->addr_size
)
269 static int fib_nl_newrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
271 struct net
*net
= sock_net(skb
->sk
);
272 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
273 struct fib_rules_ops
*ops
= NULL
;
274 struct fib_rule
*rule
, *r
, *last
= NULL
;
275 struct nlattr
*tb
[FRA_MAX
+1];
276 int err
= -EINVAL
, unresolved
= 0;
278 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
)))
281 ops
= lookup_rules_ops(net
, frh
->family
);
287 err
= nlmsg_parse(nlh
, sizeof(*frh
), tb
, FRA_MAX
, ops
->policy
);
291 err
= validate_rulemsg(frh
, tb
, ops
);
295 rule
= kzalloc(ops
->rule_size
, GFP_KERNEL
);
300 rule
->fr_net
= hold_net(net
);
302 if (tb
[FRA_PRIORITY
])
303 rule
->pref
= nla_get_u32(tb
[FRA_PRIORITY
]);
305 if (tb
[FRA_IIFNAME
]) {
306 struct net_device
*dev
;
309 nla_strlcpy(rule
->iifname
, tb
[FRA_IIFNAME
], IFNAMSIZ
);
310 dev
= __dev_get_by_name(net
, rule
->iifname
);
312 rule
->iifindex
= dev
->ifindex
;
315 if (tb
[FRA_OIFNAME
]) {
316 struct net_device
*dev
;
319 nla_strlcpy(rule
->oifname
, tb
[FRA_OIFNAME
], IFNAMSIZ
);
320 dev
= __dev_get_by_name(net
, rule
->oifname
);
322 rule
->oifindex
= dev
->ifindex
;
325 if (tb
[FRA_FWMARK
]) {
326 rule
->mark
= nla_get_u32(tb
[FRA_FWMARK
]);
328 /* compatibility: if the mark value is non-zero all bits
329 * are compared unless a mask is explicitly specified.
331 rule
->mark_mask
= 0xFFFFFFFF;
335 rule
->mark_mask
= nla_get_u32(tb
[FRA_FWMASK
]);
337 rule
->action
= frh
->action
;
338 rule
->flags
= frh
->flags
;
339 rule
->table
= frh_get_table(frh
, tb
);
341 if (!tb
[FRA_PRIORITY
] && ops
->default_pref
)
342 rule
->pref
= ops
->default_pref(ops
);
346 if (rule
->action
!= FR_ACT_GOTO
)
349 rule
->target
= nla_get_u32(tb
[FRA_GOTO
]);
350 /* Backward jumps are prohibited to avoid endless loops */
351 if (rule
->target
<= rule
->pref
)
354 list_for_each_entry(r
, &ops
->rules_list
, list
) {
355 if (r
->pref
== rule
->target
) {
356 RCU_INIT_POINTER(rule
->ctarget
, r
);
361 if (rcu_dereference_protected(rule
->ctarget
, 1) == NULL
)
363 } else if (rule
->action
== FR_ACT_GOTO
)
366 err
= ops
->configure(rule
, skb
, frh
, tb
);
370 list_for_each_entry(r
, &ops
->rules_list
, list
) {
371 if (r
->pref
> rule
->pref
)
379 list_add_rcu(&rule
->list
, &last
->list
);
381 list_add_rcu(&rule
->list
, &ops
->rules_list
);
383 if (ops
->unresolved_rules
) {
385 * There are unresolved goto rules in the list, check if
386 * any of them are pointing to this new rule.
388 list_for_each_entry(r
, &ops
->rules_list
, list
) {
389 if (r
->action
== FR_ACT_GOTO
&&
390 r
->target
== rule
->pref
&&
391 rtnl_dereference(r
->ctarget
) == NULL
) {
392 rcu_assign_pointer(r
->ctarget
, rule
);
393 if (--ops
->unresolved_rules
== 0)
399 if (rule
->action
== FR_ACT_GOTO
)
400 ops
->nr_goto_rules
++;
403 ops
->unresolved_rules
++;
405 notify_rule_change(RTM_NEWRULE
, rule
, ops
, nlh
, NETLINK_CB(skb
).portid
);
406 flush_route_cache(ops
);
411 release_net(rule
->fr_net
);
418 static int fib_nl_delrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
420 struct net
*net
= sock_net(skb
->sk
);
421 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
422 struct fib_rules_ops
*ops
= NULL
;
423 struct fib_rule
*rule
, *tmp
;
424 struct nlattr
*tb
[FRA_MAX
+1];
427 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
)))
430 ops
= lookup_rules_ops(net
, frh
->family
);
436 err
= nlmsg_parse(nlh
, sizeof(*frh
), tb
, FRA_MAX
, ops
->policy
);
440 err
= validate_rulemsg(frh
, tb
, ops
);
444 list_for_each_entry(rule
, &ops
->rules_list
, list
) {
445 if (frh
->action
&& (frh
->action
!= rule
->action
))
448 if (frh
->table
&& (frh_get_table(frh
, tb
) != rule
->table
))
451 if (tb
[FRA_PRIORITY
] &&
452 (rule
->pref
!= nla_get_u32(tb
[FRA_PRIORITY
])))
455 if (tb
[FRA_IIFNAME
] &&
456 nla_strcmp(tb
[FRA_IIFNAME
], rule
->iifname
))
459 if (tb
[FRA_OIFNAME
] &&
460 nla_strcmp(tb
[FRA_OIFNAME
], rule
->oifname
))
463 if (tb
[FRA_FWMARK
] &&
464 (rule
->mark
!= nla_get_u32(tb
[FRA_FWMARK
])))
467 if (tb
[FRA_FWMASK
] &&
468 (rule
->mark_mask
!= nla_get_u32(tb
[FRA_FWMASK
])))
471 if (!ops
->compare(rule
, frh
, tb
))
474 if (rule
->flags
& FIB_RULE_PERMANENT
) {
479 list_del_rcu(&rule
->list
);
481 if (rule
->action
== FR_ACT_GOTO
) {
482 ops
->nr_goto_rules
--;
483 if (rtnl_dereference(rule
->ctarget
) == NULL
)
484 ops
->unresolved_rules
--;
488 * Check if this rule is a target to any of them. If so,
489 * disable them. As this operation is eventually very
490 * expensive, it is only performed if goto rules have
491 * actually been added.
493 if (ops
->nr_goto_rules
> 0) {
494 list_for_each_entry(tmp
, &ops
->rules_list
, list
) {
495 if (rtnl_dereference(tmp
->ctarget
) == rule
) {
496 RCU_INIT_POINTER(tmp
->ctarget
, NULL
);
497 ops
->unresolved_rules
++;
502 notify_rule_change(RTM_DELRULE
, rule
, ops
, nlh
,
503 NETLINK_CB(skb
).portid
);
507 flush_route_cache(ops
);
518 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops
*ops
,
519 struct fib_rule
*rule
)
521 size_t payload
= NLMSG_ALIGN(sizeof(struct fib_rule_hdr
))
522 + nla_total_size(IFNAMSIZ
) /* FRA_IIFNAME */
523 + nla_total_size(IFNAMSIZ
) /* FRA_OIFNAME */
524 + nla_total_size(4) /* FRA_PRIORITY */
525 + nla_total_size(4) /* FRA_TABLE */
526 + nla_total_size(4) /* FRA_FWMARK */
527 + nla_total_size(4); /* FRA_FWMASK */
529 if (ops
->nlmsg_payload
)
530 payload
+= ops
->nlmsg_payload(rule
);
535 static int fib_nl_fill_rule(struct sk_buff
*skb
, struct fib_rule
*rule
,
536 u32 pid
, u32 seq
, int type
, int flags
,
537 struct fib_rules_ops
*ops
)
539 struct nlmsghdr
*nlh
;
540 struct fib_rule_hdr
*frh
;
542 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*frh
), flags
);
546 frh
= nlmsg_data(nlh
);
547 frh
->family
= ops
->family
;
548 frh
->table
= rule
->table
;
549 if (nla_put_u32(skb
, FRA_TABLE
, rule
->table
))
550 goto nla_put_failure
;
553 frh
->action
= rule
->action
;
554 frh
->flags
= rule
->flags
;
556 if (rule
->action
== FR_ACT_GOTO
&&
557 rcu_access_pointer(rule
->ctarget
) == NULL
)
558 frh
->flags
|= FIB_RULE_UNRESOLVED
;
560 if (rule
->iifname
[0]) {
561 if (nla_put_string(skb
, FRA_IIFNAME
, rule
->iifname
))
562 goto nla_put_failure
;
563 if (rule
->iifindex
== -1)
564 frh
->flags
|= FIB_RULE_IIF_DETACHED
;
567 if (rule
->oifname
[0]) {
568 if (nla_put_string(skb
, FRA_OIFNAME
, rule
->oifname
))
569 goto nla_put_failure
;
570 if (rule
->oifindex
== -1)
571 frh
->flags
|= FIB_RULE_OIF_DETACHED
;
575 nla_put_u32(skb
, FRA_PRIORITY
, rule
->pref
)) ||
577 nla_put_u32(skb
, FRA_FWMARK
, rule
->mark
)) ||
578 ((rule
->mark_mask
|| rule
->mark
) &&
579 nla_put_u32(skb
, FRA_FWMASK
, rule
->mark_mask
)) ||
581 nla_put_u32(skb
, FRA_GOTO
, rule
->target
)))
582 goto nla_put_failure
;
583 if (ops
->fill(rule
, skb
, frh
) < 0)
584 goto nla_put_failure
;
586 return nlmsg_end(skb
, nlh
);
589 nlmsg_cancel(skb
, nlh
);
593 static int dump_rules(struct sk_buff
*skb
, struct netlink_callback
*cb
,
594 struct fib_rules_ops
*ops
)
597 struct fib_rule
*rule
;
600 list_for_each_entry_rcu(rule
, &ops
->rules_list
, list
) {
601 if (idx
< cb
->args
[1])
604 if (fib_nl_fill_rule(skb
, rule
, NETLINK_CB(cb
->skb
).portid
,
605 cb
->nlh
->nlmsg_seq
, RTM_NEWRULE
,
606 NLM_F_MULTI
, ops
) < 0)
618 static int fib_nl_dumprule(struct sk_buff
*skb
, struct netlink_callback
*cb
)
620 struct net
*net
= sock_net(skb
->sk
);
621 struct fib_rules_ops
*ops
;
624 family
= rtnl_msg_family(cb
->nlh
);
625 if (family
!= AF_UNSPEC
) {
626 /* Protocol specific dump request */
627 ops
= lookup_rules_ops(net
, family
);
629 return -EAFNOSUPPORT
;
631 return dump_rules(skb
, cb
, ops
);
635 list_for_each_entry_rcu(ops
, &net
->rules_ops
, list
) {
636 if (idx
< cb
->args
[0] || !try_module_get(ops
->owner
))
639 if (dump_rules(skb
, cb
, ops
) < 0)
652 static void notify_rule_change(int event
, struct fib_rule
*rule
,
653 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
661 skb
= nlmsg_new(fib_rule_nlmsg_size(ops
, rule
), GFP_KERNEL
);
665 err
= fib_nl_fill_rule(skb
, rule
, pid
, nlh
->nlmsg_seq
, event
, 0, ops
);
667 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
668 WARN_ON(err
== -EMSGSIZE
);
673 rtnl_notify(skb
, net
, pid
, ops
->nlgroup
, nlh
, GFP_KERNEL
);
677 rtnl_set_sk_err(net
, ops
->nlgroup
, err
);
680 static void attach_rules(struct list_head
*rules
, struct net_device
*dev
)
682 struct fib_rule
*rule
;
684 list_for_each_entry(rule
, rules
, list
) {
685 if (rule
->iifindex
== -1 &&
686 strcmp(dev
->name
, rule
->iifname
) == 0)
687 rule
->iifindex
= dev
->ifindex
;
688 if (rule
->oifindex
== -1 &&
689 strcmp(dev
->name
, rule
->oifname
) == 0)
690 rule
->oifindex
= dev
->ifindex
;
694 static void detach_rules(struct list_head
*rules
, struct net_device
*dev
)
696 struct fib_rule
*rule
;
698 list_for_each_entry(rule
, rules
, list
) {
699 if (rule
->iifindex
== dev
->ifindex
)
701 if (rule
->oifindex
== dev
->ifindex
)
707 static int fib_rules_event(struct notifier_block
*this, unsigned long event
,
710 struct net_device
*dev
= ptr
;
711 struct net
*net
= dev_net(dev
);
712 struct fib_rules_ops
*ops
;
717 case NETDEV_REGISTER
:
718 list_for_each_entry(ops
, &net
->rules_ops
, list
)
719 attach_rules(&ops
->rules_list
, dev
);
722 case NETDEV_UNREGISTER
:
723 list_for_each_entry(ops
, &net
->rules_ops
, list
)
724 detach_rules(&ops
->rules_list
, dev
);
731 static struct notifier_block fib_rules_notifier
= {
732 .notifier_call
= fib_rules_event
,
735 static int __net_init
fib_rules_net_init(struct net
*net
)
737 INIT_LIST_HEAD(&net
->rules_ops
);
738 spin_lock_init(&net
->rules_mod_lock
);
742 static struct pernet_operations fib_rules_net_ops
= {
743 .init
= fib_rules_net_init
,
746 static int __init
fib_rules_init(void)
749 rtnl_register(PF_UNSPEC
, RTM_NEWRULE
, fib_nl_newrule
, NULL
, NULL
);
750 rtnl_register(PF_UNSPEC
, RTM_DELRULE
, fib_nl_delrule
, NULL
, NULL
);
751 rtnl_register(PF_UNSPEC
, RTM_GETRULE
, NULL
, fib_nl_dumprule
, NULL
);
753 err
= register_pernet_subsys(&fib_rules_net_ops
);
757 err
= register_netdevice_notifier(&fib_rules_notifier
);
759 goto fail_unregister
;
764 unregister_pernet_subsys(&fib_rules_net_ops
);
766 rtnl_unregister(PF_UNSPEC
, RTM_NEWRULE
);
767 rtnl_unregister(PF_UNSPEC
, RTM_DELRULE
);
768 rtnl_unregister(PF_UNSPEC
, RTM_GETRULE
);
772 subsys_initcall(fib_rules_init
);