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 static LIST_HEAD(rules_ops
);
19 static DEFINE_SPINLOCK(rules_mod_lock
);
21 static void notify_rule_change(int event
, struct fib_rule
*rule
,
22 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
25 static struct fib_rules_ops
*lookup_rules_ops(int family
)
27 struct fib_rules_ops
*ops
;
30 list_for_each_entry_rcu(ops
, &rules_ops
, list
) {
31 if (ops
->family
== family
) {
32 if (!try_module_get(ops
->owner
))
43 static void rules_ops_put(struct fib_rules_ops
*ops
)
46 module_put(ops
->owner
);
49 static void flush_route_cache(struct fib_rules_ops
*ops
)
55 int fib_rules_register(struct fib_rules_ops
*ops
)
58 struct fib_rules_ops
*o
;
60 if (ops
->rule_size
< sizeof(struct fib_rule
))
63 if (ops
->match
== NULL
|| ops
->configure
== NULL
||
64 ops
->compare
== NULL
|| ops
->fill
== NULL
||
68 spin_lock(&rules_mod_lock
);
69 list_for_each_entry(o
, &rules_ops
, list
)
70 if (ops
->family
== o
->family
)
73 list_add_tail_rcu(&ops
->list
, &rules_ops
);
76 spin_unlock(&rules_mod_lock
);
81 EXPORT_SYMBOL_GPL(fib_rules_register
);
83 static void cleanup_ops(struct fib_rules_ops
*ops
)
85 struct fib_rule
*rule
, *tmp
;
87 list_for_each_entry_safe(rule
, tmp
, &ops
->rules_list
, list
) {
88 list_del_rcu(&rule
->list
);
93 int fib_rules_unregister(struct fib_rules_ops
*ops
)
96 struct fib_rules_ops
*o
;
98 spin_lock(&rules_mod_lock
);
99 list_for_each_entry(o
, &rules_ops
, list
) {
101 list_del_rcu(&o
->list
);
109 spin_unlock(&rules_mod_lock
);
116 EXPORT_SYMBOL_GPL(fib_rules_unregister
);
118 static int fib_rule_match(struct fib_rule
*rule
, struct fib_rules_ops
*ops
,
119 struct flowi
*fl
, int flags
)
123 if (rule
->ifindex
&& (rule
->ifindex
!= fl
->iif
))
126 if ((rule
->mark
^ fl
->mark
) & rule
->mark_mask
)
129 ret
= ops
->match(rule
, fl
, flags
);
131 return (rule
->flags
& FIB_RULE_INVERT
) ? !ret
: ret
;
134 int fib_rules_lookup(struct fib_rules_ops
*ops
, struct flowi
*fl
,
135 int flags
, struct fib_lookup_arg
*arg
)
137 struct fib_rule
*rule
;
142 list_for_each_entry_rcu(rule
, &ops
->rules_list
, list
) {
144 if (!fib_rule_match(rule
, ops
, fl
, flags
))
147 if (rule
->action
== FR_ACT_GOTO
) {
148 struct fib_rule
*target
;
150 target
= rcu_dereference(rule
->ctarget
);
151 if (target
== NULL
) {
157 } else if (rule
->action
== FR_ACT_NOP
)
160 err
= ops
->action(rule
, fl
, flags
, arg
);
162 if (err
!= -EAGAIN
) {
176 EXPORT_SYMBOL_GPL(fib_rules_lookup
);
178 static int validate_rulemsg(struct fib_rule_hdr
*frh
, struct nlattr
**tb
,
179 struct fib_rules_ops
*ops
)
184 if (tb
[FRA_SRC
] == NULL
||
185 frh
->src_len
> (ops
->addr_size
* 8) ||
186 nla_len(tb
[FRA_SRC
]) != ops
->addr_size
)
190 if (tb
[FRA_DST
] == NULL
||
191 frh
->dst_len
> (ops
->addr_size
* 8) ||
192 nla_len(tb
[FRA_DST
]) != ops
->addr_size
)
200 static int fib_nl_newrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
202 struct net
*net
= skb
->sk
->sk_net
;
203 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
204 struct fib_rules_ops
*ops
= NULL
;
205 struct fib_rule
*rule
, *r
, *last
= NULL
;
206 struct nlattr
*tb
[FRA_MAX
+1];
207 int err
= -EINVAL
, unresolved
= 0;
209 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
)))
212 ops
= lookup_rules_ops(frh
->family
);
218 err
= nlmsg_parse(nlh
, sizeof(*frh
), tb
, FRA_MAX
, ops
->policy
);
222 err
= validate_rulemsg(frh
, tb
, ops
);
226 rule
= kzalloc(ops
->rule_size
, GFP_KERNEL
);
232 if (tb
[FRA_PRIORITY
])
233 rule
->pref
= nla_get_u32(tb
[FRA_PRIORITY
]);
235 if (tb
[FRA_IFNAME
]) {
236 struct net_device
*dev
;
239 nla_strlcpy(rule
->ifname
, tb
[FRA_IFNAME
], IFNAMSIZ
);
240 dev
= __dev_get_by_name(net
, rule
->ifname
);
242 rule
->ifindex
= dev
->ifindex
;
245 if (tb
[FRA_FWMARK
]) {
246 rule
->mark
= nla_get_u32(tb
[FRA_FWMARK
]);
248 /* compatibility: if the mark value is non-zero all bits
249 * are compared unless a mask is explicitly specified.
251 rule
->mark_mask
= 0xFFFFFFFF;
255 rule
->mark_mask
= nla_get_u32(tb
[FRA_FWMASK
]);
257 rule
->action
= frh
->action
;
258 rule
->flags
= frh
->flags
;
259 rule
->table
= frh_get_table(frh
, tb
);
261 if (!rule
->pref
&& ops
->default_pref
)
262 rule
->pref
= ops
->default_pref();
266 if (rule
->action
!= FR_ACT_GOTO
)
269 rule
->target
= nla_get_u32(tb
[FRA_GOTO
]);
270 /* Backward jumps are prohibited to avoid endless loops */
271 if (rule
->target
<= rule
->pref
)
274 list_for_each_entry(r
, &ops
->rules_list
, list
) {
275 if (r
->pref
== rule
->target
) {
281 if (rule
->ctarget
== NULL
)
283 } else if (rule
->action
== FR_ACT_GOTO
)
286 err
= ops
->configure(rule
, skb
, nlh
, frh
, tb
);
290 list_for_each_entry(r
, &ops
->rules_list
, list
) {
291 if (r
->pref
> rule
->pref
)
298 if (ops
->unresolved_rules
) {
300 * There are unresolved goto rules in the list, check if
301 * any of them are pointing to this new rule.
303 list_for_each_entry(r
, &ops
->rules_list
, list
) {
304 if (r
->action
== FR_ACT_GOTO
&&
305 r
->target
== rule
->pref
) {
306 BUG_ON(r
->ctarget
!= NULL
);
307 rcu_assign_pointer(r
->ctarget
, rule
);
308 if (--ops
->unresolved_rules
== 0)
314 if (rule
->action
== FR_ACT_GOTO
)
315 ops
->nr_goto_rules
++;
318 ops
->unresolved_rules
++;
321 list_add_rcu(&rule
->list
, &last
->list
);
323 list_add_rcu(&rule
->list
, &ops
->rules_list
);
325 notify_rule_change(RTM_NEWRULE
, rule
, ops
, nlh
, NETLINK_CB(skb
).pid
);
326 flush_route_cache(ops
);
337 static int fib_nl_delrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
339 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
340 struct fib_rules_ops
*ops
= NULL
;
341 struct fib_rule
*rule
, *tmp
;
342 struct nlattr
*tb
[FRA_MAX
+1];
345 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
)))
348 ops
= lookup_rules_ops(frh
->family
);
354 err
= nlmsg_parse(nlh
, sizeof(*frh
), tb
, FRA_MAX
, ops
->policy
);
358 err
= validate_rulemsg(frh
, tb
, ops
);
362 list_for_each_entry(rule
, &ops
->rules_list
, list
) {
363 if (frh
->action
&& (frh
->action
!= rule
->action
))
366 if (frh
->table
&& (frh_get_table(frh
, tb
) != rule
->table
))
369 if (tb
[FRA_PRIORITY
] &&
370 (rule
->pref
!= nla_get_u32(tb
[FRA_PRIORITY
])))
373 if (tb
[FRA_IFNAME
] &&
374 nla_strcmp(tb
[FRA_IFNAME
], rule
->ifname
))
377 if (tb
[FRA_FWMARK
] &&
378 (rule
->mark
!= nla_get_u32(tb
[FRA_FWMARK
])))
381 if (tb
[FRA_FWMASK
] &&
382 (rule
->mark_mask
!= nla_get_u32(tb
[FRA_FWMASK
])))
385 if (!ops
->compare(rule
, frh
, tb
))
388 if (rule
->flags
& FIB_RULE_PERMANENT
) {
393 list_del_rcu(&rule
->list
);
395 if (rule
->action
== FR_ACT_GOTO
)
396 ops
->nr_goto_rules
--;
399 * Check if this rule is a target to any of them. If so,
400 * disable them. As this operation is eventually very
401 * expensive, it is only performed if goto rules have
402 * actually been added.
404 if (ops
->nr_goto_rules
> 0) {
405 list_for_each_entry(tmp
, &ops
->rules_list
, list
) {
406 if (tmp
->ctarget
== rule
) {
407 rcu_assign_pointer(tmp
->ctarget
, NULL
);
408 ops
->unresolved_rules
++;
414 notify_rule_change(RTM_DELRULE
, rule
, ops
, nlh
,
415 NETLINK_CB(skb
).pid
);
417 flush_route_cache(ops
);
428 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops
*ops
,
429 struct fib_rule
*rule
)
431 size_t payload
= NLMSG_ALIGN(sizeof(struct fib_rule_hdr
))
432 + nla_total_size(IFNAMSIZ
) /* FRA_IFNAME */
433 + nla_total_size(4) /* FRA_PRIORITY */
434 + nla_total_size(4) /* FRA_TABLE */
435 + nla_total_size(4) /* FRA_FWMARK */
436 + nla_total_size(4); /* FRA_FWMASK */
438 if (ops
->nlmsg_payload
)
439 payload
+= ops
->nlmsg_payload(rule
);
444 static int fib_nl_fill_rule(struct sk_buff
*skb
, struct fib_rule
*rule
,
445 u32 pid
, u32 seq
, int type
, int flags
,
446 struct fib_rules_ops
*ops
)
448 struct nlmsghdr
*nlh
;
449 struct fib_rule_hdr
*frh
;
451 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*frh
), flags
);
455 frh
= nlmsg_data(nlh
);
456 frh
->table
= rule
->table
;
457 NLA_PUT_U32(skb
, FRA_TABLE
, rule
->table
);
460 frh
->action
= rule
->action
;
461 frh
->flags
= rule
->flags
;
463 if (rule
->action
== FR_ACT_GOTO
&& rule
->ctarget
== NULL
)
464 frh
->flags
|= FIB_RULE_UNRESOLVED
;
466 if (rule
->ifname
[0]) {
467 NLA_PUT_STRING(skb
, FRA_IFNAME
, rule
->ifname
);
469 if (rule
->ifindex
== -1)
470 frh
->flags
|= FIB_RULE_DEV_DETACHED
;
474 NLA_PUT_U32(skb
, FRA_PRIORITY
, rule
->pref
);
477 NLA_PUT_U32(skb
, FRA_FWMARK
, rule
->mark
);
479 if (rule
->mark_mask
|| rule
->mark
)
480 NLA_PUT_U32(skb
, FRA_FWMASK
, rule
->mark_mask
);
483 NLA_PUT_U32(skb
, FRA_GOTO
, rule
->target
);
485 if (ops
->fill(rule
, skb
, nlh
, frh
) < 0)
486 goto nla_put_failure
;
488 return nlmsg_end(skb
, nlh
);
491 nlmsg_cancel(skb
, nlh
);
495 static int dump_rules(struct sk_buff
*skb
, struct netlink_callback
*cb
,
496 struct fib_rules_ops
*ops
)
499 struct fib_rule
*rule
;
501 list_for_each_entry(rule
, &ops
->rules_list
, list
) {
502 if (idx
< cb
->args
[1])
505 if (fib_nl_fill_rule(skb
, rule
, NETLINK_CB(cb
->skb
).pid
,
506 cb
->nlh
->nlmsg_seq
, RTM_NEWRULE
,
507 NLM_F_MULTI
, ops
) < 0)
518 static int fib_nl_dumprule(struct sk_buff
*skb
, struct netlink_callback
*cb
)
520 struct fib_rules_ops
*ops
;
523 family
= rtnl_msg_family(cb
->nlh
);
524 if (family
!= AF_UNSPEC
) {
525 /* Protocol specific dump request */
526 ops
= lookup_rules_ops(family
);
528 return -EAFNOSUPPORT
;
530 return dump_rules(skb
, cb
, ops
);
534 list_for_each_entry_rcu(ops
, &rules_ops
, list
) {
535 if (idx
< cb
->args
[0] || !try_module_get(ops
->owner
))
538 if (dump_rules(skb
, cb
, ops
) < 0)
551 static void notify_rule_change(int event
, struct fib_rule
*rule
,
552 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
558 skb
= nlmsg_new(fib_rule_nlmsg_size(ops
, rule
), GFP_KERNEL
);
562 err
= fib_nl_fill_rule(skb
, rule
, pid
, nlh
->nlmsg_seq
, event
, 0, ops
);
564 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
565 WARN_ON(err
== -EMSGSIZE
);
569 err
= rtnl_notify(skb
, pid
, ops
->nlgroup
, nlh
, GFP_KERNEL
);
572 rtnl_set_sk_err(ops
->nlgroup
, err
);
575 static void attach_rules(struct list_head
*rules
, struct net_device
*dev
)
577 struct fib_rule
*rule
;
579 list_for_each_entry(rule
, rules
, list
) {
580 if (rule
->ifindex
== -1 &&
581 strcmp(dev
->name
, rule
->ifname
) == 0)
582 rule
->ifindex
= dev
->ifindex
;
586 static void detach_rules(struct list_head
*rules
, struct net_device
*dev
)
588 struct fib_rule
*rule
;
590 list_for_each_entry(rule
, rules
, list
)
591 if (rule
->ifindex
== dev
->ifindex
)
596 static int fib_rules_event(struct notifier_block
*this, unsigned long event
,
599 struct net_device
*dev
= ptr
;
600 struct fib_rules_ops
*ops
;
602 if (dev
->nd_net
!= &init_net
)
609 case NETDEV_REGISTER
:
610 list_for_each_entry(ops
, &rules_ops
, list
)
611 attach_rules(&ops
->rules_list
, dev
);
614 case NETDEV_UNREGISTER
:
615 list_for_each_entry(ops
, &rules_ops
, list
)
616 detach_rules(&ops
->rules_list
, dev
);
625 static struct notifier_block fib_rules_notifier
= {
626 .notifier_call
= fib_rules_event
,
629 static int __init
fib_rules_init(void)
631 rtnl_register(PF_UNSPEC
, RTM_NEWRULE
, fib_nl_newrule
, NULL
);
632 rtnl_register(PF_UNSPEC
, RTM_DELRULE
, fib_nl_delrule
, NULL
);
633 rtnl_register(PF_UNSPEC
, RTM_GETRULE
, NULL
, fib_nl_dumprule
);
635 return register_netdevice_notifier(&fib_rules_notifier
);
638 subsys_initcall(fib_rules_init
);