Blame view
net/ipv6/xfrm6_policy.c
8.52 KB
1da177e4c Linux-2.6.12-rc2 |
1 2 3 4 5 6 7 8 9 10 |
/* * xfrm6_policy.c: based on xfrm4_policy.c * * Authors: * Mitsuru KANDA @USAGI * Kazunori MIYAZAWA @USAGI * Kunihiro Ishiguro <kunihiro@ipinfusion.com> * IPv6 support * YOSHIFUJI Hideaki * Split up af-specific portion |
1ab1457c4 [NET] IPV6: Fix w... |
11 |
* |
1da177e4c Linux-2.6.12-rc2 |
12 |
*/ |
66cdb3ca2 [IPSEC]: Move flo... |
13 14 |
#include <linux/err.h> #include <linux/kernel.h> |
aabc9761b [IPSEC]: Store id... |
15 16 |
#include <linux/netdevice.h> #include <net/addrconf.h> |
45ff5a3f9 [IPSEC]: Set dst-... |
17 |
#include <net/dst.h> |
1da177e4c Linux-2.6.12-rc2 |
18 19 20 21 |
#include <net/xfrm.h> #include <net/ip.h> #include <net/ipv6.h> #include <net/ip6_route.h> |
59fbb3a61 [IPV6] MIP6: Load... |
22 |
#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
2ce4272a6 [IPV6] MIP6: Tran... |
23 24 |
#include <net/mip6.h> #endif |
1da177e4c Linux-2.6.12-rc2 |
25 |
|
1da177e4c Linux-2.6.12-rc2 |
26 |
static struct xfrm_policy_afinfo xfrm6_policy_afinfo; |
c5b3cf46e netns xfrm: ->dst... |
27 |
static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos, |
5e6b930f2 xfrm: Const'ify a... |
28 29 |
const xfrm_address_t *saddr, const xfrm_address_t *daddr) |
1da177e4c Linux-2.6.12-rc2 |
30 |
{ |
7e1dc7b6f net: Use flowi4 a... |
31 |
struct flowi6 fl6; |
66cdb3ca2 [IPSEC]: Move flo... |
32 33 |
struct dst_entry *dst; int err; |
7e1dc7b6f net: Use flowi4 a... |
34 35 |
memset(&fl6, 0, sizeof(fl6)); memcpy(&fl6.daddr, daddr, sizeof(fl6.daddr)); |
66cdb3ca2 [IPSEC]: Move flo... |
36 |
if (saddr) |
7e1dc7b6f net: Use flowi4 a... |
37 |
memcpy(&fl6.saddr, saddr, sizeof(fl6.saddr)); |
66cdb3ca2 [IPSEC]: Move flo... |
38 |
|
4c9483b2f ipv6: Convert to ... |
39 |
dst = ip6_route_output(net, NULL, &fl6); |
66cdb3ca2 [IPSEC]: Move flo... |
40 41 42 |
err = dst->error; if (dst->error) { |
4251320fa [IPV6]: Make sure... |
43 |
dst_release(dst); |
66cdb3ca2 [IPSEC]: Move flo... |
44 45 46 47 |
dst = ERR_PTR(err); } return dst; |
1da177e4c Linux-2.6.12-rc2 |
48 |
} |
fbda33b2b netns xfrm: ->get... |
49 50 |
static int xfrm6_get_saddr(struct net *net, xfrm_address_t *saddr, xfrm_address_t *daddr) |
a1e59abf8 [XFRM]: Fix wildc... |
51 |
{ |
66cdb3ca2 [IPSEC]: Move flo... |
52 |
struct dst_entry *dst; |
191cd5825 netns: Add networ... |
53 |
struct net_device *dev; |
66cdb3ca2 [IPSEC]: Move flo... |
54 |
|
fbda33b2b netns xfrm: ->get... |
55 |
dst = xfrm6_dst_lookup(net, 0, NULL, daddr); |
66cdb3ca2 [IPSEC]: Move flo... |
56 57 |
if (IS_ERR(dst)) return -EHOSTUNREACH; |
191cd5825 netns: Add networ... |
58 59 |
dev = ip6_dst_idev(dst)->dev; ipv6_dev_get_saddr(dev_net(dev), dev, |
7cbca67c0 [IPV6]: Support S... |
60 |
(struct in6_addr *)&daddr->a6, 0, |
5e5f3f0f8 [IPV6] ADDRCONF: ... |
61 |
(struct in6_addr *)&saddr->a6); |
66cdb3ca2 [IPSEC]: Move flo... |
62 63 |
dst_release(dst); return 0; |
a1e59abf8 [XFRM]: Fix wildc... |
64 |
} |
05d840257 xfrm: Mark flowi ... |
65 |
static int xfrm6_get_tos(const struct flowi *fl) |
1da177e4c Linux-2.6.12-rc2 |
66 |
{ |
25ee3286d [IPSEC]: Merge co... |
67 68 |
return 0; } |
1da177e4c Linux-2.6.12-rc2 |
69 |
|
a1b051405 [XFRM] IPv6: Fix ... |
70 71 72 73 74 75 76 77 78 79 80 81 82 |
static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst, int nfheader_len) { if (dst->ops->family == AF_INET6) { struct rt6_info *rt = (struct rt6_info*)dst; if (rt->rt6i_node) path->path_cookie = rt->rt6i_node->fn_sernum; } path->u.rt6.rt6i_nfheader_len = nfheader_len; return 0; } |
87c1e12b5 ipsec: Fix bogus ... |
83 |
static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev, |
0c7b3eefb xfrm: Mark flowi ... |
84 |
const struct flowi *fl) |
25ee3286d [IPSEC]: Merge co... |
85 86 |
{ struct rt6_info *rt = (struct rt6_info*)xdst->route; |
1da177e4c Linux-2.6.12-rc2 |
87 |
|
25ee3286d [IPSEC]: Merge co... |
88 89 |
xdst->u.dst.dev = dev; dev_hold(dev); |
1da177e4c Linux-2.6.12-rc2 |
90 |
|
bc8e4b954 xfrm6: ensure to ... |
91 |
xdst->u.rt6.rt6i_idev = in6_dev_get(dev); |
25ee3286d [IPSEC]: Merge co... |
92 93 |
if (!xdst->u.rt6.rt6i_idev) return -ENODEV; |
1da177e4c Linux-2.6.12-rc2 |
94 |
|
7cc2edb83 xfrm6: Don't forg... |
95 96 97 |
xdst->u.rt6.rt6i_peer = rt->rt6i_peer; if (rt->rt6i_peer) atomic_inc(&rt->rt6i_peer->refcnt); |
25ee3286d [IPSEC]: Merge co... |
98 99 100 101 102 103 |
/* Sheit... I remember I did this right. Apparently, * it was magically lost, so this code needs audit */ xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST | RTF_LOCAL); xdst->u.rt6.rt6i_metric = rt->rt6i_metric; xdst->u.rt6.rt6i_node = rt->rt6i_node; |
a1b051405 [XFRM] IPv6: Fix ... |
104 105 |
if (rt->rt6i_node) xdst->route_cookie = rt->rt6i_node->fn_sernum; |
25ee3286d [IPSEC]: Merge co... |
106 107 108 |
xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway; xdst->u.rt6.rt6i_dst = rt->rt6i_dst; xdst->u.rt6.rt6i_src = rt->rt6i_src; |
1da177e4c Linux-2.6.12-rc2 |
109 |
|
1da177e4c Linux-2.6.12-rc2 |
110 |
return 0; |
1da177e4c Linux-2.6.12-rc2 |
111 112 113 |
} static inline void |
d5422efe6 [IPSEC]: Added xf... |
114 |
_decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse) |
1da177e4c Linux-2.6.12-rc2 |
115 |
{ |
7e1dc7b6f net: Use flowi4 a... |
116 |
struct flowi6 *fl6 = &fl->u.ip6; |
7e3a42a12 xfrm6: handling f... |
117 |
int onlyproto = 0; |
cfe1fc775 [SK_BUFF]: Introd... |
118 |
u16 offset = skb_network_header_len(skb); |
b71d1d426 inet: constify ip... |
119 |
const struct ipv6hdr *hdr = ipv6_hdr(skb); |
e3cae904d [IPV6] XFRM: Don'... |
120 |
struct ipv6_opt_hdr *exthdr; |
d56f90a7c [SK_BUFF]: Introd... |
121 122 |
const unsigned char *nh = skb_network_header(skb); u8 nexthdr = nh[IP6CB(skb)->nhoff]; |
1da177e4c Linux-2.6.12-rc2 |
123 |
|
7e1dc7b6f net: Use flowi4 a... |
124 125 |
memset(fl6, 0, sizeof(struct flowi6)); fl6->flowi6_mark = skb->mark; |
44b451f16 xfrm: fix xfrm by... |
126 |
|
4e3fd7a06 net: remove ipv6_... |
127 128 |
fl6->daddr = reverse ? hdr->saddr : hdr->daddr; fl6->saddr = reverse ? hdr->daddr : hdr->saddr; |
1da177e4c Linux-2.6.12-rc2 |
129 |
|
59cae0092 xfrm6: fix the pr... |
130 131 |
while (nh + offset + 1 < skb->data || pskb_may_pull(skb, nh + offset + 1 - skb->data)) { |
d56f90a7c [SK_BUFF]: Introd... |
132 133 |
nh = skb_network_header(skb); exthdr = (struct ipv6_opt_hdr *)(nh + offset); |
e3cae904d [IPV6] XFRM: Don'... |
134 |
|
1da177e4c Linux-2.6.12-rc2 |
135 |
switch (nexthdr) { |
7e3a42a12 xfrm6: handling f... |
136 137 |
case NEXTHDR_FRAGMENT: onlyproto = 1; |
1da177e4c Linux-2.6.12-rc2 |
138 139 140 141 142 |
case NEXTHDR_ROUTING: case NEXTHDR_HOP: case NEXTHDR_DEST: offset += ipv6_optlen(exthdr); nexthdr = exthdr->nexthdr; |
d56f90a7c [SK_BUFF]: Introd... |
143 |
exthdr = (struct ipv6_opt_hdr *)(nh + offset); |
1da177e4c Linux-2.6.12-rc2 |
144 145 146 |
break; case IPPROTO_UDP: |
ba4e58eca [NET]: Supporting... |
147 |
case IPPROTO_UDPLITE: |
1da177e4c Linux-2.6.12-rc2 |
148 149 |
case IPPROTO_TCP: case IPPROTO_SCTP: |
9e999993c [XFRM]: Handle DC... |
150 |
case IPPROTO_DCCP: |
59cae0092 xfrm6: fix the pr... |
151 152 |
if (!onlyproto && (nh + offset + 4 < skb->data || pskb_may_pull(skb, nh + offset + 4 - skb->data))) { |
8c689a6ea [XFRM]: misc anno... |
153 |
__be16 *ports = (__be16 *)exthdr; |
1da177e4c Linux-2.6.12-rc2 |
154 |
|
1958b856c net: Put fl6_* ma... |
155 156 |
fl6->fl6_sport = ports[!!reverse]; fl6->fl6_dport = ports[!reverse]; |
1da177e4c Linux-2.6.12-rc2 |
157 |
} |
7e1dc7b6f net: Use flowi4 a... |
158 |
fl6->flowi6_proto = nexthdr; |
1da177e4c Linux-2.6.12-rc2 |
159 160 161 |
return; case IPPROTO_ICMPV6: |
7e3a42a12 xfrm6: handling f... |
162 |
if (!onlyproto && pskb_may_pull(skb, nh + offset + 2 - skb->data)) { |
1da177e4c Linux-2.6.12-rc2 |
163 |
u8 *icmp = (u8 *)exthdr; |
1958b856c net: Put fl6_* ma... |
164 165 |
fl6->fl6_icmp_type = icmp[0]; fl6->fl6_icmp_code = icmp[1]; |
1da177e4c Linux-2.6.12-rc2 |
166 |
} |
7e1dc7b6f net: Use flowi4 a... |
167 |
fl6->flowi6_proto = nexthdr; |
1da177e4c Linux-2.6.12-rc2 |
168 |
return; |
59fbb3a61 [IPV6] MIP6: Load... |
169 |
#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
2ce4272a6 [IPV6] MIP6: Tran... |
170 |
case IPPROTO_MH: |
7e3a42a12 xfrm6: handling f... |
171 |
if (!onlyproto && pskb_may_pull(skb, nh + offset + 3 - skb->data)) { |
2ce4272a6 [IPV6] MIP6: Tran... |
172 173 |
struct ip6_mh *mh; mh = (struct ip6_mh *)exthdr; |
1958b856c net: Put fl6_* ma... |
174 |
fl6->fl6_mh_type = mh->ip6mh_type; |
2ce4272a6 [IPV6] MIP6: Tran... |
175 |
} |
7e1dc7b6f net: Use flowi4 a... |
176 |
fl6->flowi6_proto = nexthdr; |
2ce4272a6 [IPV6] MIP6: Tran... |
177 178 |
return; #endif |
1da177e4c Linux-2.6.12-rc2 |
179 180 181 182 183 |
/* XXX Why are there these headers? */ case IPPROTO_AH: case IPPROTO_ESP: case IPPROTO_COMP: default: |
1958b856c net: Put fl6_* ma... |
184 |
fl6->fl6_ipsec_spi = 0; |
7e1dc7b6f net: Use flowi4 a... |
185 |
fl6->flowi6_proto = nexthdr; |
1da177e4c Linux-2.6.12-rc2 |
186 |
return; |
3ff50b799 [NET]: cleanup ex... |
187 |
} |
1da177e4c Linux-2.6.12-rc2 |
188 189 |
} } |
569d36452 [NETNS][DST] dst:... |
190 |
static inline int xfrm6_garbage_collect(struct dst_ops *ops) |
1da177e4c Linux-2.6.12-rc2 |
191 |
{ |
d7c7544c3 netns xfrm: deal ... |
192 193 194 |
struct net *net = container_of(ops, struct net, xfrm.xfrm6_dst_ops); xfrm6_policy_afinfo.garbage_collect(net); |
fc66f95c6 net dst: use a pe... |
195 |
return dst_entries_get_fast(ops) > ops->gc_thresh * 2; |
1da177e4c Linux-2.6.12-rc2 |
196 197 198 199 200 201 202 203 204 |
} static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu) { struct xfrm_dst *xdst = (struct xfrm_dst *)dst; struct dst_entry *path = xdst->route; path->ops->update_pmtu(path, mtu); } |
aabc9761b [IPSEC]: Store id... |
205 206 207 208 209 210 |
static void xfrm6_dst_destroy(struct dst_entry *dst) { struct xfrm_dst *xdst = (struct xfrm_dst *)dst; if (likely(xdst->u.rt6.rt6i_idev)) in6_dev_put(xdst->u.rt6.rt6i_idev); |
62fa8a846 net: Implement re... |
211 |
dst_destroy_metrics_generic(dst); |
7cc2edb83 xfrm6: Don't forg... |
212 213 |
if (likely(xdst->u.rt6.rt6i_peer)) inet_putpeer(xdst->u.rt6.rt6i_peer); |
aabc9761b [IPSEC]: Store id... |
214 215 216 217 218 219 220 221 222 223 224 225 226 |
xfrm_dst_destroy(xdst); } static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, int unregister) { struct xfrm_dst *xdst; if (!unregister) return; xdst = (struct xfrm_dst *)dst; if (xdst->u.rt6.rt6i_idev->dev == dev) { |
5a3e55d68 [NET]: Multiple n... |
227 |
struct inet6_dev *loopback_idev = |
c346dca10 [NET] NETNS: Omit... |
228 |
in6_dev_get(dev_net(dev)->loopback_dev); |
aabc9761b [IPSEC]: Store id... |
229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
BUG_ON(!loopback_idev); do { in6_dev_put(xdst->u.rt6.rt6i_idev); xdst->u.rt6.rt6i_idev = loopback_idev; in6_dev_hold(loopback_idev); xdst = (struct xfrm_dst *)xdst->u.dst.child; } while (xdst->u.dst.xfrm); __in6_dev_put(loopback_idev); } xfrm_dst_ifdown(dst, dev); } |
1da177e4c Linux-2.6.12-rc2 |
243 244 |
static struct dst_ops xfrm6_dst_ops = { .family = AF_INET6, |
09640e636 net: replace uses... |
245 |
.protocol = cpu_to_be16(ETH_P_IPV6), |
1da177e4c Linux-2.6.12-rc2 |
246 247 |
.gc = xfrm6_garbage_collect, .update_pmtu = xfrm6_update_pmtu, |
62fa8a846 net: Implement re... |
248 |
.cow_metrics = dst_cow_metrics_generic, |
aabc9761b [IPSEC]: Store id... |
249 250 |
.destroy = xfrm6_dst_destroy, .ifdown = xfrm6_dst_ifdown, |
862b82c6f [IPSEC]: Merge mo... |
251 |
.local_out = __ip6_local_out, |
1da177e4c Linux-2.6.12-rc2 |
252 |
.gc_thresh = 1024, |
1da177e4c Linux-2.6.12-rc2 |
253 254 255 256 |
}; static struct xfrm_policy_afinfo xfrm6_policy_afinfo = { .family = AF_INET6, |
1da177e4c Linux-2.6.12-rc2 |
257 258 |
.dst_ops = &xfrm6_dst_ops, .dst_lookup = xfrm6_dst_lookup, |
a1e59abf8 [XFRM]: Fix wildc... |
259 |
.get_saddr = xfrm6_get_saddr, |
1da177e4c Linux-2.6.12-rc2 |
260 |
.decode_session = _decode_session6, |
25ee3286d [IPSEC]: Merge co... |
261 |
.get_tos = xfrm6_get_tos, |
a1b051405 [XFRM] IPv6: Fix ... |
262 |
.init_path = xfrm6_init_path, |
25ee3286d [IPSEC]: Merge co... |
263 |
.fill_dst = xfrm6_fill_dst, |
2774c131b xfrm: Handle blac... |
264 |
.blackhole_route = ip6_blackhole_route, |
1da177e4c Linux-2.6.12-rc2 |
265 |
}; |
0013cabab [IPV6]: Make xfrm... |
266 |
static int __init xfrm6_policy_init(void) |
1da177e4c Linux-2.6.12-rc2 |
267 |
{ |
0013cabab [IPV6]: Make xfrm... |
268 |
return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo); |
1da177e4c Linux-2.6.12-rc2 |
269 270 271 272 273 274 |
} static void xfrm6_policy_fini(void) { xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo); } |
db71789c0 xfrm6: Fix xfrm6_... |
275 |
#ifdef CONFIG_SYSCTL |
a44a4a006 xfrm: export xfrm... |
276 277 |
static struct ctl_table xfrm6_policy_table[] = { { |
a44a4a006 xfrm: export xfrm... |
278 |
.procname = "xfrm6_gc_thresh", |
d7c7544c3 netns xfrm: deal ... |
279 |
.data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh, |
a44a4a006 xfrm: export xfrm... |
280 281 282 283 284 285 286 287 |
.maxlen = sizeof(int), .mode = 0644, .proc_handler = proc_dointvec, }, { } }; static struct ctl_table_header *sysctl_hdr; |
db71789c0 xfrm6: Fix xfrm6_... |
288 |
#endif |
a44a4a006 xfrm: export xfrm... |
289 |
|
0013cabab [IPV6]: Make xfrm... |
290 |
int __init xfrm6_init(void) |
1da177e4c Linux-2.6.12-rc2 |
291 |
{ |
0013cabab [IPV6]: Make xfrm... |
292 |
int ret; |
a33bc5c15 xfrm: select sane... |
293 |
unsigned int gc_thresh; |
0013cabab [IPV6]: Make xfrm... |
294 |
|
a33bc5c15 xfrm: select sane... |
295 296 297 298 299 300 301 302 303 304 305 306 307 |
/* * We need a good default value for the xfrm6 gc threshold. * In ipv4 we set it to the route hash table size * 8, which * is half the size of the maximaum route cache for ipv4. It * would be good to do the same thing for v6, except the table is * constructed differently here. Here each table for a net namespace * can have FIB_TABLE_HASHSZ entries, so lets go with the same * computation that we used for ipv4 here. Also, lets keep the initial * gc_thresh to a minimum of 1024, since, the ipv6 route cache defaults * to that as a minimum as well */ gc_thresh = FIB6_TABLE_HASHSZ * 8; xfrm6_dst_ops.gc_thresh = (gc_thresh < 1024) ? 1024 : gc_thresh; |
fc66f95c6 net dst: use a pe... |
308 |
dst_entries_init(&xfrm6_dst_ops); |
d7c7544c3 netns xfrm: deal ... |
309 310 |
ret = xfrm6_policy_init(); |
fc66f95c6 net dst: use a pe... |
311 312 |
if (ret) { dst_entries_destroy(&xfrm6_dst_ops); |
d7c7544c3 netns xfrm: deal ... |
313 |
goto out; |
fc66f95c6 net dst: use a pe... |
314 |
} |
d7c7544c3 netns xfrm: deal ... |
315 316 317 |
ret = xfrm6_state_init(); if (ret) goto out_policy; |
db71789c0 xfrm6: Fix xfrm6_... |
318 |
#ifdef CONFIG_SYSCTL |
a44a4a006 xfrm: export xfrm... |
319 320 |
sysctl_hdr = register_net_sysctl_table(&init_net, net_ipv6_ctl_path, xfrm6_policy_table); |
db71789c0 xfrm6: Fix xfrm6_... |
321 |
#endif |
0013cabab [IPV6]: Make xfrm... |
322 323 324 325 326 |
out: return ret; out_policy: xfrm6_policy_fini(); goto out; |
1da177e4c Linux-2.6.12-rc2 |
327 328 329 330 |
} void xfrm6_fini(void) { |
db71789c0 xfrm6: Fix xfrm6_... |
331 |
#ifdef CONFIG_SYSCTL |
a44a4a006 xfrm: export xfrm... |
332 333 |
if (sysctl_hdr) unregister_net_sysctl_table(sysctl_hdr); |
db71789c0 xfrm6: Fix xfrm6_... |
334 |
#endif |
1da177e4c Linux-2.6.12-rc2 |
335 336 337 |
//xfrm6_input_fini(); xfrm6_policy_fini(); xfrm6_state_fini(); |
fc66f95c6 net dst: use a pe... |
338 |
dst_entries_destroy(&xfrm6_dst_ops); |
1da177e4c Linux-2.6.12-rc2 |
339 |
} |