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 <net/net_namespace.h>
17 #include <net/fib_rules.h>
19 int fib_default_rule_add(struct fib_rules_ops
*ops
,
20 u32 pref
, u32 table
, u32 flags
)
24 r
= kzalloc(ops
->rule_size
, GFP_KERNEL
);
28 atomic_set(&r
->refcnt
, 1);
29 r
->action
= FR_ACT_TO_TBL
;
33 r
->fr_net
= hold_net(ops
->fro_net
);
35 /* The lock is not required here, the list in unreacheable
36 * at the moment this function is called */
37 list_add_tail(&r
->list
, &ops
->rules_list
);
40 EXPORT_SYMBOL(fib_default_rule_add
);
42 u32
fib_default_rule_pref(struct fib_rules_ops
*ops
)
44 struct list_head
*pos
;
45 struct fib_rule
*rule
;
47 if (!list_empty(&ops
->rules_list
)) {
48 pos
= ops
->rules_list
.next
;
49 if (pos
->next
!= &ops
->rules_list
) {
50 rule
= list_entry(pos
->next
, struct fib_rule
, list
);
52 return rule
->pref
- 1;
58 EXPORT_SYMBOL(fib_default_rule_pref
);
60 static void notify_rule_change(int event
, struct fib_rule
*rule
,
61 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
64 static struct fib_rules_ops
*lookup_rules_ops(struct net
*net
, int family
)
66 struct fib_rules_ops
*ops
;
69 list_for_each_entry_rcu(ops
, &net
->rules_ops
, list
) {
70 if (ops
->family
== family
) {
71 if (!try_module_get(ops
->owner
))
82 static void rules_ops_put(struct fib_rules_ops
*ops
)
85 module_put(ops
->owner
);
88 static void flush_route_cache(struct fib_rules_ops
*ops
)
91 ops
->flush_cache(ops
);
94 static int __fib_rules_register(struct fib_rules_ops
*ops
)
97 struct fib_rules_ops
*o
;
102 if (ops
->rule_size
< sizeof(struct fib_rule
))
105 if (ops
->match
== NULL
|| ops
->configure
== NULL
||
106 ops
->compare
== NULL
|| ops
->fill
== NULL
||
110 spin_lock(&net
->rules_mod_lock
);
111 list_for_each_entry(o
, &net
->rules_ops
, list
)
112 if (ops
->family
== o
->family
)
116 list_add_tail_rcu(&ops
->list
, &net
->rules_ops
);
119 spin_unlock(&net
->rules_mod_lock
);
124 struct fib_rules_ops
*
125 fib_rules_register(const struct fib_rules_ops
*tmpl
, struct net
*net
)
127 struct fib_rules_ops
*ops
;
130 ops
= kmemdup(tmpl
, sizeof(*ops
), GFP_KERNEL
);
132 return ERR_PTR(-ENOMEM
);
134 INIT_LIST_HEAD(&ops
->rules_list
);
137 err
= __fib_rules_register(ops
);
145 EXPORT_SYMBOL_GPL(fib_rules_register
);
147 void fib_rules_cleanup_ops(struct fib_rules_ops
*ops
)
149 struct fib_rule
*rule
, *tmp
;
151 list_for_each_entry_safe(rule
, tmp
, &ops
->rules_list
, list
) {
152 list_del_rcu(&rule
->list
);
156 EXPORT_SYMBOL_GPL(fib_rules_cleanup_ops
);
158 static void fib_rules_put_rcu(struct rcu_head
*head
)
160 struct fib_rules_ops
*ops
= container_of(head
, struct fib_rules_ops
, rcu
);
161 struct net
*net
= ops
->fro_net
;
167 void fib_rules_unregister(struct fib_rules_ops
*ops
)
169 struct net
*net
= ops
->fro_net
;
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
);
176 call_rcu(&ops
->rcu
, fib_rules_put_rcu
);
178 EXPORT_SYMBOL_GPL(fib_rules_unregister
);
180 static int fib_rule_match(struct fib_rule
*rule
, struct fib_rules_ops
*ops
,
181 struct flowi
*fl
, int flags
)
185 if (rule
->iifindex
&& (rule
->iifindex
!= fl
->iif
))
188 if (rule
->oifindex
&& (rule
->oifindex
!= fl
->oif
))
191 if ((rule
->mark
^ fl
->mark
) & rule
->mark_mask
)
194 ret
= ops
->match(rule
, fl
, flags
);
196 return (rule
->flags
& FIB_RULE_INVERT
) ? !ret
: ret
;
199 int fib_rules_lookup(struct fib_rules_ops
*ops
, struct flowi
*fl
,
200 int flags
, struct fib_lookup_arg
*arg
)
202 struct fib_rule
*rule
;
207 list_for_each_entry_rcu(rule
, &ops
->rules_list
, list
) {
209 if (!fib_rule_match(rule
, ops
, fl
, flags
))
212 if (rule
->action
== FR_ACT_GOTO
) {
213 struct fib_rule
*target
;
215 target
= rcu_dereference(rule
->ctarget
);
216 if (target
== NULL
) {
222 } else if (rule
->action
== FR_ACT_NOP
)
225 err
= ops
->action(rule
, fl
, flags
, arg
);
227 if (err
!= -EAGAIN
) {
240 EXPORT_SYMBOL_GPL(fib_rules_lookup
);
242 static int validate_rulemsg(struct fib_rule_hdr
*frh
, struct nlattr
**tb
,
243 struct fib_rules_ops
*ops
)
248 if (tb
[FRA_SRC
] == NULL
||
249 frh
->src_len
> (ops
->addr_size
* 8) ||
250 nla_len(tb
[FRA_SRC
]) != ops
->addr_size
)
254 if (tb
[FRA_DST
] == NULL
||
255 frh
->dst_len
> (ops
->addr_size
* 8) ||
256 nla_len(tb
[FRA_DST
]) != ops
->addr_size
)
264 static int fib_nl_newrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
266 struct net
*net
= sock_net(skb
->sk
);
267 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
268 struct fib_rules_ops
*ops
= NULL
;
269 struct fib_rule
*rule
, *r
, *last
= NULL
;
270 struct nlattr
*tb
[FRA_MAX
+1];
271 int err
= -EINVAL
, unresolved
= 0;
273 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
)))
276 ops
= lookup_rules_ops(net
, frh
->family
);
282 err
= nlmsg_parse(nlh
, sizeof(*frh
), tb
, FRA_MAX
, ops
->policy
);
286 err
= validate_rulemsg(frh
, tb
, ops
);
290 rule
= kzalloc(ops
->rule_size
, GFP_KERNEL
);
295 rule
->fr_net
= hold_net(net
);
297 if (tb
[FRA_PRIORITY
])
298 rule
->pref
= nla_get_u32(tb
[FRA_PRIORITY
]);
300 if (tb
[FRA_IIFNAME
]) {
301 struct net_device
*dev
;
304 nla_strlcpy(rule
->iifname
, tb
[FRA_IIFNAME
], IFNAMSIZ
);
305 dev
= __dev_get_by_name(net
, rule
->iifname
);
307 rule
->iifindex
= dev
->ifindex
;
310 if (tb
[FRA_OIFNAME
]) {
311 struct net_device
*dev
;
314 nla_strlcpy(rule
->oifname
, tb
[FRA_OIFNAME
], IFNAMSIZ
);
315 dev
= __dev_get_by_name(net
, rule
->oifname
);
317 rule
->oifindex
= dev
->ifindex
;
320 if (tb
[FRA_FWMARK
]) {
321 rule
->mark
= nla_get_u32(tb
[FRA_FWMARK
]);
323 /* compatibility: if the mark value is non-zero all bits
324 * are compared unless a mask is explicitly specified.
326 rule
->mark_mask
= 0xFFFFFFFF;
330 rule
->mark_mask
= nla_get_u32(tb
[FRA_FWMASK
]);
332 rule
->action
= frh
->action
;
333 rule
->flags
= frh
->flags
;
334 rule
->table
= frh_get_table(frh
, tb
);
336 if (!tb
[FRA_PRIORITY
] && ops
->default_pref
)
337 rule
->pref
= ops
->default_pref(ops
);
341 if (rule
->action
!= FR_ACT_GOTO
)
344 rule
->target
= nla_get_u32(tb
[FRA_GOTO
]);
345 /* Backward jumps are prohibited to avoid endless loops */
346 if (rule
->target
<= rule
->pref
)
349 list_for_each_entry(r
, &ops
->rules_list
, list
) {
350 if (r
->pref
== rule
->target
) {
356 if (rule
->ctarget
== NULL
)
358 } else if (rule
->action
== FR_ACT_GOTO
)
361 err
= ops
->configure(rule
, skb
, frh
, tb
);
365 list_for_each_entry(r
, &ops
->rules_list
, list
) {
366 if (r
->pref
> rule
->pref
)
373 if (ops
->unresolved_rules
) {
375 * There are unresolved goto rules in the list, check if
376 * any of them are pointing to this new rule.
378 list_for_each_entry(r
, &ops
->rules_list
, list
) {
379 if (r
->action
== FR_ACT_GOTO
&&
380 r
->target
== rule
->pref
) {
381 BUG_ON(r
->ctarget
!= NULL
);
382 rcu_assign_pointer(r
->ctarget
, rule
);
383 if (--ops
->unresolved_rules
== 0)
389 if (rule
->action
== FR_ACT_GOTO
)
390 ops
->nr_goto_rules
++;
393 ops
->unresolved_rules
++;
396 list_add_rcu(&rule
->list
, &last
->list
);
398 list_add_rcu(&rule
->list
, &ops
->rules_list
);
400 notify_rule_change(RTM_NEWRULE
, rule
, ops
, nlh
, NETLINK_CB(skb
).pid
);
401 flush_route_cache(ops
);
406 release_net(rule
->fr_net
);
413 static int fib_nl_delrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
415 struct net
*net
= sock_net(skb
->sk
);
416 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
417 struct fib_rules_ops
*ops
= NULL
;
418 struct fib_rule
*rule
, *tmp
;
419 struct nlattr
*tb
[FRA_MAX
+1];
422 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
)))
425 ops
= lookup_rules_ops(net
, frh
->family
);
431 err
= nlmsg_parse(nlh
, sizeof(*frh
), tb
, FRA_MAX
, ops
->policy
);
435 err
= validate_rulemsg(frh
, tb
, ops
);
439 list_for_each_entry(rule
, &ops
->rules_list
, list
) {
440 if (frh
->action
&& (frh
->action
!= rule
->action
))
443 if (frh
->table
&& (frh_get_table(frh
, tb
) != rule
->table
))
446 if (tb
[FRA_PRIORITY
] &&
447 (rule
->pref
!= nla_get_u32(tb
[FRA_PRIORITY
])))
450 if (tb
[FRA_IIFNAME
] &&
451 nla_strcmp(tb
[FRA_IIFNAME
], rule
->iifname
))
454 if (tb
[FRA_OIFNAME
] &&
455 nla_strcmp(tb
[FRA_OIFNAME
], rule
->oifname
))
458 if (tb
[FRA_FWMARK
] &&
459 (rule
->mark
!= nla_get_u32(tb
[FRA_FWMARK
])))
462 if (tb
[FRA_FWMASK
] &&
463 (rule
->mark_mask
!= nla_get_u32(tb
[FRA_FWMASK
])))
466 if (!ops
->compare(rule
, frh
, tb
))
469 if (rule
->flags
& FIB_RULE_PERMANENT
) {
474 list_del_rcu(&rule
->list
);
476 if (rule
->action
== FR_ACT_GOTO
)
477 ops
->nr_goto_rules
--;
480 * Check if this rule is a target to any of them. If so,
481 * disable them. As this operation is eventually very
482 * expensive, it is only performed if goto rules have
483 * actually been added.
485 if (ops
->nr_goto_rules
> 0) {
486 list_for_each_entry(tmp
, &ops
->rules_list
, list
) {
487 if (tmp
->ctarget
== rule
) {
488 rcu_assign_pointer(tmp
->ctarget
, NULL
);
489 ops
->unresolved_rules
++;
495 notify_rule_change(RTM_DELRULE
, rule
, ops
, nlh
,
496 NETLINK_CB(skb
).pid
);
498 flush_route_cache(ops
);
509 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops
*ops
,
510 struct fib_rule
*rule
)
512 size_t payload
= NLMSG_ALIGN(sizeof(struct fib_rule_hdr
))
513 + nla_total_size(IFNAMSIZ
) /* FRA_IIFNAME */
514 + nla_total_size(IFNAMSIZ
) /* FRA_OIFNAME */
515 + nla_total_size(4) /* FRA_PRIORITY */
516 + nla_total_size(4) /* FRA_TABLE */
517 + nla_total_size(4) /* FRA_FWMARK */
518 + nla_total_size(4); /* FRA_FWMASK */
520 if (ops
->nlmsg_payload
)
521 payload
+= ops
->nlmsg_payload(rule
);
526 static int fib_nl_fill_rule(struct sk_buff
*skb
, struct fib_rule
*rule
,
527 u32 pid
, u32 seq
, int type
, int flags
,
528 struct fib_rules_ops
*ops
)
530 struct nlmsghdr
*nlh
;
531 struct fib_rule_hdr
*frh
;
533 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*frh
), flags
);
537 frh
= nlmsg_data(nlh
);
538 frh
->family
= ops
->family
;
539 frh
->table
= rule
->table
;
540 NLA_PUT_U32(skb
, FRA_TABLE
, rule
->table
);
543 frh
->action
= rule
->action
;
544 frh
->flags
= rule
->flags
;
546 if (rule
->action
== FR_ACT_GOTO
&& rule
->ctarget
== NULL
)
547 frh
->flags
|= FIB_RULE_UNRESOLVED
;
549 if (rule
->iifname
[0]) {
550 NLA_PUT_STRING(skb
, FRA_IIFNAME
, rule
->iifname
);
552 if (rule
->iifindex
== -1)
553 frh
->flags
|= FIB_RULE_IIF_DETACHED
;
556 if (rule
->oifname
[0]) {
557 NLA_PUT_STRING(skb
, FRA_OIFNAME
, rule
->oifname
);
559 if (rule
->oifindex
== -1)
560 frh
->flags
|= FIB_RULE_OIF_DETACHED
;
564 NLA_PUT_U32(skb
, FRA_PRIORITY
, rule
->pref
);
567 NLA_PUT_U32(skb
, FRA_FWMARK
, rule
->mark
);
569 if (rule
->mark_mask
|| rule
->mark
)
570 NLA_PUT_U32(skb
, FRA_FWMASK
, rule
->mark_mask
);
573 NLA_PUT_U32(skb
, FRA_GOTO
, rule
->target
);
575 if (ops
->fill(rule
, skb
, frh
) < 0)
576 goto nla_put_failure
;
578 return nlmsg_end(skb
, nlh
);
581 nlmsg_cancel(skb
, nlh
);
585 static int dump_rules(struct sk_buff
*skb
, struct netlink_callback
*cb
,
586 struct fib_rules_ops
*ops
)
589 struct fib_rule
*rule
;
591 list_for_each_entry(rule
, &ops
->rules_list
, list
) {
592 if (idx
< cb
->args
[1])
595 if (fib_nl_fill_rule(skb
, rule
, NETLINK_CB(cb
->skb
).pid
,
596 cb
->nlh
->nlmsg_seq
, RTM_NEWRULE
,
597 NLM_F_MULTI
, ops
) < 0)
608 static int fib_nl_dumprule(struct sk_buff
*skb
, struct netlink_callback
*cb
)
610 struct net
*net
= sock_net(skb
->sk
);
611 struct fib_rules_ops
*ops
;
614 family
= rtnl_msg_family(cb
->nlh
);
615 if (family
!= AF_UNSPEC
) {
616 /* Protocol specific dump request */
617 ops
= lookup_rules_ops(net
, family
);
619 return -EAFNOSUPPORT
;
621 return dump_rules(skb
, cb
, ops
);
625 list_for_each_entry_rcu(ops
, &net
->rules_ops
, list
) {
626 if (idx
< cb
->args
[0] || !try_module_get(ops
->owner
))
629 if (dump_rules(skb
, cb
, ops
) < 0)
642 static void notify_rule_change(int event
, struct fib_rule
*rule
,
643 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
651 skb
= nlmsg_new(fib_rule_nlmsg_size(ops
, rule
), GFP_KERNEL
);
655 err
= fib_nl_fill_rule(skb
, rule
, pid
, nlh
->nlmsg_seq
, event
, 0, ops
);
657 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
658 WARN_ON(err
== -EMSGSIZE
);
663 rtnl_notify(skb
, net
, pid
, ops
->nlgroup
, nlh
, GFP_KERNEL
);
667 rtnl_set_sk_err(net
, ops
->nlgroup
, err
);
670 static void attach_rules(struct list_head
*rules
, struct net_device
*dev
)
672 struct fib_rule
*rule
;
674 list_for_each_entry(rule
, rules
, list
) {
675 if (rule
->iifindex
== -1 &&
676 strcmp(dev
->name
, rule
->iifname
) == 0)
677 rule
->iifindex
= dev
->ifindex
;
678 if (rule
->oifindex
== -1 &&
679 strcmp(dev
->name
, rule
->oifname
) == 0)
680 rule
->oifindex
= dev
->ifindex
;
684 static void detach_rules(struct list_head
*rules
, struct net_device
*dev
)
686 struct fib_rule
*rule
;
688 list_for_each_entry(rule
, rules
, list
) {
689 if (rule
->iifindex
== dev
->ifindex
)
691 if (rule
->oifindex
== dev
->ifindex
)
697 static int fib_rules_event(struct notifier_block
*this, unsigned long event
,
700 struct net_device
*dev
= ptr
;
701 struct net
*net
= dev_net(dev
);
702 struct fib_rules_ops
*ops
;
707 case NETDEV_REGISTER
:
708 list_for_each_entry(ops
, &net
->rules_ops
, list
)
709 attach_rules(&ops
->rules_list
, dev
);
712 case NETDEV_UNREGISTER
:
713 list_for_each_entry(ops
, &net
->rules_ops
, list
)
714 detach_rules(&ops
->rules_list
, dev
);
721 static struct notifier_block fib_rules_notifier
= {
722 .notifier_call
= fib_rules_event
,
725 static int __net_init
fib_rules_net_init(struct net
*net
)
727 INIT_LIST_HEAD(&net
->rules_ops
);
728 spin_lock_init(&net
->rules_mod_lock
);
732 static struct pernet_operations fib_rules_net_ops
= {
733 .init
= fib_rules_net_init
,
736 static int __init
fib_rules_init(void)
739 rtnl_register(PF_UNSPEC
, RTM_NEWRULE
, fib_nl_newrule
, NULL
);
740 rtnl_register(PF_UNSPEC
, RTM_DELRULE
, fib_nl_delrule
, NULL
);
741 rtnl_register(PF_UNSPEC
, RTM_GETRULE
, NULL
, fib_nl_dumprule
);
743 err
= register_pernet_subsys(&fib_rules_net_ops
);
747 err
= register_netdevice_notifier(&fib_rules_notifier
);
749 goto fail_unregister
;
754 unregister_pernet_subsys(&fib_rules_net_ops
);
756 rtnl_unregister(PF_UNSPEC
, RTM_NEWRULE
);
757 rtnl_unregister(PF_UNSPEC
, RTM_DELRULE
);
758 rtnl_unregister(PF_UNSPEC
, RTM_GETRULE
);
762 subsys_initcall(fib_rules_init
);