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>
53 return rule->
pref - 1;
71 if (ops->
family == family) {
72 if (!try_module_get(ops->
owner))
86 module_put(ops->
owner);
113 if (ops->family == o->family)
138 err = __fib_rules_register(ops);
153 list_del_rcu(&rule->
list);
163 struct net *net = ops->
fro_net;
171 struct net *net = ops->
fro_net;
174 list_del_rcu(&ops->
list);
175 fib_rules_cleanup_ops(ops);
196 ret = ops->
match(rule, fl, flags);
211 if (!fib_rule_match(rule, ops, fl, flags))
218 if (target ==
NULL) {
227 err = ops->
action(rule, fl, flags, arg);
271 struct net *net = sock_net(skb->
sk);
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);
300 rule->
fr_net = hold_net(net);
303 rule->
pref = nla_get_u32(tb[FRA_PRIORITY]);
326 rule->
mark = nla_get_u32(tb[FRA_FWMARK]);
335 rule->
mark_mask = nla_get_u32(tb[FRA_FWMASK]);
339 rule->
table = frh_get_table(frh, tb);
349 rule->
target = nla_get_u32(tb[FRA_GOTO]);
366 err = ops->
configure(rule, skb, frh, tb);
379 list_add_rcu(&rule->
list, &last->
list);
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);
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);
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] &&
459 if (tb[FRA_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))
479 list_del_rcu(&rule->
list);
507 flush_route_cache(ops);
518 static inline size_t fib_rule_nlmsg_size(
struct fib_rules_ops *ops,
535 static int fib_nl_fill_rule(
struct sk_buff *skb,
struct fib_rule *rule,
542 nlh = nlmsg_put(skb, pid, seq, type,
sizeof(*frh), flags);
546 frh = nlmsg_data(nlh);
550 goto nla_put_failure;
561 if (nla_put_string(skb, FRA_IIFNAME, rule->
iifname))
562 goto nla_put_failure;
568 if (nla_put_string(skb, FRA_OIFNAME, rule->
oifname))
569 goto nla_put_failure;
575 nla_put_u32(skb, FRA_PRIORITY, rule->
pref)) ||
577 nla_put_u32(skb, FRA_FWMARK, 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);
601 if (idx < cb->args[1])
620 struct net *net = sock_net(skb->
sk);
624 family = rtnl_msg_family(cb->
nlh);
627 ops = lookup_rules_ops(net, family);
631 return dump_rules(skb, cb, ops);
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,
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);
707 static int fib_rules_event(
struct notifier_block *
this,
unsigned long event,
711 struct net *net = dev_net(dev);
724 detach_rules(&ops->rules_list, dev);
735 static int __net_init fib_rules_net_init(
struct net *net)
743 .init = fib_rules_net_init,
746 static int __init fib_rules_init(
void)
759 goto fail_unregister;