2 * net/ipv6/fib6_rules.c IPv6 Routing Policy Rules
4 * Copyright (C)2003-2006 Helsinki University of Technology
5 * Copyright (C)2003-2006 USAGI/WIDE Project
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, version 2.
12 * Thomas Graf <tgraf@suug.ch>
13 * Ville Nuorvala <vnuorval@tcs.hut.fi>
16 #include <linux/netdevice.h>
17 #include <linux/notifier.h>
18 #include <linux/export.h>
20 #include <net/fib_rules.h>
22 #include <net/addrconf.h>
23 #include <net/ip6_route.h>
24 #include <net/netlink.h>
27 struct fib_rule common
;
33 static bool fib6_rule_matchall(const struct fib_rule
*rule
)
35 struct fib6_rule
*r
= container_of(rule
, struct fib6_rule
, common
);
37 if (r
->dst
.plen
|| r
->src
.plen
|| r
->tclass
)
39 return fib_rule_matchall(rule
);
42 bool fib6_rule_default(const struct fib_rule
*rule
)
44 if (!fib6_rule_matchall(rule
) || rule
->action
!= FR_ACT_TO_TBL
||
47 if (rule
->table
!= RT6_TABLE_LOCAL
&& rule
->table
!= RT6_TABLE_MAIN
)
51 EXPORT_SYMBOL_GPL(fib6_rule_default
);
53 int fib6_rules_dump(struct net
*net
, struct notifier_block
*nb
)
55 return fib_rules_dump(net
, nb
, AF_INET6
);
58 unsigned int fib6_rules_seq_read(struct net
*net
)
60 return fib_rules_seq_read(net
, AF_INET6
);
63 struct dst_entry
*fib6_rule_lookup(struct net
*net
, struct flowi6
*fl6
,
64 int flags
, pol_lookup_t lookup
)
66 if (net
->ipv6
.fib6_has_custom_rules
) {
67 struct fib_lookup_arg arg
= {
69 .flags
= FIB_LOOKUP_NOREF
,
72 /* update flow if oif or iif point to device enslaved to l3mdev */
73 l3mdev_update_flow(net
, flowi6_to_flowi(fl6
));
75 fib_rules_lookup(net
->ipv6
.fib6_rules_ops
,
76 flowi6_to_flowi(fl6
), flags
, &arg
);
83 rt
= lookup(net
, net
->ipv6
.fib6_local_tbl
, fl6
, flags
);
84 if (rt
!= net
->ipv6
.ip6_null_entry
&& rt
->dst
.error
!= -EAGAIN
)
87 rt
= lookup(net
, net
->ipv6
.fib6_main_tbl
, fl6
, flags
);
88 if (rt
->dst
.error
!= -EAGAIN
)
93 dst_hold(&net
->ipv6
.ip6_null_entry
->dst
);
94 return &net
->ipv6
.ip6_null_entry
->dst
;
97 static int fib6_rule_action(struct fib_rule
*rule
, struct flowi
*flp
,
98 int flags
, struct fib_lookup_arg
*arg
)
100 struct flowi6
*flp6
= &flp
->u
.ip6
;
101 struct rt6_info
*rt
= NULL
;
102 struct fib6_table
*table
;
103 struct net
*net
= rule
->fr_net
;
104 pol_lookup_t lookup
= arg
->lookup_ptr
;
108 switch (rule
->action
) {
111 case FR_ACT_UNREACHABLE
:
113 rt
= net
->ipv6
.ip6_null_entry
;
116 case FR_ACT_BLACKHOLE
:
118 rt
= net
->ipv6
.ip6_blk_hole_entry
;
120 case FR_ACT_PROHIBIT
:
122 rt
= net
->ipv6
.ip6_prohibit_entry
;
126 tb_id
= fib_rule_get_table(rule
, arg
);
127 table
= fib6_get_table(net
, tb_id
);
133 rt
= lookup(net
, table
, flp6
, flags
);
134 if (rt
!= net
->ipv6
.ip6_null_entry
) {
135 struct fib6_rule
*r
= (struct fib6_rule
*)rule
;
138 * If we need to find a source address for this traffic,
139 * we check the result if it meets requirement of the rule.
141 if ((rule
->flags
& FIB_RULE_FIND_SADDR
) &&
142 r
->src
.plen
&& !(flags
& RT6_LOOKUP_F_HAS_SADDR
)) {
143 struct in6_addr saddr
;
145 if (ipv6_dev_get_saddr(net
,
146 ip6_dst_idev(&rt
->dst
)->dev
,
148 rt6_flags2srcprefs(flags
),
151 if (!ipv6_prefix_equal(&saddr
, &r
->src
.addr
,
173 static bool fib6_rule_suppress(struct fib_rule
*rule
, struct fib_lookup_arg
*arg
)
175 struct rt6_info
*rt
= (struct rt6_info
*) arg
->result
;
176 struct net_device
*dev
= NULL
;
179 dev
= rt
->rt6i_idev
->dev
;
181 /* do not accept result if the route does
182 * not meet the required prefix length
184 if (rt
->rt6i_dst
.plen
<= rule
->suppress_prefixlen
)
187 /* do not accept result if the route uses a device
188 * belonging to a forbidden interface group
190 if (rule
->suppress_ifgroup
!= -1 && dev
&& dev
->group
== rule
->suppress_ifgroup
)
200 static int fib6_rule_match(struct fib_rule
*rule
, struct flowi
*fl
, int flags
)
202 struct fib6_rule
*r
= (struct fib6_rule
*) rule
;
203 struct flowi6
*fl6
= &fl
->u
.ip6
;
206 !ipv6_prefix_equal(&fl6
->daddr
, &r
->dst
.addr
, r
->dst
.plen
))
210 * If FIB_RULE_FIND_SADDR is set and we do not have a
211 * source address for the traffic, we defer check for
215 if (flags
& RT6_LOOKUP_F_HAS_SADDR
) {
216 if (!ipv6_prefix_equal(&fl6
->saddr
, &r
->src
.addr
,
219 } else if (!(r
->common
.flags
& FIB_RULE_FIND_SADDR
))
223 if (r
->tclass
&& r
->tclass
!= ip6_tclass(fl6
->flowlabel
))
229 static const struct nla_policy fib6_rule_policy
[FRA_MAX
+1] = {
233 static int fib6_rule_configure(struct fib_rule
*rule
, struct sk_buff
*skb
,
234 struct fib_rule_hdr
*frh
,
238 struct net
*net
= sock_net(skb
->sk
);
239 struct fib6_rule
*rule6
= (struct fib6_rule
*) rule
;
241 if (rule
->action
== FR_ACT_TO_TBL
&& !rule
->l3mdev
) {
242 if (rule
->table
== RT6_TABLE_UNSPEC
)
245 if (fib6_new_table(net
, rule
->table
) == NULL
) {
252 rule6
->src
.addr
= nla_get_in6_addr(tb
[FRA_SRC
]);
255 rule6
->dst
.addr
= nla_get_in6_addr(tb
[FRA_DST
]);
257 rule6
->src
.plen
= frh
->src_len
;
258 rule6
->dst
.plen
= frh
->dst_len
;
259 rule6
->tclass
= frh
->tos
;
261 net
->ipv6
.fib6_has_custom_rules
= true;
267 static int fib6_rule_compare(struct fib_rule
*rule
, struct fib_rule_hdr
*frh
,
270 struct fib6_rule
*rule6
= (struct fib6_rule
*) rule
;
272 if (frh
->src_len
&& (rule6
->src
.plen
!= frh
->src_len
))
275 if (frh
->dst_len
&& (rule6
->dst
.plen
!= frh
->dst_len
))
278 if (frh
->tos
&& (rule6
->tclass
!= frh
->tos
))
282 nla_memcmp(tb
[FRA_SRC
], &rule6
->src
.addr
, sizeof(struct in6_addr
)))
286 nla_memcmp(tb
[FRA_DST
], &rule6
->dst
.addr
, sizeof(struct in6_addr
)))
292 static int fib6_rule_fill(struct fib_rule
*rule
, struct sk_buff
*skb
,
293 struct fib_rule_hdr
*frh
)
295 struct fib6_rule
*rule6
= (struct fib6_rule
*) rule
;
297 frh
->dst_len
= rule6
->dst
.plen
;
298 frh
->src_len
= rule6
->src
.plen
;
299 frh
->tos
= rule6
->tclass
;
301 if ((rule6
->dst
.plen
&&
302 nla_put_in6_addr(skb
, FRA_DST
, &rule6
->dst
.addr
)) ||
304 nla_put_in6_addr(skb
, FRA_SRC
, &rule6
->src
.addr
)))
305 goto nla_put_failure
;
312 static size_t fib6_rule_nlmsg_payload(struct fib_rule
*rule
)
314 return nla_total_size(16) /* dst */
315 + nla_total_size(16); /* src */
318 static const struct fib_rules_ops __net_initconst fib6_rules_ops_template
= {
320 .rule_size
= sizeof(struct fib6_rule
),
321 .addr_size
= sizeof(struct in6_addr
),
322 .action
= fib6_rule_action
,
323 .match
= fib6_rule_match
,
324 .suppress
= fib6_rule_suppress
,
325 .configure
= fib6_rule_configure
,
326 .compare
= fib6_rule_compare
,
327 .fill
= fib6_rule_fill
,
328 .nlmsg_payload
= fib6_rule_nlmsg_payload
,
329 .nlgroup
= RTNLGRP_IPV6_RULE
,
330 .policy
= fib6_rule_policy
,
331 .owner
= THIS_MODULE
,
332 .fro_net
= &init_net
,
335 static int __net_init
fib6_rules_net_init(struct net
*net
)
337 struct fib_rules_ops
*ops
;
340 ops
= fib_rules_register(&fib6_rules_ops_template
, net
);
344 err
= fib_default_rule_add(ops
, 0, RT6_TABLE_LOCAL
, 0);
346 goto out_fib6_rules_ops
;
348 err
= fib_default_rule_add(ops
, 0x7FFE, RT6_TABLE_MAIN
, 0);
350 goto out_fib6_rules_ops
;
352 net
->ipv6
.fib6_rules_ops
= ops
;
357 fib_rules_unregister(ops
);
361 static void __net_exit
fib6_rules_net_exit(struct net
*net
)
364 fib_rules_unregister(net
->ipv6
.fib6_rules_ops
);
368 static struct pernet_operations fib6_rules_net_ops
= {
369 .init
= fib6_rules_net_init
,
370 .exit
= fib6_rules_net_exit
,
373 int __init
fib6_rules_init(void)
375 return register_pernet_subsys(&fib6_rules_net_ops
);
379 void fib6_rules_cleanup(void)
381 unregister_pernet_subsys(&fib6_rules_net_ops
);