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/config.h>
17 #include <linux/netdevice.h>
19 #include <net/fib_rules.h>
21 #include <net/ip6_route.h>
22 #include <net/netlink.h>
26 struct fib_rule common
;
32 static struct fib_rules_ops fib6_rules_ops
;
34 static struct fib6_rule main_rule
= {
36 .refcnt
= ATOMIC_INIT(2),
38 .action
= FR_ACT_TO_TBL
,
39 .table
= RT6_TABLE_MAIN
,
43 static struct fib6_rule local_rule
= {
45 .refcnt
= ATOMIC_INIT(2),
47 .action
= FR_ACT_TO_TBL
,
48 .table
= RT6_TABLE_LOCAL
,
49 .flags
= FIB_RULE_PERMANENT
,
53 static LIST_HEAD(fib6_rules
);
55 struct dst_entry
*fib6_rule_lookup(struct flowi
*fl
, int flags
,
58 struct fib_lookup_arg arg
= {
62 fib_rules_lookup(&fib6_rules_ops
, fl
, flags
, &arg
);
64 fib_rule_put(arg
.rule
);
67 return (struct dst_entry
*) arg
.result
;
69 dst_hold(&ip6_null_entry
.u
.dst
);
70 return &ip6_null_entry
.u
.dst
;
73 static int fib6_rule_action(struct fib_rule
*rule
, struct flowi
*flp
,
74 int flags
, struct fib_lookup_arg
*arg
)
76 struct rt6_info
*rt
= NULL
;
77 struct fib6_table
*table
;
78 pol_lookup_t lookup
= arg
->lookup_ptr
;
80 switch (rule
->action
) {
83 case FR_ACT_UNREACHABLE
:
87 case FR_ACT_BLACKHOLE
:
88 rt
= &ip6_blk_hole_entry
;
91 rt
= &ip6_prohibit_entry
;
95 table
= fib6_get_table(rule
->table
);
97 rt
= lookup(table
, flp
, flags
);
99 if (rt
!= &ip6_null_entry
)
101 dst_release(&rt
->u
.dst
);
106 dst_hold(&rt
->u
.dst
);
109 return rt
== NULL
? -EAGAIN
: 0;
113 static int fib6_rule_match(struct fib_rule
*rule
, struct flowi
*fl
, int flags
)
115 struct fib6_rule
*r
= (struct fib6_rule
*) rule
;
117 if (!ipv6_prefix_equal(&fl
->fl6_dst
, &r
->dst
.addr
, r
->dst
.plen
))
120 if ((flags
& RT6_LOOKUP_F_HAS_SADDR
) &&
121 !ipv6_prefix_equal(&fl
->fl6_src
, &r
->src
.addr
, r
->src
.plen
))
127 static struct nla_policy fib6_rule_policy
[RTA_MAX
+1] __read_mostly
= {
128 [FRA_IFNAME
] = { .type
= NLA_STRING
},
129 [FRA_PRIORITY
] = { .type
= NLA_U32
},
130 [FRA_SRC
] = { .minlen
= sizeof(struct in6_addr
) },
131 [FRA_DST
] = { .minlen
= sizeof(struct in6_addr
) },
132 [FRA_TABLE
] = { .type
= NLA_U32
},
135 static int fib6_rule_configure(struct fib_rule
*rule
, struct sk_buff
*skb
,
136 struct nlmsghdr
*nlh
, struct fib_rule_hdr
*frh
,
140 struct fib6_rule
*rule6
= (struct fib6_rule
*) rule
;
142 if (frh
->src_len
> 128 || frh
->dst_len
> 128 ||
143 (frh
->tos
& ~IPV6_FLOWINFO_MASK
))
146 if (rule
->action
== FR_ACT_TO_TBL
) {
147 if (rule
->table
== RT6_TABLE_UNSPEC
)
150 if (fib6_new_table(rule
->table
) == NULL
) {
157 nla_memcpy(&rule6
->src
.addr
, tb
[FRA_SRC
],
158 sizeof(struct in6_addr
));
161 nla_memcpy(&rule6
->dst
.addr
, tb
[FRA_DST
],
162 sizeof(struct in6_addr
));
164 rule6
->src
.plen
= frh
->src_len
;
165 rule6
->dst
.plen
= frh
->dst_len
;
166 rule6
->tclass
= frh
->tos
;
173 static int fib6_rule_compare(struct fib_rule
*rule
, struct fib_rule_hdr
*frh
,
176 struct fib6_rule
*rule6
= (struct fib6_rule
*) rule
;
178 if (frh
->src_len
&& (rule6
->src
.plen
!= frh
->src_len
))
181 if (frh
->dst_len
&& (rule6
->dst
.plen
!= frh
->dst_len
))
184 if (frh
->tos
&& (rule6
->tclass
!= frh
->tos
))
188 nla_memcmp(tb
[FRA_SRC
], &rule6
->src
.addr
, sizeof(struct in6_addr
)))
192 nla_memcmp(tb
[FRA_DST
], &rule6
->dst
.addr
, sizeof(struct in6_addr
)))
198 static int fib6_rule_fill(struct fib_rule
*rule
, struct sk_buff
*skb
,
199 struct nlmsghdr
*nlh
, struct fib_rule_hdr
*frh
)
201 struct fib6_rule
*rule6
= (struct fib6_rule
*) rule
;
203 frh
->family
= AF_INET6
;
204 frh
->dst_len
= rule6
->dst
.plen
;
205 frh
->src_len
= rule6
->src
.plen
;
206 frh
->tos
= rule6
->tclass
;
209 NLA_PUT(skb
, FRA_DST
, sizeof(struct in6_addr
),
213 NLA_PUT(skb
, FRA_SRC
, sizeof(struct in6_addr
),
222 int fib6_rules_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
224 return fib_rules_dump(skb
, cb
, AF_INET6
);
227 static u32
fib6_rule_default_pref(void)
232 static struct fib_rules_ops fib6_rules_ops
= {
234 .rule_size
= sizeof(struct fib6_rule
),
235 .action
= fib6_rule_action
,
236 .match
= fib6_rule_match
,
237 .configure
= fib6_rule_configure
,
238 .compare
= fib6_rule_compare
,
239 .fill
= fib6_rule_fill
,
240 .default_pref
= fib6_rule_default_pref
,
241 .nlgroup
= RTNLGRP_IPV6_RULE
,
242 .policy
= fib6_rule_policy
,
243 .rules_list
= &fib6_rules
,
244 .owner
= THIS_MODULE
,
247 void __init
fib6_rules_init(void)
249 list_add_tail(&local_rule
.common
.list
, &fib6_rules
);
250 list_add_tail(&main_rule
.common
.list
, &fib6_rules
);
252 fib_rules_register(&fib6_rules_ops
);
255 void fib6_rules_cleanup(void)
257 fib_rules_unregister(&fib6_rules_ops
);