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
;
29 #ifdef CONFIG_IPV6_ROUTE_FWMARK
36 static struct fib_rules_ops fib6_rules_ops
;
38 static struct fib6_rule main_rule
= {
40 .refcnt
= ATOMIC_INIT(2),
42 .action
= FR_ACT_TO_TBL
,
43 .table
= RT6_TABLE_MAIN
,
47 static struct fib6_rule local_rule
= {
49 .refcnt
= ATOMIC_INIT(2),
51 .action
= FR_ACT_TO_TBL
,
52 .table
= RT6_TABLE_LOCAL
,
53 .flags
= FIB_RULE_PERMANENT
,
57 static LIST_HEAD(fib6_rules
);
59 struct dst_entry
*fib6_rule_lookup(struct flowi
*fl
, int flags
,
62 struct fib_lookup_arg arg
= {
66 fib_rules_lookup(&fib6_rules_ops
, fl
, flags
, &arg
);
68 fib_rule_put(arg
.rule
);
71 return (struct dst_entry
*) arg
.result
;
73 dst_hold(&ip6_null_entry
.u
.dst
);
74 return &ip6_null_entry
.u
.dst
;
77 static int fib6_rule_action(struct fib_rule
*rule
, struct flowi
*flp
,
78 int flags
, struct fib_lookup_arg
*arg
)
80 struct rt6_info
*rt
= NULL
;
81 struct fib6_table
*table
;
82 pol_lookup_t lookup
= arg
->lookup_ptr
;
84 switch (rule
->action
) {
87 case FR_ACT_UNREACHABLE
:
91 case FR_ACT_BLACKHOLE
:
92 rt
= &ip6_blk_hole_entry
;
95 rt
= &ip6_prohibit_entry
;
99 table
= fib6_get_table(rule
->table
);
101 rt
= lookup(table
, flp
, flags
);
103 if (rt
!= &ip6_null_entry
)
105 dst_release(&rt
->u
.dst
);
110 dst_hold(&rt
->u
.dst
);
113 return rt
== NULL
? -EAGAIN
: 0;
117 static int fib6_rule_match(struct fib_rule
*rule
, struct flowi
*fl
, int flags
)
119 struct fib6_rule
*r
= (struct fib6_rule
*) rule
;
121 if (!ipv6_prefix_equal(&fl
->fl6_dst
, &r
->dst
.addr
, r
->dst
.plen
))
124 if ((flags
& RT6_LOOKUP_F_HAS_SADDR
) &&
125 !ipv6_prefix_equal(&fl
->fl6_src
, &r
->src
.addr
, r
->src
.plen
))
128 if (r
->tclass
&& r
->tclass
!= ((ntohl(fl
->fl6_flowlabel
) >> 20) & 0xff))
131 #ifdef CONFIG_IPV6_ROUTE_FWMARK
132 if ((r
->fwmark
^ fl
->fl6_fwmark
) & r
->fwmask
)
139 static struct nla_policy fib6_rule_policy
[FRA_MAX
+1] __read_mostly
= {
140 [FRA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
- 1 },
141 [FRA_PRIORITY
] = { .type
= NLA_U32
},
142 [FRA_SRC
] = { .len
= sizeof(struct in6_addr
) },
143 [FRA_DST
] = { .len
= sizeof(struct in6_addr
) },
144 [FRA_FWMARK
] = { .type
= NLA_U32
},
145 [FRA_FWMASK
] = { .type
= NLA_U32
},
146 [FRA_TABLE
] = { .type
= NLA_U32
},
149 static int fib6_rule_configure(struct fib_rule
*rule
, struct sk_buff
*skb
,
150 struct nlmsghdr
*nlh
, struct fib_rule_hdr
*frh
,
154 struct fib6_rule
*rule6
= (struct fib6_rule
*) rule
;
156 if (frh
->src_len
> 128 || frh
->dst_len
> 128 ||
157 (frh
->tos
& ~IPV6_FLOWINFO_MASK
))
160 if (rule
->action
== FR_ACT_TO_TBL
) {
161 if (rule
->table
== RT6_TABLE_UNSPEC
)
164 if (fib6_new_table(rule
->table
) == NULL
) {
171 nla_memcpy(&rule6
->src
.addr
, tb
[FRA_SRC
],
172 sizeof(struct in6_addr
));
175 nla_memcpy(&rule6
->dst
.addr
, tb
[FRA_DST
],
176 sizeof(struct in6_addr
));
178 #ifdef CONFIG_IPV6_ROUTE_FWMARK
179 if (tb
[FRA_FWMARK
]) {
180 rule6
->fwmark
= nla_get_u32(tb
[FRA_FWMARK
]);
183 * if the mark value is non-zero,
184 * all bits are compared by default
185 * unless a mask is explicitly specified.
187 rule6
->fwmask
= 0xFFFFFFFF;
192 rule6
->fwmask
= nla_get_u32(tb
[FRA_FWMASK
]);
195 rule6
->src
.plen
= frh
->src_len
;
196 rule6
->dst
.plen
= frh
->dst_len
;
197 rule6
->tclass
= frh
->tos
;
204 static int fib6_rule_compare(struct fib_rule
*rule
, struct fib_rule_hdr
*frh
,
207 struct fib6_rule
*rule6
= (struct fib6_rule
*) rule
;
209 if (frh
->src_len
&& (rule6
->src
.plen
!= frh
->src_len
))
212 if (frh
->dst_len
&& (rule6
->dst
.plen
!= frh
->dst_len
))
215 if (frh
->tos
&& (rule6
->tclass
!= frh
->tos
))
219 nla_memcmp(tb
[FRA_SRC
], &rule6
->src
.addr
, sizeof(struct in6_addr
)))
223 nla_memcmp(tb
[FRA_DST
], &rule6
->dst
.addr
, sizeof(struct in6_addr
)))
226 #ifdef CONFIG_IPV6_ROUTE_FWMARK
227 if (tb
[FRA_FWMARK
] && (rule6
->fwmark
!= nla_get_u32(tb
[FRA_FWMARK
])))
230 if (tb
[FRA_FWMASK
] && (rule6
->fwmask
!= nla_get_u32(tb
[FRA_FWMASK
])))
237 static int fib6_rule_fill(struct fib_rule
*rule
, struct sk_buff
*skb
,
238 struct nlmsghdr
*nlh
, struct fib_rule_hdr
*frh
)
240 struct fib6_rule
*rule6
= (struct fib6_rule
*) rule
;
242 frh
->family
= AF_INET6
;
243 frh
->dst_len
= rule6
->dst
.plen
;
244 frh
->src_len
= rule6
->src
.plen
;
245 frh
->tos
= rule6
->tclass
;
248 NLA_PUT(skb
, FRA_DST
, sizeof(struct in6_addr
),
252 NLA_PUT(skb
, FRA_SRC
, sizeof(struct in6_addr
),
255 #ifdef CONFIG_IPV6_ROUTE_FWMARK
257 NLA_PUT_U32(skb
, FRA_FWMARK
, rule6
->fwmark
);
259 if (rule6
->fwmask
|| rule6
->fwmark
)
260 NLA_PUT_U32(skb
, FRA_FWMASK
, rule6
->fwmask
);
269 int fib6_rules_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
271 return fib_rules_dump(skb
, cb
, AF_INET6
);
274 static u32
fib6_rule_default_pref(void)
279 static struct fib_rules_ops fib6_rules_ops
= {
281 .rule_size
= sizeof(struct fib6_rule
),
282 .action
= fib6_rule_action
,
283 .match
= fib6_rule_match
,
284 .configure
= fib6_rule_configure
,
285 .compare
= fib6_rule_compare
,
286 .fill
= fib6_rule_fill
,
287 .default_pref
= fib6_rule_default_pref
,
288 .nlgroup
= RTNLGRP_IPV6_RULE
,
289 .policy
= fib6_rule_policy
,
290 .rules_list
= &fib6_rules
,
291 .owner
= THIS_MODULE
,
294 void __init
fib6_rules_init(void)
296 list_add_tail(&local_rule
.common
.list
, &fib6_rules
);
297 list_add_tail(&main_rule
.common
.list
, &fib6_rules
);
299 fib_rules_register(&fib6_rules_ops
);
302 void fib6_rules_cleanup(void)
304 fib_rules_unregister(&fib6_rules_ops
);