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 int fib_rules_register(struct fib_rules_ops
*ops
)
50 struct fib_rules_ops
*o
;
52 if (ops
->rule_size
< sizeof(struct fib_rule
))
55 if (ops
->match
== NULL
|| ops
->configure
== NULL
||
56 ops
->compare
== NULL
|| ops
->fill
== NULL
||
60 spin_lock(&rules_mod_lock
);
61 list_for_each_entry(o
, &rules_ops
, list
)
62 if (ops
->family
== o
->family
)
65 list_add_tail_rcu(&ops
->list
, &rules_ops
);
68 spin_unlock(&rules_mod_lock
);
73 EXPORT_SYMBOL_GPL(fib_rules_register
);
75 static void cleanup_ops(struct fib_rules_ops
*ops
)
77 struct fib_rule
*rule
, *tmp
;
79 list_for_each_entry_safe(rule
, tmp
, ops
->rules_list
, list
) {
80 list_del_rcu(&rule
->list
);
85 int fib_rules_unregister(struct fib_rules_ops
*ops
)
88 struct fib_rules_ops
*o
;
90 spin_lock(&rules_mod_lock
);
91 list_for_each_entry(o
, &rules_ops
, list
) {
93 list_del_rcu(&o
->list
);
101 spin_unlock(&rules_mod_lock
);
108 EXPORT_SYMBOL_GPL(fib_rules_unregister
);
110 int fib_rules_lookup(struct fib_rules_ops
*ops
, struct flowi
*fl
,
111 int flags
, struct fib_lookup_arg
*arg
)
113 struct fib_rule
*rule
;
118 list_for_each_entry_rcu(rule
, ops
->rules_list
, list
) {
119 if (rule
->ifindex
&& (rule
->ifindex
!= fl
->iif
))
122 if ((rule
->mark
^ fl
->mark
) & rule
->mark_mask
)
125 if (!ops
->match(rule
, fl
, flags
))
128 err
= ops
->action(rule
, fl
, flags
, arg
);
129 if (err
!= -EAGAIN
) {
143 EXPORT_SYMBOL_GPL(fib_rules_lookup
);
145 int fib_nl_newrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
147 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
148 struct fib_rules_ops
*ops
= NULL
;
149 struct fib_rule
*rule
, *r
, *last
= NULL
;
150 struct nlattr
*tb
[FRA_MAX
+1];
153 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
)))
156 ops
= lookup_rules_ops(frh
->family
);
162 err
= nlmsg_parse(nlh
, sizeof(*frh
), tb
, FRA_MAX
, ops
->policy
);
166 rule
= kzalloc(ops
->rule_size
, GFP_KERNEL
);
172 if (tb
[FRA_PRIORITY
])
173 rule
->pref
= nla_get_u32(tb
[FRA_PRIORITY
]);
175 if (tb
[FRA_IFNAME
]) {
176 struct net_device
*dev
;
179 nla_strlcpy(rule
->ifname
, tb
[FRA_IFNAME
], IFNAMSIZ
);
180 dev
= __dev_get_by_name(rule
->ifname
);
182 rule
->ifindex
= dev
->ifindex
;
185 if (tb
[FRA_FWMARK
]) {
186 rule
->mark
= nla_get_u32(tb
[FRA_FWMARK
]);
188 /* compatibility: if the mark value is non-zero all bits
189 * are compared unless a mask is explicitly specified.
191 rule
->mark_mask
= 0xFFFFFFFF;
195 rule
->mark_mask
= nla_get_u32(tb
[FRA_FWMASK
]);
197 rule
->action
= frh
->action
;
198 rule
->flags
= frh
->flags
;
199 rule
->table
= frh_get_table(frh
, tb
);
201 if (!rule
->pref
&& ops
->default_pref
)
202 rule
->pref
= ops
->default_pref();
204 err
= ops
->configure(rule
, skb
, nlh
, frh
, tb
);
208 list_for_each_entry(r
, ops
->rules_list
, list
) {
209 if (r
->pref
> rule
->pref
)
217 list_add_rcu(&rule
->list
, &last
->list
);
219 list_add_rcu(&rule
->list
, ops
->rules_list
);
221 notify_rule_change(RTM_NEWRULE
, rule
, ops
, nlh
, NETLINK_CB(skb
).pid
);
232 int fib_nl_delrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
234 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
235 struct fib_rules_ops
*ops
= NULL
;
236 struct fib_rule
*rule
;
237 struct nlattr
*tb
[FRA_MAX
+1];
240 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
)))
243 ops
= lookup_rules_ops(frh
->family
);
249 err
= nlmsg_parse(nlh
, sizeof(*frh
), tb
, FRA_MAX
, ops
->policy
);
253 list_for_each_entry(rule
, ops
->rules_list
, list
) {
254 if (frh
->action
&& (frh
->action
!= rule
->action
))
257 if (frh
->table
&& (frh_get_table(frh
, tb
) != rule
->table
))
260 if (tb
[FRA_PRIORITY
] &&
261 (rule
->pref
!= nla_get_u32(tb
[FRA_PRIORITY
])))
264 if (tb
[FRA_IFNAME
] &&
265 nla_strcmp(tb
[FRA_IFNAME
], rule
->ifname
))
268 if (tb
[FRA_FWMARK
] &&
269 (rule
->mark
!= nla_get_u32(tb
[FRA_FWMARK
])))
272 if (tb
[FRA_FWMASK
] &&
273 (rule
->mark_mask
!= nla_get_u32(tb
[FRA_FWMASK
])))
276 if (!ops
->compare(rule
, frh
, tb
))
279 if (rule
->flags
& FIB_RULE_PERMANENT
) {
284 list_del_rcu(&rule
->list
);
286 notify_rule_change(RTM_DELRULE
, rule
, ops
, nlh
,
287 NETLINK_CB(skb
).pid
);
299 static int fib_nl_fill_rule(struct sk_buff
*skb
, struct fib_rule
*rule
,
300 u32 pid
, u32 seq
, int type
, int flags
,
301 struct fib_rules_ops
*ops
)
303 struct nlmsghdr
*nlh
;
304 struct fib_rule_hdr
*frh
;
306 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*frh
), flags
);
310 frh
= nlmsg_data(nlh
);
311 frh
->table
= rule
->table
;
312 NLA_PUT_U32(skb
, FRA_TABLE
, rule
->table
);
315 frh
->action
= rule
->action
;
316 frh
->flags
= rule
->flags
;
319 NLA_PUT_STRING(skb
, FRA_IFNAME
, rule
->ifname
);
322 NLA_PUT_U32(skb
, FRA_PRIORITY
, rule
->pref
);
325 NLA_PUT_U32(skb
, FRA_FWMARK
, rule
->mark
);
327 if (rule
->mark_mask
|| rule
->mark
)
328 NLA_PUT_U32(skb
, FRA_FWMASK
, rule
->mark_mask
);
330 if (ops
->fill(rule
, skb
, nlh
, frh
) < 0)
331 goto nla_put_failure
;
333 return nlmsg_end(skb
, nlh
);
336 return nlmsg_cancel(skb
, nlh
);
339 int fib_rules_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
, int family
)
342 struct fib_rule
*rule
;
343 struct fib_rules_ops
*ops
;
345 ops
= lookup_rules_ops(family
);
347 return -EAFNOSUPPORT
;
350 list_for_each_entry(rule
, ops
->rules_list
, list
) {
351 if (idx
< cb
->args
[0])
354 if (fib_nl_fill_rule(skb
, rule
, NETLINK_CB(cb
->skb
).pid
,
355 cb
->nlh
->nlmsg_seq
, RTM_NEWRULE
,
356 NLM_F_MULTI
, ops
) < 0)
368 EXPORT_SYMBOL_GPL(fib_rules_dump
);
370 static void notify_rule_change(int event
, struct fib_rule
*rule
,
371 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
377 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
381 err
= fib_nl_fill_rule(skb
, rule
, pid
, nlh
->nlmsg_seq
, event
, 0, ops
);
387 err
= rtnl_notify(skb
, pid
, ops
->nlgroup
, nlh
, GFP_KERNEL
);
390 rtnl_set_sk_err(ops
->nlgroup
, err
);
393 static void attach_rules(struct list_head
*rules
, struct net_device
*dev
)
395 struct fib_rule
*rule
;
397 list_for_each_entry(rule
, rules
, list
) {
398 if (rule
->ifindex
== -1 &&
399 strcmp(dev
->name
, rule
->ifname
) == 0)
400 rule
->ifindex
= dev
->ifindex
;
404 static void detach_rules(struct list_head
*rules
, struct net_device
*dev
)
406 struct fib_rule
*rule
;
408 list_for_each_entry(rule
, rules
, list
)
409 if (rule
->ifindex
== dev
->ifindex
)
414 static int fib_rules_event(struct notifier_block
*this, unsigned long event
,
417 struct net_device
*dev
= ptr
;
418 struct fib_rules_ops
*ops
;
424 case NETDEV_REGISTER
:
425 list_for_each_entry(ops
, &rules_ops
, list
)
426 attach_rules(ops
->rules_list
, dev
);
429 case NETDEV_UNREGISTER
:
430 list_for_each_entry(ops
, &rules_ops
, list
)
431 detach_rules(ops
->rules_list
, dev
);
440 static struct notifier_block fib_rules_notifier
= {
441 .notifier_call
= fib_rules_event
,
444 static int __init
fib_rules_init(void)
446 return register_netdevice_notifier(&fib_rules_notifier
);
449 subsys_initcall(fib_rules_init
);