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/list.h>
14 #include <net/net_namespace.h>
16 #include <net/fib_rules.h>
18 int fib_default_rule_add(struct fib_rules_ops
*ops
,
19 u32 pref
, u32 table
, u32 flags
)
23 r
= kzalloc(ops
->rule_size
, GFP_KERNEL
);
27 atomic_set(&r
->refcnt
, 1);
28 r
->action
= FR_ACT_TO_TBL
;
32 r
->fr_net
= ops
->fro_net
;
34 /* The lock is not required here, the list in unreacheable
35 * at the moment this function is called */
36 list_add_tail(&r
->list
, &ops
->rules_list
);
39 EXPORT_SYMBOL(fib_default_rule_add
);
41 static void notify_rule_change(int event
, struct fib_rule
*rule
,
42 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
45 static struct fib_rules_ops
*lookup_rules_ops(struct net
*net
, int family
)
47 struct fib_rules_ops
*ops
;
50 list_for_each_entry_rcu(ops
, &net
->rules_ops
, list
) {
51 if (ops
->family
== family
) {
52 if (!try_module_get(ops
->owner
))
63 static void rules_ops_put(struct fib_rules_ops
*ops
)
66 module_put(ops
->owner
);
69 static void flush_route_cache(struct fib_rules_ops
*ops
)
75 int fib_rules_register(struct fib_rules_ops
*ops
)
78 struct fib_rules_ops
*o
;
83 if (ops
->rule_size
< sizeof(struct fib_rule
))
86 if (ops
->match
== NULL
|| ops
->configure
== NULL
||
87 ops
->compare
== NULL
|| ops
->fill
== NULL
||
91 spin_lock(&net
->rules_mod_lock
);
92 list_for_each_entry(o
, &net
->rules_ops
, list
)
93 if (ops
->family
== o
->family
)
97 list_add_tail_rcu(&ops
->list
, &net
->rules_ops
);
100 spin_unlock(&net
->rules_mod_lock
);
105 EXPORT_SYMBOL_GPL(fib_rules_register
);
107 void fib_rules_cleanup_ops(struct fib_rules_ops
*ops
)
109 struct fib_rule
*rule
, *tmp
;
111 list_for_each_entry_safe(rule
, tmp
, &ops
->rules_list
, list
) {
112 list_del_rcu(&rule
->list
);
116 EXPORT_SYMBOL_GPL(fib_rules_cleanup_ops
);
118 void fib_rules_unregister(struct fib_rules_ops
*ops
)
120 struct net
*net
= ops
->fro_net
;
122 spin_lock(&net
->rules_mod_lock
);
123 list_del_rcu(&ops
->list
);
124 fib_rules_cleanup_ops(ops
);
125 spin_unlock(&net
->rules_mod_lock
);
131 EXPORT_SYMBOL_GPL(fib_rules_unregister
);
133 static int fib_rule_match(struct fib_rule
*rule
, struct fib_rules_ops
*ops
,
134 struct flowi
*fl
, int flags
)
138 if (rule
->ifindex
&& (rule
->ifindex
!= fl
->iif
))
141 if ((rule
->mark
^ fl
->mark
) & rule
->mark_mask
)
144 ret
= ops
->match(rule
, fl
, flags
);
146 return (rule
->flags
& FIB_RULE_INVERT
) ? !ret
: ret
;
149 int fib_rules_lookup(struct fib_rules_ops
*ops
, struct flowi
*fl
,
150 int flags
, struct fib_lookup_arg
*arg
)
152 struct fib_rule
*rule
;
157 list_for_each_entry_rcu(rule
, &ops
->rules_list
, list
) {
159 if (!fib_rule_match(rule
, ops
, fl
, flags
))
162 if (rule
->action
== FR_ACT_GOTO
) {
163 struct fib_rule
*target
;
165 target
= rcu_dereference(rule
->ctarget
);
166 if (target
== NULL
) {
172 } else if (rule
->action
== FR_ACT_NOP
)
175 err
= ops
->action(rule
, fl
, flags
, arg
);
177 if (err
!= -EAGAIN
) {
191 EXPORT_SYMBOL_GPL(fib_rules_lookup
);
193 static int validate_rulemsg(struct fib_rule_hdr
*frh
, struct nlattr
**tb
,
194 struct fib_rules_ops
*ops
)
199 if (tb
[FRA_SRC
] == NULL
||
200 frh
->src_len
> (ops
->addr_size
* 8) ||
201 nla_len(tb
[FRA_SRC
]) != ops
->addr_size
)
205 if (tb
[FRA_DST
] == NULL
||
206 frh
->dst_len
> (ops
->addr_size
* 8) ||
207 nla_len(tb
[FRA_DST
]) != ops
->addr_size
)
215 static int fib_nl_newrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
217 struct net
*net
= skb
->sk
->sk_net
;
218 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
219 struct fib_rules_ops
*ops
= NULL
;
220 struct fib_rule
*rule
, *r
, *last
= NULL
;
221 struct nlattr
*tb
[FRA_MAX
+1];
222 int err
= -EINVAL
, unresolved
= 0;
224 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
)))
227 ops
= lookup_rules_ops(net
, frh
->family
);
233 err
= nlmsg_parse(nlh
, sizeof(*frh
), tb
, FRA_MAX
, ops
->policy
);
237 err
= validate_rulemsg(frh
, tb
, ops
);
241 rule
= kzalloc(ops
->rule_size
, GFP_KERNEL
);
248 if (tb
[FRA_PRIORITY
])
249 rule
->pref
= nla_get_u32(tb
[FRA_PRIORITY
]);
251 if (tb
[FRA_IFNAME
]) {
252 struct net_device
*dev
;
255 nla_strlcpy(rule
->ifname
, tb
[FRA_IFNAME
], IFNAMSIZ
);
256 dev
= __dev_get_by_name(net
, rule
->ifname
);
258 rule
->ifindex
= dev
->ifindex
;
261 if (tb
[FRA_FWMARK
]) {
262 rule
->mark
= nla_get_u32(tb
[FRA_FWMARK
]);
264 /* compatibility: if the mark value is non-zero all bits
265 * are compared unless a mask is explicitly specified.
267 rule
->mark_mask
= 0xFFFFFFFF;
271 rule
->mark_mask
= nla_get_u32(tb
[FRA_FWMASK
]);
273 rule
->action
= frh
->action
;
274 rule
->flags
= frh
->flags
;
275 rule
->table
= frh_get_table(frh
, tb
);
277 if (!rule
->pref
&& ops
->default_pref
)
278 rule
->pref
= ops
->default_pref(ops
);
282 if (rule
->action
!= FR_ACT_GOTO
)
285 rule
->target
= nla_get_u32(tb
[FRA_GOTO
]);
286 /* Backward jumps are prohibited to avoid endless loops */
287 if (rule
->target
<= rule
->pref
)
290 list_for_each_entry(r
, &ops
->rules_list
, list
) {
291 if (r
->pref
== rule
->target
) {
297 if (rule
->ctarget
== NULL
)
299 } else if (rule
->action
== FR_ACT_GOTO
)
302 err
= ops
->configure(rule
, skb
, nlh
, frh
, tb
);
306 list_for_each_entry(r
, &ops
->rules_list
, list
) {
307 if (r
->pref
> rule
->pref
)
314 if (ops
->unresolved_rules
) {
316 * There are unresolved goto rules in the list, check if
317 * any of them are pointing to this new rule.
319 list_for_each_entry(r
, &ops
->rules_list
, list
) {
320 if (r
->action
== FR_ACT_GOTO
&&
321 r
->target
== rule
->pref
) {
322 BUG_ON(r
->ctarget
!= NULL
);
323 rcu_assign_pointer(r
->ctarget
, rule
);
324 if (--ops
->unresolved_rules
== 0)
330 if (rule
->action
== FR_ACT_GOTO
)
331 ops
->nr_goto_rules
++;
334 ops
->unresolved_rules
++;
337 list_add_rcu(&rule
->list
, &last
->list
);
339 list_add_rcu(&rule
->list
, &ops
->rules_list
);
341 notify_rule_change(RTM_NEWRULE
, rule
, ops
, nlh
, NETLINK_CB(skb
).pid
);
342 flush_route_cache(ops
);
353 static int fib_nl_delrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
355 struct net
*net
= skb
->sk
->sk_net
;
356 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
357 struct fib_rules_ops
*ops
= NULL
;
358 struct fib_rule
*rule
, *tmp
;
359 struct nlattr
*tb
[FRA_MAX
+1];
362 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
)))
365 ops
= lookup_rules_ops(net
, frh
->family
);
371 err
= nlmsg_parse(nlh
, sizeof(*frh
), tb
, FRA_MAX
, ops
->policy
);
375 err
= validate_rulemsg(frh
, tb
, ops
);
379 list_for_each_entry(rule
, &ops
->rules_list
, list
) {
380 if (frh
->action
&& (frh
->action
!= rule
->action
))
383 if (frh
->table
&& (frh_get_table(frh
, tb
) != rule
->table
))
386 if (tb
[FRA_PRIORITY
] &&
387 (rule
->pref
!= nla_get_u32(tb
[FRA_PRIORITY
])))
390 if (tb
[FRA_IFNAME
] &&
391 nla_strcmp(tb
[FRA_IFNAME
], rule
->ifname
))
394 if (tb
[FRA_FWMARK
] &&
395 (rule
->mark
!= nla_get_u32(tb
[FRA_FWMARK
])))
398 if (tb
[FRA_FWMASK
] &&
399 (rule
->mark_mask
!= nla_get_u32(tb
[FRA_FWMASK
])))
402 if (!ops
->compare(rule
, frh
, tb
))
405 if (rule
->flags
& FIB_RULE_PERMANENT
) {
410 list_del_rcu(&rule
->list
);
412 if (rule
->action
== FR_ACT_GOTO
)
413 ops
->nr_goto_rules
--;
416 * Check if this rule is a target to any of them. If so,
417 * disable them. As this operation is eventually very
418 * expensive, it is only performed if goto rules have
419 * actually been added.
421 if (ops
->nr_goto_rules
> 0) {
422 list_for_each_entry(tmp
, &ops
->rules_list
, list
) {
423 if (tmp
->ctarget
== rule
) {
424 rcu_assign_pointer(tmp
->ctarget
, NULL
);
425 ops
->unresolved_rules
++;
431 notify_rule_change(RTM_DELRULE
, rule
, ops
, nlh
,
432 NETLINK_CB(skb
).pid
);
434 flush_route_cache(ops
);
445 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops
*ops
,
446 struct fib_rule
*rule
)
448 size_t payload
= NLMSG_ALIGN(sizeof(struct fib_rule_hdr
))
449 + nla_total_size(IFNAMSIZ
) /* FRA_IFNAME */
450 + nla_total_size(4) /* FRA_PRIORITY */
451 + nla_total_size(4) /* FRA_TABLE */
452 + nla_total_size(4) /* FRA_FWMARK */
453 + nla_total_size(4); /* FRA_FWMASK */
455 if (ops
->nlmsg_payload
)
456 payload
+= ops
->nlmsg_payload(rule
);
461 static int fib_nl_fill_rule(struct sk_buff
*skb
, struct fib_rule
*rule
,
462 u32 pid
, u32 seq
, int type
, int flags
,
463 struct fib_rules_ops
*ops
)
465 struct nlmsghdr
*nlh
;
466 struct fib_rule_hdr
*frh
;
468 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*frh
), flags
);
472 frh
= nlmsg_data(nlh
);
473 frh
->table
= rule
->table
;
474 NLA_PUT_U32(skb
, FRA_TABLE
, rule
->table
);
477 frh
->action
= rule
->action
;
478 frh
->flags
= rule
->flags
;
480 if (rule
->action
== FR_ACT_GOTO
&& rule
->ctarget
== NULL
)
481 frh
->flags
|= FIB_RULE_UNRESOLVED
;
483 if (rule
->ifname
[0]) {
484 NLA_PUT_STRING(skb
, FRA_IFNAME
, rule
->ifname
);
486 if (rule
->ifindex
== -1)
487 frh
->flags
|= FIB_RULE_DEV_DETACHED
;
491 NLA_PUT_U32(skb
, FRA_PRIORITY
, rule
->pref
);
494 NLA_PUT_U32(skb
, FRA_FWMARK
, rule
->mark
);
496 if (rule
->mark_mask
|| rule
->mark
)
497 NLA_PUT_U32(skb
, FRA_FWMASK
, rule
->mark_mask
);
500 NLA_PUT_U32(skb
, FRA_GOTO
, rule
->target
);
502 if (ops
->fill(rule
, skb
, nlh
, frh
) < 0)
503 goto nla_put_failure
;
505 return nlmsg_end(skb
, nlh
);
508 nlmsg_cancel(skb
, nlh
);
512 static int dump_rules(struct sk_buff
*skb
, struct netlink_callback
*cb
,
513 struct fib_rules_ops
*ops
)
516 struct fib_rule
*rule
;
518 list_for_each_entry(rule
, &ops
->rules_list
, list
) {
519 if (idx
< cb
->args
[1])
522 if (fib_nl_fill_rule(skb
, rule
, NETLINK_CB(cb
->skb
).pid
,
523 cb
->nlh
->nlmsg_seq
, RTM_NEWRULE
,
524 NLM_F_MULTI
, ops
) < 0)
535 static int fib_nl_dumprule(struct sk_buff
*skb
, struct netlink_callback
*cb
)
537 struct net
*net
= skb
->sk
->sk_net
;
538 struct fib_rules_ops
*ops
;
541 family
= rtnl_msg_family(cb
->nlh
);
542 if (family
!= AF_UNSPEC
) {
543 /* Protocol specific dump request */
544 ops
= lookup_rules_ops(net
, family
);
546 return -EAFNOSUPPORT
;
548 return dump_rules(skb
, cb
, ops
);
552 list_for_each_entry_rcu(ops
, &net
->rules_ops
, list
) {
553 if (idx
< cb
->args
[0] || !try_module_get(ops
->owner
))
556 if (dump_rules(skb
, cb
, ops
) < 0)
569 static void notify_rule_change(int event
, struct fib_rule
*rule
,
570 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
578 skb
= nlmsg_new(fib_rule_nlmsg_size(ops
, rule
), GFP_KERNEL
);
582 err
= fib_nl_fill_rule(skb
, rule
, pid
, nlh
->nlmsg_seq
, event
, 0, ops
);
584 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
585 WARN_ON(err
== -EMSGSIZE
);
590 err
= rtnl_notify(skb
, net
, pid
, ops
->nlgroup
, nlh
, GFP_KERNEL
);
593 rtnl_set_sk_err(net
, ops
->nlgroup
, err
);
596 static void attach_rules(struct list_head
*rules
, struct net_device
*dev
)
598 struct fib_rule
*rule
;
600 list_for_each_entry(rule
, rules
, list
) {
601 if (rule
->ifindex
== -1 &&
602 strcmp(dev
->name
, rule
->ifname
) == 0)
603 rule
->ifindex
= dev
->ifindex
;
607 static void detach_rules(struct list_head
*rules
, struct net_device
*dev
)
609 struct fib_rule
*rule
;
611 list_for_each_entry(rule
, rules
, list
)
612 if (rule
->ifindex
== dev
->ifindex
)
617 static int fib_rules_event(struct notifier_block
*this, unsigned long event
,
620 struct net_device
*dev
= ptr
;
621 struct net
*net
= dev
->nd_net
;
622 struct fib_rules_ops
*ops
;
628 case NETDEV_REGISTER
:
629 list_for_each_entry(ops
, &net
->rules_ops
, list
)
630 attach_rules(&ops
->rules_list
, dev
);
633 case NETDEV_UNREGISTER
:
634 list_for_each_entry(ops
, &net
->rules_ops
, list
)
635 detach_rules(&ops
->rules_list
, dev
);
644 static struct notifier_block fib_rules_notifier
= {
645 .notifier_call
= fib_rules_event
,
648 static int fib_rules_net_init(struct net
*net
)
650 INIT_LIST_HEAD(&net
->rules_ops
);
651 spin_lock_init(&net
->rules_mod_lock
);
655 static struct pernet_operations fib_rules_net_ops
= {
656 .init
= fib_rules_net_init
,
659 static int __init
fib_rules_init(void)
662 rtnl_register(PF_UNSPEC
, RTM_NEWRULE
, fib_nl_newrule
, NULL
);
663 rtnl_register(PF_UNSPEC
, RTM_DELRULE
, fib_nl_delrule
, NULL
);
664 rtnl_register(PF_UNSPEC
, RTM_GETRULE
, NULL
, fib_nl_dumprule
);
666 err
= register_netdevice_notifier(&fib_rules_notifier
);
670 err
= register_pernet_subsys(&fib_rules_net_ops
);
672 goto fail_unregister
;
676 unregister_netdevice_notifier(&fib_rules_notifier
);
678 rtnl_unregister(PF_UNSPEC
, RTM_NEWRULE
);
679 rtnl_unregister(PF_UNSPEC
, RTM_DELRULE
);
680 rtnl_unregister(PF_UNSPEC
, RTM_GETRULE
);
684 subsys_initcall(fib_rules_init
);