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 static void notify_rule_change(int event
, struct fib_rule
*rule
,
20 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
23 static struct fib_rules_ops
*lookup_rules_ops(int family
)
25 struct fib_rules_ops
*ops
;
28 list_for_each_entry_rcu(ops
, &rules_ops
, list
) {
29 if (ops
->family
== family
) {
30 if (!try_module_get(ops
->owner
))
41 static void rules_ops_put(struct fib_rules_ops
*ops
)
44 module_put(ops
->owner
);
47 static void flush_route_cache(struct fib_rules_ops
*ops
)
53 int fib_rules_register(struct fib_rules_ops
*ops
)
56 struct fib_rules_ops
*o
;
58 if (ops
->rule_size
< sizeof(struct fib_rule
))
61 if (ops
->match
== NULL
|| ops
->configure
== NULL
||
62 ops
->compare
== NULL
|| ops
->fill
== NULL
||
66 spin_lock(&rules_mod_lock
);
67 list_for_each_entry(o
, &rules_ops
, list
)
68 if (ops
->family
== o
->family
)
71 list_add_tail_rcu(&ops
->list
, &rules_ops
);
74 spin_unlock(&rules_mod_lock
);
79 EXPORT_SYMBOL_GPL(fib_rules_register
);
81 static void cleanup_ops(struct fib_rules_ops
*ops
)
83 struct fib_rule
*rule
, *tmp
;
85 list_for_each_entry_safe(rule
, tmp
, ops
->rules_list
, list
) {
86 list_del_rcu(&rule
->list
);
91 int fib_rules_unregister(struct fib_rules_ops
*ops
)
94 struct fib_rules_ops
*o
;
96 spin_lock(&rules_mod_lock
);
97 list_for_each_entry(o
, &rules_ops
, list
) {
99 list_del_rcu(&o
->list
);
107 spin_unlock(&rules_mod_lock
);
114 EXPORT_SYMBOL_GPL(fib_rules_unregister
);
116 static int fib_rule_match(struct fib_rule
*rule
, struct fib_rules_ops
*ops
,
117 struct flowi
*fl
, int flags
)
121 if (rule
->ifindex
&& (rule
->ifindex
!= fl
->iif
))
124 if ((rule
->mark
^ fl
->mark
) & rule
->mark_mask
)
127 ret
= ops
->match(rule
, fl
, flags
);
129 return (rule
->flags
& FIB_RULE_INVERT
) ? !ret
: ret
;
132 int fib_rules_lookup(struct fib_rules_ops
*ops
, struct flowi
*fl
,
133 int flags
, struct fib_lookup_arg
*arg
)
135 struct fib_rule
*rule
;
140 list_for_each_entry_rcu(rule
, ops
->rules_list
, list
) {
142 if (!fib_rule_match(rule
, ops
, fl
, flags
))
145 if (rule
->action
== FR_ACT_GOTO
) {
146 struct fib_rule
*target
;
148 target
= rcu_dereference(rule
->ctarget
);
149 if (target
== NULL
) {
155 } else if (rule
->action
== FR_ACT_NOP
)
158 err
= ops
->action(rule
, fl
, flags
, arg
);
160 if (err
!= -EAGAIN
) {
174 EXPORT_SYMBOL_GPL(fib_rules_lookup
);
176 static int validate_rulemsg(struct fib_rule_hdr
*frh
, struct nlattr
**tb
,
177 struct fib_rules_ops
*ops
)
182 if (tb
[FRA_SRC
] == NULL
||
183 frh
->src_len
> (ops
->addr_size
* 8) ||
184 nla_len(tb
[FRA_SRC
]) != ops
->addr_size
)
188 if (tb
[FRA_DST
] == NULL
||
189 frh
->dst_len
> (ops
->addr_size
* 8) ||
190 nla_len(tb
[FRA_DST
]) != ops
->addr_size
)
198 static int fib_nl_newrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
200 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
201 struct fib_rules_ops
*ops
= NULL
;
202 struct fib_rule
*rule
, *r
, *last
= NULL
;
203 struct nlattr
*tb
[FRA_MAX
+1];
204 int err
= -EINVAL
, unresolved
= 0;
206 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
)))
209 ops
= lookup_rules_ops(frh
->family
);
215 err
= nlmsg_parse(nlh
, sizeof(*frh
), tb
, FRA_MAX
, ops
->policy
);
219 err
= validate_rulemsg(frh
, tb
, ops
);
223 rule
= kzalloc(ops
->rule_size
, GFP_KERNEL
);
229 if (tb
[FRA_PRIORITY
])
230 rule
->pref
= nla_get_u32(tb
[FRA_PRIORITY
]);
232 if (tb
[FRA_IFNAME
]) {
233 struct net_device
*dev
;
236 nla_strlcpy(rule
->ifname
, tb
[FRA_IFNAME
], IFNAMSIZ
);
237 dev
= __dev_get_by_name(rule
->ifname
);
239 rule
->ifindex
= dev
->ifindex
;
242 if (tb
[FRA_FWMARK
]) {
243 rule
->mark
= nla_get_u32(tb
[FRA_FWMARK
]);
245 /* compatibility: if the mark value is non-zero all bits
246 * are compared unless a mask is explicitly specified.
248 rule
->mark_mask
= 0xFFFFFFFF;
252 rule
->mark_mask
= nla_get_u32(tb
[FRA_FWMASK
]);
254 rule
->action
= frh
->action
;
255 rule
->flags
= frh
->flags
;
256 rule
->table
= frh_get_table(frh
, tb
);
258 if (!rule
->pref
&& ops
->default_pref
)
259 rule
->pref
= ops
->default_pref();
263 if (rule
->action
!= FR_ACT_GOTO
)
266 rule
->target
= nla_get_u32(tb
[FRA_GOTO
]);
267 /* Backward jumps are prohibited to avoid endless loops */
268 if (rule
->target
<= rule
->pref
)
271 list_for_each_entry(r
, ops
->rules_list
, list
) {
272 if (r
->pref
== rule
->target
) {
278 if (rule
->ctarget
== NULL
)
280 } else if (rule
->action
== FR_ACT_GOTO
)
283 err
= ops
->configure(rule
, skb
, nlh
, frh
, tb
);
287 list_for_each_entry(r
, ops
->rules_list
, list
) {
288 if (r
->pref
> rule
->pref
)
295 if (ops
->unresolved_rules
) {
297 * There are unresolved goto rules in the list, check if
298 * any of them are pointing to this new rule.
300 list_for_each_entry(r
, ops
->rules_list
, list
) {
301 if (r
->action
== FR_ACT_GOTO
&&
302 r
->target
== rule
->pref
) {
303 BUG_ON(r
->ctarget
!= NULL
);
304 rcu_assign_pointer(r
->ctarget
, rule
);
305 if (--ops
->unresolved_rules
== 0)
311 if (rule
->action
== FR_ACT_GOTO
)
312 ops
->nr_goto_rules
++;
315 ops
->unresolved_rules
++;
318 list_add_rcu(&rule
->list
, &last
->list
);
320 list_add_rcu(&rule
->list
, ops
->rules_list
);
322 notify_rule_change(RTM_NEWRULE
, rule
, ops
, nlh
, NETLINK_CB(skb
).pid
);
323 flush_route_cache(ops
);
334 static int fib_nl_delrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
336 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
337 struct fib_rules_ops
*ops
= NULL
;
338 struct fib_rule
*rule
, *tmp
;
339 struct nlattr
*tb
[FRA_MAX
+1];
342 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
)))
345 ops
= lookup_rules_ops(frh
->family
);
351 err
= nlmsg_parse(nlh
, sizeof(*frh
), tb
, FRA_MAX
, ops
->policy
);
355 err
= validate_rulemsg(frh
, tb
, ops
);
359 list_for_each_entry(rule
, ops
->rules_list
, list
) {
360 if (frh
->action
&& (frh
->action
!= rule
->action
))
363 if (frh
->table
&& (frh_get_table(frh
, tb
) != rule
->table
))
366 if (tb
[FRA_PRIORITY
] &&
367 (rule
->pref
!= nla_get_u32(tb
[FRA_PRIORITY
])))
370 if (tb
[FRA_IFNAME
] &&
371 nla_strcmp(tb
[FRA_IFNAME
], rule
->ifname
))
374 if (tb
[FRA_FWMARK
] &&
375 (rule
->mark
!= nla_get_u32(tb
[FRA_FWMARK
])))
378 if (tb
[FRA_FWMASK
] &&
379 (rule
->mark_mask
!= nla_get_u32(tb
[FRA_FWMASK
])))
382 if (!ops
->compare(rule
, frh
, tb
))
385 if (rule
->flags
& FIB_RULE_PERMANENT
) {
390 list_del_rcu(&rule
->list
);
392 if (rule
->action
== FR_ACT_GOTO
)
393 ops
->nr_goto_rules
--;
396 * Check if this rule is a target to any of them. If so,
397 * disable them. As this operation is eventually very
398 * expensive, it is only performed if goto rules have
399 * actually been added.
401 if (ops
->nr_goto_rules
> 0) {
402 list_for_each_entry(tmp
, ops
->rules_list
, list
) {
403 if (tmp
->ctarget
== rule
) {
404 rcu_assign_pointer(tmp
->ctarget
, NULL
);
405 ops
->unresolved_rules
++;
411 notify_rule_change(RTM_DELRULE
, rule
, ops
, nlh
,
412 NETLINK_CB(skb
).pid
);
414 flush_route_cache(ops
);
425 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops
*ops
,
426 struct fib_rule
*rule
)
428 size_t payload
= NLMSG_ALIGN(sizeof(struct fib_rule_hdr
))
429 + nla_total_size(IFNAMSIZ
) /* FRA_IFNAME */
430 + nla_total_size(4) /* FRA_PRIORITY */
431 + nla_total_size(4) /* FRA_TABLE */
432 + nla_total_size(4) /* FRA_FWMARK */
433 + nla_total_size(4); /* FRA_FWMASK */
435 if (ops
->nlmsg_payload
)
436 payload
+= ops
->nlmsg_payload(rule
);
441 static int fib_nl_fill_rule(struct sk_buff
*skb
, struct fib_rule
*rule
,
442 u32 pid
, u32 seq
, int type
, int flags
,
443 struct fib_rules_ops
*ops
)
445 struct nlmsghdr
*nlh
;
446 struct fib_rule_hdr
*frh
;
448 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*frh
), flags
);
452 frh
= nlmsg_data(nlh
);
453 frh
->table
= rule
->table
;
454 NLA_PUT_U32(skb
, FRA_TABLE
, rule
->table
);
457 frh
->action
= rule
->action
;
458 frh
->flags
= rule
->flags
;
460 if (rule
->action
== FR_ACT_GOTO
&& rule
->ctarget
== NULL
)
461 frh
->flags
|= FIB_RULE_UNRESOLVED
;
463 if (rule
->ifname
[0]) {
464 NLA_PUT_STRING(skb
, FRA_IFNAME
, rule
->ifname
);
466 if (rule
->ifindex
== -1)
467 frh
->flags
|= FIB_RULE_DEV_DETACHED
;
471 NLA_PUT_U32(skb
, FRA_PRIORITY
, rule
->pref
);
474 NLA_PUT_U32(skb
, FRA_FWMARK
, rule
->mark
);
476 if (rule
->mark_mask
|| rule
->mark
)
477 NLA_PUT_U32(skb
, FRA_FWMASK
, rule
->mark_mask
);
480 NLA_PUT_U32(skb
, FRA_GOTO
, rule
->target
);
482 if (ops
->fill(rule
, skb
, nlh
, frh
) < 0)
483 goto nla_put_failure
;
485 return nlmsg_end(skb
, nlh
);
488 nlmsg_cancel(skb
, nlh
);
492 static int dump_rules(struct sk_buff
*skb
, struct netlink_callback
*cb
,
493 struct fib_rules_ops
*ops
)
496 struct fib_rule
*rule
;
498 list_for_each_entry(rule
, ops
->rules_list
, list
) {
499 if (idx
< cb
->args
[1])
502 if (fib_nl_fill_rule(skb
, rule
, NETLINK_CB(cb
->skb
).pid
,
503 cb
->nlh
->nlmsg_seq
, RTM_NEWRULE
,
504 NLM_F_MULTI
, ops
) < 0)
515 static int fib_nl_dumprule(struct sk_buff
*skb
, struct netlink_callback
*cb
)
517 struct fib_rules_ops
*ops
;
520 family
= rtnl_msg_family(cb
->nlh
);
521 if (family
!= AF_UNSPEC
) {
522 /* Protocol specific dump request */
523 ops
= lookup_rules_ops(family
);
525 return -EAFNOSUPPORT
;
527 return dump_rules(skb
, cb
, ops
);
531 list_for_each_entry_rcu(ops
, &rules_ops
, list
) {
532 if (idx
< cb
->args
[0] || !try_module_get(ops
->owner
))
535 if (dump_rules(skb
, cb
, ops
) < 0)
548 static void notify_rule_change(int event
, struct fib_rule
*rule
,
549 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
555 skb
= nlmsg_new(fib_rule_nlmsg_size(ops
, rule
), GFP_KERNEL
);
559 err
= fib_nl_fill_rule(skb
, rule
, pid
, nlh
->nlmsg_seq
, event
, 0, ops
);
561 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
562 WARN_ON(err
== -EMSGSIZE
);
566 err
= rtnl_notify(skb
, pid
, ops
->nlgroup
, nlh
, GFP_KERNEL
);
569 rtnl_set_sk_err(ops
->nlgroup
, err
);
572 static void attach_rules(struct list_head
*rules
, struct net_device
*dev
)
574 struct fib_rule
*rule
;
576 list_for_each_entry(rule
, rules
, list
) {
577 if (rule
->ifindex
== -1 &&
578 strcmp(dev
->name
, rule
->ifname
) == 0)
579 rule
->ifindex
= dev
->ifindex
;
583 static void detach_rules(struct list_head
*rules
, struct net_device
*dev
)
585 struct fib_rule
*rule
;
587 list_for_each_entry(rule
, rules
, list
)
588 if (rule
->ifindex
== dev
->ifindex
)
593 static int fib_rules_event(struct notifier_block
*this, unsigned long event
,
596 struct net_device
*dev
= ptr
;
597 struct fib_rules_ops
*ops
;
603 case NETDEV_REGISTER
:
604 list_for_each_entry(ops
, &rules_ops
, list
)
605 attach_rules(ops
->rules_list
, dev
);
608 case NETDEV_UNREGISTER
:
609 list_for_each_entry(ops
, &rules_ops
, list
)
610 detach_rules(ops
->rules_list
, dev
);
619 static struct notifier_block fib_rules_notifier
= {
620 .notifier_call
= fib_rules_event
,
623 static int __init
fib_rules_init(void)
625 rtnl_register(PF_UNSPEC
, RTM_NEWRULE
, fib_nl_newrule
, NULL
);
626 rtnl_register(PF_UNSPEC
, RTM_DELRULE
, fib_nl_delrule
, NULL
);
627 rtnl_register(PF_UNSPEC
, RTM_GETRULE
, NULL
, fib_nl_dumprule
);
629 return register_netdevice_notifier(&fib_rules_notifier
);
632 subsys_initcall(fib_rules_init
);