Skip to content

Commit

Permalink
fib_rules: add .suppress operation
Browse files Browse the repository at this point in the history
This change adds a new operation to the fib_rules_ops struct; it allows the
suppression of routing decisions if certain criteria are not met by its
results.

The first implemented constraint is a minimum prefix length added to the
structures of routing rules. If a rule is added with a minimum prefix length
>0, only routes meeting this threshold will be considered. Any other (more
general) routing table entries will be ignored.

When configuring a system with multiple network uplinks and default routes, it
is often convinient to reference the main routing table multiple times - but
omitting the default route. Using this patch and a modified "ip" utility, this
can be achieved by using the following command sequence:

  $ ip route add table secuplink default via 10.42.23.1

  $ ip rule add pref 100            table main prefixlength 1
  $ ip rule add pref 150 fwmark 0xA table secuplink

With this setup, packets marked 0xA will be processed by the additional routing
table "secuplink", but only if no suitable route in the main routing table can
be found. By using a minimal prefixlength of 1, the default route (/0) of the
table "main" is hidden to packets processed by rule 100; packets traveling to
destinations with more specific routing entries are processed as usual.

Signed-off-by: Stefan Tomanek <stefan.tomanek@wertarbyte.de>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Stefan Tomanek authored and David S. Miller committed Aug 1, 2013
1 parent 5c15257 commit 7764a45
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions include/net/fib_rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct fib_rule {
u32 pref;
u32 flags;
u32 table;
u8 table_prefixlen_min;
u8 action;
u32 target;
struct fib_rule __rcu *ctarget;
Expand Down Expand Up @@ -46,6 +47,8 @@ struct fib_rules_ops {
int (*action)(struct fib_rule *,
struct flowi *, int,
struct fib_lookup_arg *);
bool (*suppress)(struct fib_rule *,
struct fib_lookup_arg *);
int (*match)(struct fib_rule *,
struct flowi *, int);
int (*configure)(struct fib_rule *,
Expand Down Expand Up @@ -80,6 +83,7 @@ struct fib_rules_ops {
[FRA_FWMARK] = { .type = NLA_U32 }, \
[FRA_FWMASK] = { .type = NLA_U32 }, \
[FRA_TABLE] = { .type = NLA_U32 }, \
[FRA_TABLE_PREFIXLEN_MIN] = { .type = NLA_U8 }, \
[FRA_GOTO] = { .type = NLA_U32 }

static inline void fib_rule_get(struct fib_rule *rule)
Expand Down
2 changes: 1 addition & 1 deletion include/uapi/linux/fib_rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ enum {
FRA_FLOW, /* flow/class id */
FRA_UNUSED6,
FRA_UNUSED7,
FRA_UNUSED8,
FRA_TABLE_PREFIXLEN_MIN,
FRA_TABLE, /* Extended table id */
FRA_FWMASK, /* mask for netfilter mark */
FRA_OIFNAME,
Expand Down
8 changes: 8 additions & 0 deletions net/core/fib_rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
else
err = ops->action(rule, fl, flags, arg);

if (!err && ops->suppress && ops->suppress(rule, arg))
continue;

if (err != -EAGAIN) {
if ((arg->flags & FIB_LOOKUP_NOREF) ||
likely(atomic_inc_not_zero(&rule->refcnt))) {
Expand Down Expand Up @@ -337,6 +340,8 @@ static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh)
rule->action = frh->action;
rule->flags = frh->flags;
rule->table = frh_get_table(frh, tb);
if (tb[FRA_TABLE_PREFIXLEN_MIN])
rule->table_prefixlen_min = nla_get_u8(tb[FRA_TABLE_PREFIXLEN_MIN]);

if (!tb[FRA_PRIORITY] && ops->default_pref)
rule->pref = ops->default_pref(ops);
Expand Down Expand Up @@ -523,6 +528,7 @@ static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
+ nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
+ nla_total_size(4) /* FRA_PRIORITY */
+ nla_total_size(4) /* FRA_TABLE */
+ nla_total_size(1) /* FRA_TABLE_PREFIXLEN_MIN */
+ nla_total_size(4) /* FRA_FWMARK */
+ nla_total_size(4); /* FRA_FWMASK */

Expand All @@ -548,6 +554,8 @@ static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
frh->table = rule->table;
if (nla_put_u32(skb, FRA_TABLE, rule->table))
goto nla_put_failure;
if (nla_put_u8(skb, FRA_TABLE_PREFIXLEN_MIN, rule->table_prefixlen_min))
goto nla_put_failure;
frh->res1 = 0;
frh->res2 = 0;
frh->action = rule->action;
Expand Down
14 changes: 14 additions & 0 deletions net/ipv4/fib_rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
return err;
}

static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
{
/* do not accept result if the route does
* not meet the required prefix length
*/
struct fib_result *result = (struct fib_result *) arg->result;
if (result->prefixlen < rule->table_prefixlen_min) {
if (!(arg->flags & FIB_LOOKUP_NOREF))
fib_info_put(result->fi);
return true;
}
return false;
}

static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
{
Expand Down Expand Up @@ -267,6 +280,7 @@ static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
.rule_size = sizeof(struct fib4_rule),
.addr_size = sizeof(u32),
.action = fib4_rule_action,
.suppress = fib4_rule_suppress,
.match = fib4_rule_match,
.configure = fib4_rule_configure,
.delete = fib4_rule_delete,
Expand Down
13 changes: 13 additions & 0 deletions net/ipv6/fib6_rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
return rt == NULL ? -EAGAIN : 0;
}

static bool fib6_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
{
struct rt6_info *rt = (struct rt6_info *) arg->result;
/* do not accept result if the route does
* not meet the required prefix length
*/
if (rt->rt6i_dst.plen < rule->table_prefixlen_min) {
ip6_rt_put(rt);
return true;
}
return false;
}

static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
{
Expand Down Expand Up @@ -244,6 +256,7 @@ static const struct fib_rules_ops __net_initconst fib6_rules_ops_template = {
.addr_size = sizeof(struct in6_addr),
.action = fib6_rule_action,
.match = fib6_rule_match,
.suppress = fib6_rule_suppress,
.configure = fib6_rule_configure,
.compare = fib6_rule_compare,
.fill = fib6_rule_fill,
Expand Down

0 comments on commit 7764a45

Please sign in to comment.