Blame view
net/ipv6/xfrm6_policy.c
8.52 KB
1da177e4c
|
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
|
11 |
* |
1da177e4c
|
12 |
*/ |
66cdb3ca2
|
13 14 |
#include <linux/err.h> #include <linux/kernel.h> |
aabc9761b
|
15 16 |
#include <linux/netdevice.h> #include <net/addrconf.h> |
45ff5a3f9
|
17 |
#include <net/dst.h> |
1da177e4c
|
18 19 20 21 |
#include <net/xfrm.h> #include <net/ip.h> #include <net/ipv6.h> #include <net/ip6_route.h> |
59fbb3a61
|
22 |
#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
2ce4272a6
|
23 24 |
#include <net/mip6.h> #endif |
1da177e4c
|
25 |
|
1da177e4c
|
26 |
static struct xfrm_policy_afinfo xfrm6_policy_afinfo; |
c5b3cf46e
|
27 |
static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos, |
5e6b930f2
|
28 29 |
const xfrm_address_t *saddr, const xfrm_address_t *daddr) |
1da177e4c
|
30 |
{ |
7e1dc7b6f
|
31 |
struct flowi6 fl6; |
66cdb3ca2
|
32 33 |
struct dst_entry *dst; int err; |
7e1dc7b6f
|
34 35 |
memset(&fl6, 0, sizeof(fl6)); memcpy(&fl6.daddr, daddr, sizeof(fl6.daddr)); |
66cdb3ca2
|
36 |
if (saddr) |
7e1dc7b6f
|
37 |
memcpy(&fl6.saddr, saddr, sizeof(fl6.saddr)); |
66cdb3ca2
|
38 |
|
4c9483b2f
|
39 |
dst = ip6_route_output(net, NULL, &fl6); |
66cdb3ca2
|
40 41 42 |
err = dst->error; if (dst->error) { |
4251320fa
|
43 |
dst_release(dst); |
66cdb3ca2
|
44 45 46 47 |
dst = ERR_PTR(err); } return dst; |
1da177e4c
|
48 |
} |
fbda33b2b
|
49 50 |
static int xfrm6_get_saddr(struct net *net, xfrm_address_t *saddr, xfrm_address_t *daddr) |
a1e59abf8
|
51 |
{ |
66cdb3ca2
|
52 |
struct dst_entry *dst; |
191cd5825
|
53 |
struct net_device *dev; |
66cdb3ca2
|
54 |
|
fbda33b2b
|
55 |
dst = xfrm6_dst_lookup(net, 0, NULL, daddr); |
66cdb3ca2
|
56 57 |
if (IS_ERR(dst)) return -EHOSTUNREACH; |
191cd5825
|
58 59 |
dev = ip6_dst_idev(dst)->dev; ipv6_dev_get_saddr(dev_net(dev), dev, |
7cbca67c0
|
60 |
(struct in6_addr *)&daddr->a6, 0, |
5e5f3f0f8
|
61 |
(struct in6_addr *)&saddr->a6); |
66cdb3ca2
|
62 63 |
dst_release(dst); return 0; |
a1e59abf8
|
64 |
} |
05d840257
|
65 |
static int xfrm6_get_tos(const struct flowi *fl) |
1da177e4c
|
66 |
{ |
25ee3286d
|
67 68 |
return 0; } |
1da177e4c
|
69 |
|
a1b051405
|
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
|
83 |
static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev, |
0c7b3eefb
|
84 |
const struct flowi *fl) |
25ee3286d
|
85 86 |
{ struct rt6_info *rt = (struct rt6_info*)xdst->route; |
1da177e4c
|
87 |
|
25ee3286d
|
88 89 |
xdst->u.dst.dev = dev; dev_hold(dev); |
1da177e4c
|
90 |
|
bc8e4b954
|
91 |
xdst->u.rt6.rt6i_idev = in6_dev_get(dev); |
25ee3286d
|
92 93 |
if (!xdst->u.rt6.rt6i_idev) return -ENODEV; |
1da177e4c
|
94 |
|
7cc2edb83
|
95 96 97 |
xdst->u.rt6.rt6i_peer = rt->rt6i_peer; if (rt->rt6i_peer) atomic_inc(&rt->rt6i_peer->refcnt); |
25ee3286d
|
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
|
104 105 |
if (rt->rt6i_node) xdst->route_cookie = rt->rt6i_node->fn_sernum; |
25ee3286d
|
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
|
109 |
|
1da177e4c
|
110 |
return 0; |
1da177e4c
|
111 112 113 |
} static inline void |
d5422efe6
|
114 |
_decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse) |
1da177e4c
|
115 |
{ |
7e1dc7b6f
|
116 |
struct flowi6 *fl6 = &fl->u.ip6; |
7e3a42a12
|
117 |
int onlyproto = 0; |
cfe1fc775
|
118 |
u16 offset = skb_network_header_len(skb); |
b71d1d426
|
119 |
const struct ipv6hdr *hdr = ipv6_hdr(skb); |
e3cae904d
|
120 |
struct ipv6_opt_hdr *exthdr; |
d56f90a7c
|
121 122 |
const unsigned char *nh = skb_network_header(skb); u8 nexthdr = nh[IP6CB(skb)->nhoff]; |
1da177e4c
|
123 |
|
7e1dc7b6f
|
124 125 |
memset(fl6, 0, sizeof(struct flowi6)); fl6->flowi6_mark = skb->mark; |
44b451f16
|
126 |
|
4e3fd7a06
|
127 128 |
fl6->daddr = reverse ? hdr->saddr : hdr->daddr; fl6->saddr = reverse ? hdr->daddr : hdr->saddr; |
1da177e4c
|
129 |
|
59cae0092
|
130 131 |
while (nh + offset + 1 < skb->data || pskb_may_pull(skb, nh + offset + 1 - skb->data)) { |
d56f90a7c
|
132 133 |
nh = skb_network_header(skb); exthdr = (struct ipv6_opt_hdr *)(nh + offset); |
e3cae904d
|
134 |
|
1da177e4c
|
135 |
switch (nexthdr) { |
7e3a42a12
|
136 137 |
case NEXTHDR_FRAGMENT: onlyproto = 1; |
1da177e4c
|
138 139 140 141 142 |
case NEXTHDR_ROUTING: case NEXTHDR_HOP: case NEXTHDR_DEST: offset += ipv6_optlen(exthdr); nexthdr = exthdr->nexthdr; |
d56f90a7c
|
143 |
exthdr = (struct ipv6_opt_hdr *)(nh + offset); |
1da177e4c
|
144 145 146 |
break; case IPPROTO_UDP: |
ba4e58eca
|
147 |
case IPPROTO_UDPLITE: |
1da177e4c
|
148 149 |
case IPPROTO_TCP: case IPPROTO_SCTP: |
9e999993c
|
150 |
case IPPROTO_DCCP: |
59cae0092
|
151 152 |
if (!onlyproto && (nh + offset + 4 < skb->data || pskb_may_pull(skb, nh + offset + 4 - skb->data))) { |
8c689a6ea
|
153 |
__be16 *ports = (__be16 *)exthdr; |
1da177e4c
|
154 |
|
1958b856c
|
155 156 |
fl6->fl6_sport = ports[!!reverse]; fl6->fl6_dport = ports[!reverse]; |
1da177e4c
|
157 |
} |
7e1dc7b6f
|
158 |
fl6->flowi6_proto = nexthdr; |
1da177e4c
|
159 160 161 |
return; case IPPROTO_ICMPV6: |
7e3a42a12
|
162 |
if (!onlyproto && pskb_may_pull(skb, nh + offset + 2 - skb->data)) { |
1da177e4c
|
163 |
u8 *icmp = (u8 *)exthdr; |
1958b856c
|
164 165 |
fl6->fl6_icmp_type = icmp[0]; fl6->fl6_icmp_code = icmp[1]; |
1da177e4c
|
166 |
} |
7e1dc7b6f
|
167 |
fl6->flowi6_proto = nexthdr; |
1da177e4c
|
168 |
return; |
59fbb3a61
|
169 |
#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
2ce4272a6
|
170 |
case IPPROTO_MH: |
7e3a42a12
|
171 |
if (!onlyproto && pskb_may_pull(skb, nh + offset + 3 - skb->data)) { |
2ce4272a6
|
172 173 |
struct ip6_mh *mh; mh = (struct ip6_mh *)exthdr; |
1958b856c
|
174 |
fl6->fl6_mh_type = mh->ip6mh_type; |
2ce4272a6
|
175 |
} |
7e1dc7b6f
|
176 |
fl6->flowi6_proto = nexthdr; |
2ce4272a6
|
177 178 |
return; #endif |
1da177e4c
|
179 180 181 182 183 |
/* XXX Why are there these headers? */ case IPPROTO_AH: case IPPROTO_ESP: case IPPROTO_COMP: default: |
1958b856c
|
184 |
fl6->fl6_ipsec_spi = 0; |
7e1dc7b6f
|
185 |
fl6->flowi6_proto = nexthdr; |
1da177e4c
|
186 |
return; |
3ff50b799
|
187 |
} |
1da177e4c
|
188 189 |
} } |
569d36452
|
190 |
static inline int xfrm6_garbage_collect(struct dst_ops *ops) |
1da177e4c
|
191 |
{ |
d7c7544c3
|
192 193 194 |
struct net *net = container_of(ops, struct net, xfrm.xfrm6_dst_ops); xfrm6_policy_afinfo.garbage_collect(net); |
fc66f95c6
|
195 |
return dst_entries_get_fast(ops) > ops->gc_thresh * 2; |
1da177e4c
|
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
|
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
|
211 |
dst_destroy_metrics_generic(dst); |
7cc2edb83
|
212 213 |
if (likely(xdst->u.rt6.rt6i_peer)) inet_putpeer(xdst->u.rt6.rt6i_peer); |
aabc9761b
|
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
|
227 |
struct inet6_dev *loopback_idev = |
c346dca10
|
228 |
in6_dev_get(dev_net(dev)->loopback_dev); |
aabc9761b
|
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
|
243 244 |
static struct dst_ops xfrm6_dst_ops = { .family = AF_INET6, |
09640e636
|
245 |
.protocol = cpu_to_be16(ETH_P_IPV6), |
1da177e4c
|
246 247 |
.gc = xfrm6_garbage_collect, .update_pmtu = xfrm6_update_pmtu, |
62fa8a846
|
248 |
.cow_metrics = dst_cow_metrics_generic, |
aabc9761b
|
249 250 |
.destroy = xfrm6_dst_destroy, .ifdown = xfrm6_dst_ifdown, |
862b82c6f
|
251 |
.local_out = __ip6_local_out, |
1da177e4c
|
252 |
.gc_thresh = 1024, |
1da177e4c
|
253 254 255 256 |
}; static struct xfrm_policy_afinfo xfrm6_policy_afinfo = { .family = AF_INET6, |
1da177e4c
|
257 258 |
.dst_ops = &xfrm6_dst_ops, .dst_lookup = xfrm6_dst_lookup, |
a1e59abf8
|
259 |
.get_saddr = xfrm6_get_saddr, |
1da177e4c
|
260 |
.decode_session = _decode_session6, |
25ee3286d
|
261 |
.get_tos = xfrm6_get_tos, |
a1b051405
|
262 |
.init_path = xfrm6_init_path, |
25ee3286d
|
263 |
.fill_dst = xfrm6_fill_dst, |
2774c131b
|
264 |
.blackhole_route = ip6_blackhole_route, |
1da177e4c
|
265 |
}; |
0013cabab
|
266 |
static int __init xfrm6_policy_init(void) |
1da177e4c
|
267 |
{ |
0013cabab
|
268 |
return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo); |
1da177e4c
|
269 270 271 272 273 274 |
} static void xfrm6_policy_fini(void) { xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo); } |
db71789c0
|
275 |
#ifdef CONFIG_SYSCTL |
a44a4a006
|
276 277 |
static struct ctl_table xfrm6_policy_table[] = { { |
a44a4a006
|
278 |
.procname = "xfrm6_gc_thresh", |
d7c7544c3
|
279 |
.data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh, |
a44a4a006
|
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
|
288 |
#endif |
a44a4a006
|
289 |
|
0013cabab
|
290 |
int __init xfrm6_init(void) |
1da177e4c
|
291 |
{ |
0013cabab
|
292 |
int ret; |
a33bc5c15
|
293 |
unsigned int gc_thresh; |
0013cabab
|
294 |
|
a33bc5c15
|
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
|
308 |
dst_entries_init(&xfrm6_dst_ops); |
d7c7544c3
|
309 310 |
ret = xfrm6_policy_init(); |
fc66f95c6
|
311 312 |
if (ret) { dst_entries_destroy(&xfrm6_dst_ops); |
d7c7544c3
|
313 |
goto out; |
fc66f95c6
|
314 |
} |
d7c7544c3
|
315 316 317 |
ret = xfrm6_state_init(); if (ret) goto out_policy; |
db71789c0
|
318 |
#ifdef CONFIG_SYSCTL |
a44a4a006
|
319 320 |
sysctl_hdr = register_net_sysctl_table(&init_net, net_ipv6_ctl_path, xfrm6_policy_table); |
db71789c0
|
321 |
#endif |
0013cabab
|
322 323 324 325 326 |
out: return ret; out_policy: xfrm6_policy_fini(); goto out; |
1da177e4c
|
327 328 329 330 |
} void xfrm6_fini(void) { |
db71789c0
|
331 |
#ifdef CONFIG_SYSCTL |
a44a4a006
|
332 333 |
if (sysctl_hdr) unregister_net_sysctl_table(sysctl_hdr); |
db71789c0
|
334 |
#endif |
1da177e4c
|
335 336 337 |
//xfrm6_input_fini(); xfrm6_policy_fini(); xfrm6_state_fini(); |
fc66f95c6
|
338 |
dst_entries_destroy(&xfrm6_dst_ops); |
1da177e4c
|
339 |
} |