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/fib_rules.h>
16 static LIST_HEAD(rules_ops
);
17 static DEFINE_SPINLOCK(rules_mod_lock
);
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
;
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(int family
)
47 struct fib_rules_ops
*ops
;
50 list_for_each_entry_rcu(ops
, &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
;
80 if (ops
->rule_size
< sizeof(struct fib_rule
))
83 if (ops
->match
== NULL
|| ops
->configure
== NULL
||
84 ops
->compare
== NULL
|| ops
->fill
== NULL
||
88 spin_lock(&rules_mod_lock
);
89 list_for_each_entry(o
, &rules_ops
, list
)
90 if (ops
->family
== o
->family
)
93 list_add_tail_rcu(&ops
->list
, &rules_ops
);
96 spin_unlock(&rules_mod_lock
);
101 EXPORT_SYMBOL_GPL(fib_rules_register
);
103 static void cleanup_ops(struct fib_rules_ops
*ops
)
105 struct fib_rule
*rule
, *tmp
;
107 list_for_each_entry_safe(rule
, tmp
, &ops
->rules_list
, list
) {
108 list_del_rcu(&rule
->list
);
113 int fib_rules_unregister(struct fib_rules_ops
*ops
)
116 struct fib_rules_ops
*o
;
118 spin_lock(&rules_mod_lock
);
119 list_for_each_entry(o
, &rules_ops
, list
) {
121 list_del_rcu(&o
->list
);
129 spin_unlock(&rules_mod_lock
);
136 EXPORT_SYMBOL_GPL(fib_rules_unregister
);
138 static int fib_rule_match(struct fib_rule
*rule
, struct fib_rules_ops
*ops
,
139 struct flowi
*fl
, int flags
)
143 if (rule
->ifindex
&& (rule
->ifindex
!= fl
->iif
))
146 if ((rule
->mark
^ fl
->mark
) & rule
->mark_mask
)
149 ret
= ops
->match(rule
, fl
, flags
);
151 return (rule
->flags
& FIB_RULE_INVERT
) ? !ret
: ret
;
154 int fib_rules_lookup(struct fib_rules_ops
*ops
, struct flowi
*fl
,
155 int flags
, struct fib_lookup_arg
*arg
)
157 struct fib_rule
*rule
;
162 list_for_each_entry_rcu(rule
, &ops
->rules_list
, list
) {
164 if (!fib_rule_match(rule
, ops
, fl
, flags
))
167 if (rule
->action
== FR_ACT_GOTO
) {
168 struct fib_rule
*target
;
170 target
= rcu_dereference(rule
->ctarget
);
171 if (target
== NULL
) {
177 } else if (rule
->action
== FR_ACT_NOP
)
180 err
= ops
->action(rule
, fl
, flags
, arg
);
182 if (err
!= -EAGAIN
) {
196 EXPORT_SYMBOL_GPL(fib_rules_lookup
);
198 static int validate_rulemsg(struct fib_rule_hdr
*frh
, struct nlattr
**tb
,
199 struct fib_rules_ops
*ops
)
204 if (tb
[FRA_SRC
] == NULL
||
205 frh
->src_len
> (ops
->addr_size
* 8) ||
206 nla_len(tb
[FRA_SRC
]) != ops
->addr_size
)
210 if (tb
[FRA_DST
] == NULL
||
211 frh
->dst_len
> (ops
->addr_size
* 8) ||
212 nla_len(tb
[FRA_DST
]) != ops
->addr_size
)
220 static int fib_nl_newrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
222 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
223 struct fib_rules_ops
*ops
= NULL
;
224 struct fib_rule
*rule
, *r
, *last
= NULL
;
225 struct nlattr
*tb
[FRA_MAX
+1];
226 int err
= -EINVAL
, unresolved
= 0;
228 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
)))
231 ops
= lookup_rules_ops(frh
->family
);
237 err
= nlmsg_parse(nlh
, sizeof(*frh
), tb
, FRA_MAX
, ops
->policy
);
241 err
= validate_rulemsg(frh
, tb
, ops
);
245 rule
= kzalloc(ops
->rule_size
, GFP_KERNEL
);
251 if (tb
[FRA_PRIORITY
])
252 rule
->pref
= nla_get_u32(tb
[FRA_PRIORITY
]);
254 if (tb
[FRA_IFNAME
]) {
255 struct net_device
*dev
;
258 nla_strlcpy(rule
->ifname
, tb
[FRA_IFNAME
], IFNAMSIZ
);
259 dev
= __dev_get_by_name(rule
->ifname
);
261 rule
->ifindex
= dev
->ifindex
;
264 if (tb
[FRA_FWMARK
]) {
265 rule
->mark
= nla_get_u32(tb
[FRA_FWMARK
]);
267 /* compatibility: if the mark value is non-zero all bits
268 * are compared unless a mask is explicitly specified.
270 rule
->mark_mask
= 0xFFFFFFFF;
274 rule
->mark_mask
= nla_get_u32(tb
[FRA_FWMASK
]);
276 rule
->action
= frh
->action
;
277 rule
->flags
= frh
->flags
;
278 rule
->table
= frh_get_table(frh
, tb
);
280 if (!rule
->pref
&& ops
->default_pref
)
281 rule
->pref
= ops
->default_pref();
285 if (rule
->action
!= FR_ACT_GOTO
)
288 rule
->target
= nla_get_u32(tb
[FRA_GOTO
]);
289 /* Backward jumps are prohibited to avoid endless loops */
290 if (rule
->target
<= rule
->pref
)
293 list_for_each_entry(r
, &ops
->rules_list
, list
) {
294 if (r
->pref
== rule
->target
) {
300 if (rule
->ctarget
== NULL
)
302 } else if (rule
->action
== FR_ACT_GOTO
)
305 err
= ops
->configure(rule
, skb
, nlh
, frh
, tb
);
309 list_for_each_entry(r
, &ops
->rules_list
, list
) {
310 if (r
->pref
> rule
->pref
)
317 if (ops
->unresolved_rules
) {
319 * There are unresolved goto rules in the list, check if
320 * any of them are pointing to this new rule.
322 list_for_each_entry(r
, &ops
->rules_list
, list
) {
323 if (r
->action
== FR_ACT_GOTO
&&
324 r
->target
== rule
->pref
) {
325 BUG_ON(r
->ctarget
!= NULL
);
326 rcu_assign_pointer(r
->ctarget
, rule
);
327 if (--ops
->unresolved_rules
== 0)
333 if (rule
->action
== FR_ACT_GOTO
)
334 ops
->nr_goto_rules
++;
337 ops
->unresolved_rules
++;
340 list_add_rcu(&rule
->list
, &last
->list
);
342 list_add_rcu(&rule
->list
, &ops
->rules_list
);
344 notify_rule_change(RTM_NEWRULE
, rule
, ops
, nlh
, NETLINK_CB(skb
).pid
);
345 flush_route_cache(ops
);
356 static int fib_nl_delrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
358 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
359 struct fib_rules_ops
*ops
= NULL
;
360 struct fib_rule
*rule
, *tmp
;
361 struct nlattr
*tb
[FRA_MAX
+1];
364 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
)))
367 ops
= lookup_rules_ops(frh
->family
);
373 err
= nlmsg_parse(nlh
, sizeof(*frh
), tb
, FRA_MAX
, ops
->policy
);
377 err
= validate_rulemsg(frh
, tb
, ops
);
381 list_for_each_entry(rule
, &ops
->rules_list
, list
) {
382 if (frh
->action
&& (frh
->action
!= rule
->action
))
385 if (frh
->table
&& (frh_get_table(frh
, tb
) != rule
->table
))
388 if (tb
[FRA_PRIORITY
] &&
389 (rule
->pref
!= nla_get_u32(tb
[FRA_PRIORITY
])))
392 if (tb
[FRA_IFNAME
] &&
393 nla_strcmp(tb
[FRA_IFNAME
], rule
->ifname
))
396 if (tb
[FRA_FWMARK
] &&
397 (rule
->mark
!= nla_get_u32(tb
[FRA_FWMARK
])))
400 if (tb
[FRA_FWMASK
] &&
401 (rule
->mark_mask
!= nla_get_u32(tb
[FRA_FWMASK
])))
404 if (!ops
->compare(rule
, frh
, tb
))
407 if (rule
->flags
& FIB_RULE_PERMANENT
) {
412 list_del_rcu(&rule
->list
);
414 if (rule
->action
== FR_ACT_GOTO
)
415 ops
->nr_goto_rules
--;
418 * Check if this rule is a target to any of them. If so,
419 * disable them. As this operation is eventually very
420 * expensive, it is only performed if goto rules have
421 * actually been added.
423 if (ops
->nr_goto_rules
> 0) {
424 list_for_each_entry(tmp
, &ops
->rules_list
, list
) {
425 if (tmp
->ctarget
== rule
) {
426 rcu_assign_pointer(tmp
->ctarget
, NULL
);
427 ops
->unresolved_rules
++;
433 notify_rule_change(RTM_DELRULE
, rule
, ops
, nlh
,
434 NETLINK_CB(skb
).pid
);
436 flush_route_cache(ops
);
447 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops
*ops
,
448 struct fib_rule
*rule
)
450 size_t payload
= NLMSG_ALIGN(sizeof(struct fib_rule_hdr
))
451 + nla_total_size(IFNAMSIZ
) /* FRA_IFNAME */
452 + nla_total_size(4) /* FRA_PRIORITY */
453 + nla_total_size(4) /* FRA_TABLE */
454 + nla_total_size(4) /* FRA_FWMARK */
455 + nla_total_size(4); /* FRA_FWMASK */
457 if (ops
->nlmsg_payload
)
458 payload
+= ops
->nlmsg_payload(rule
);
463 static int fib_nl_fill_rule(struct sk_buff
*skb
, struct fib_rule
*rule
,
464 u32 pid
, u32 seq
, int type
, int flags
,
465 struct fib_rules_ops
*ops
)
467 struct nlmsghdr
*nlh
;
468 struct fib_rule_hdr
*frh
;
470 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*frh
), flags
);
474 frh
= nlmsg_data(nlh
);
475 frh
->table
= rule
->table
;
476 NLA_PUT_U32(skb
, FRA_TABLE
, rule
->table
);
479 frh
->action
= rule
->action
;
480 frh
->flags
= rule
->flags
;
482 if (rule
->action
== FR_ACT_GOTO
&& rule
->ctarget
== NULL
)
483 frh
->flags
|= FIB_RULE_UNRESOLVED
;
485 if (rule
->ifname
[0]) {
486 NLA_PUT_STRING(skb
, FRA_IFNAME
, rule
->ifname
);
488 if (rule
->ifindex
== -1)
489 frh
->flags
|= FIB_RULE_DEV_DETACHED
;
493 NLA_PUT_U32(skb
, FRA_PRIORITY
, rule
->pref
);
496 NLA_PUT_U32(skb
, FRA_FWMARK
, rule
->mark
);
498 if (rule
->mark_mask
|| rule
->mark
)
499 NLA_PUT_U32(skb
, FRA_FWMASK
, rule
->mark_mask
);
502 NLA_PUT_U32(skb
, FRA_GOTO
, rule
->target
);
504 if (ops
->fill(rule
, skb
, nlh
, frh
) < 0)
505 goto nla_put_failure
;
507 return nlmsg_end(skb
, nlh
);
510 nlmsg_cancel(skb
, nlh
);
514 static int dump_rules(struct sk_buff
*skb
, struct netlink_callback
*cb
,
515 struct fib_rules_ops
*ops
)
518 struct fib_rule
*rule
;
520 list_for_each_entry(rule
, &ops
->rules_list
, list
) {
521 if (idx
< cb
->args
[1])
524 if (fib_nl_fill_rule(skb
, rule
, NETLINK_CB(cb
->skb
).pid
,
525 cb
->nlh
->nlmsg_seq
, RTM_NEWRULE
,
526 NLM_F_MULTI
, ops
) < 0)
537 static int fib_nl_dumprule(struct sk_buff
*skb
, struct netlink_callback
*cb
)
539 struct fib_rules_ops
*ops
;
542 family
= rtnl_msg_family(cb
->nlh
);
543 if (family
!= AF_UNSPEC
) {
544 /* Protocol specific dump request */
545 ops
= lookup_rules_ops(family
);
547 return -EAFNOSUPPORT
;
549 return dump_rules(skb
, cb
, ops
);
553 list_for_each_entry_rcu(ops
, &rules_ops
, list
) {
554 if (idx
< cb
->args
[0] || !try_module_get(ops
->owner
))
557 if (dump_rules(skb
, cb
, ops
) < 0)
570 static void notify_rule_change(int event
, struct fib_rule
*rule
,
571 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
577 skb
= nlmsg_new(fib_rule_nlmsg_size(ops
, rule
), GFP_KERNEL
);
581 err
= fib_nl_fill_rule(skb
, rule
, pid
, nlh
->nlmsg_seq
, event
, 0, ops
);
583 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
584 WARN_ON(err
== -EMSGSIZE
);
588 err
= rtnl_notify(skb
, pid
, ops
->nlgroup
, nlh
, GFP_KERNEL
);
591 rtnl_set_sk_err(ops
->nlgroup
, err
);
594 static void attach_rules(struct list_head
*rules
, struct net_device
*dev
)
596 struct fib_rule
*rule
;
598 list_for_each_entry(rule
, rules
, list
) {
599 if (rule
->ifindex
== -1 &&
600 strcmp(dev
->name
, rule
->ifname
) == 0)
601 rule
->ifindex
= dev
->ifindex
;
605 static void detach_rules(struct list_head
*rules
, struct net_device
*dev
)
607 struct fib_rule
*rule
;
609 list_for_each_entry(rule
, rules
, list
)
610 if (rule
->ifindex
== dev
->ifindex
)
615 static int fib_rules_event(struct notifier_block
*this, unsigned long event
,
618 struct net_device
*dev
= ptr
;
619 struct fib_rules_ops
*ops
;
625 case NETDEV_REGISTER
:
626 list_for_each_entry(ops
, &rules_ops
, list
)
627 attach_rules(&ops
->rules_list
, dev
);
630 case NETDEV_UNREGISTER
:
631 list_for_each_entry(ops
, &rules_ops
, list
)
632 detach_rules(&ops
->rules_list
, dev
);
641 static struct notifier_block fib_rules_notifier
= {
642 .notifier_call
= fib_rules_event
,
645 static int __init
fib_rules_init(void)
647 rtnl_register(PF_UNSPEC
, RTM_NEWRULE
, fib_nl_newrule
, NULL
);
648 rtnl_register(PF_UNSPEC
, RTM_DELRULE
, fib_nl_delrule
, NULL
);
649 rtnl_register(PF_UNSPEC
, RTM_GETRULE
, NULL
, fib_nl_dumprule
);
651 return register_netdevice_notifier(&fib_rules_notifier
);
654 subsys_initcall(fib_rules_init
);