2 * xfrm6_policy.c: based on xfrm4_policy.c
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
10 * Split up af-specific portion
14 #include <linux/compiler.h>
15 #include <linux/netdevice.h>
16 #include <net/addrconf.h>
20 #include <net/ip6_route.h>
21 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
25 static struct dst_ops xfrm6_dst_ops
;
26 static struct xfrm_policy_afinfo xfrm6_policy_afinfo
;
28 static int xfrm6_dst_lookup(struct xfrm_dst
**xdst
, struct flowi
*fl
)
30 struct dst_entry
*dst
= ip6_route_output(NULL
, fl
);
33 *xdst
= (struct xfrm_dst
*) dst
;
39 static int xfrm6_get_saddr(xfrm_address_t
*saddr
, xfrm_address_t
*daddr
)
42 struct flowi fl_tunnel
= {
45 .daddr
= *(struct in6_addr
*)&daddr
->a6
,
50 if (!xfrm6_dst_lookup((struct xfrm_dst
**)&rt
, &fl_tunnel
)) {
51 ipv6_get_saddr(&rt
->u
.dst
, (struct in6_addr
*)&daddr
->a6
,
52 (struct in6_addr
*)&saddr
->a6
);
53 dst_release(&rt
->u
.dst
);
59 static struct dst_entry
*
60 __xfrm6_find_bundle(struct flowi
*fl
, struct xfrm_policy
*policy
)
62 struct dst_entry
*dst
;
64 /* Still not clear if we should set fl->fl6_{src,dst}... */
65 read_lock_bh(&policy
->lock
);
66 for (dst
= policy
->bundles
; dst
; dst
= dst
->next
) {
67 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
68 struct in6_addr fl_dst_prefix
, fl_src_prefix
;
70 ipv6_addr_prefix(&fl_dst_prefix
,
72 xdst
->u
.rt6
.rt6i_dst
.plen
);
73 ipv6_addr_prefix(&fl_src_prefix
,
75 xdst
->u
.rt6
.rt6i_src
.plen
);
76 if (ipv6_addr_equal(&xdst
->u
.rt6
.rt6i_dst
.addr
, &fl_dst_prefix
) &&
77 ipv6_addr_equal(&xdst
->u
.rt6
.rt6i_src
.addr
, &fl_src_prefix
) &&
78 xfrm_bundle_ok(policy
, xdst
, fl
, AF_INET6
,
79 (xdst
->u
.rt6
.rt6i_dst
.plen
!= 128 ||
80 xdst
->u
.rt6
.rt6i_src
.plen
!= 128))) {
85 read_unlock_bh(&policy
->lock
);
89 static inline struct in6_addr
*
90 __xfrm6_bundle_addr_remote(struct xfrm_state
*x
, struct in6_addr
*addr
)
92 return (x
->type
->remote_addr
) ?
93 (struct in6_addr
*)x
->type
->remote_addr(x
, (xfrm_address_t
*)addr
) :
94 (struct in6_addr
*)&x
->id
.daddr
;
97 static inline struct in6_addr
*
98 __xfrm6_bundle_addr_local(struct xfrm_state
*x
, struct in6_addr
*addr
)
100 return (x
->type
->local_addr
) ?
101 (struct in6_addr
*)x
->type
->local_addr(x
, (xfrm_address_t
*)addr
) :
102 (struct in6_addr
*)&x
->props
.saddr
;
105 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
106 * all the metrics... Shortly, bundle a bundle.
110 __xfrm6_bundle_create(struct xfrm_policy
*policy
, struct xfrm_state
**xfrm
, int nx
,
111 struct flowi
*fl
, struct dst_entry
**dst_p
)
113 struct dst_entry
*dst
, *dst_prev
;
114 struct rt6_info
*rt0
= (struct rt6_info
*)(*dst_p
);
115 struct rt6_info
*rt
= rt0
;
116 struct flowi fl_tunnel
= {
119 .saddr
= fl
->fl6_src
,
120 .daddr
= fl
->fl6_dst
,
129 dst
= dst_prev
= NULL
;
130 dst_hold(&rt
->u
.dst
);
132 for (i
= 0; i
< nx
; i
++) {
133 struct dst_entry
*dst1
= dst_alloc(&xfrm6_dst_ops
);
134 struct xfrm_dst
*xdst
;
136 if (unlikely(dst1
== NULL
)) {
138 dst_release(&rt
->u
.dst
);
145 dst_prev
->child
= dst1
;
146 dst1
->flags
|= DST_NOHASH
;
150 xdst
= (struct xfrm_dst
*)dst1
;
151 xdst
->route
= &rt
->u
.dst
;
152 xdst
->genid
= xfrm
[i
]->genid
;
154 xdst
->route_cookie
= rt
->rt6i_node
->fn_sernum
;
156 dst1
->next
= dst_prev
;
159 if (xfrm
[i
]->type
->flags
& XFRM_TYPE_NON_FRAGMENT
)
160 ((struct rt6_info
*)dst
)->nfheader_len
+=
161 xfrm
[i
]->props
.header_len
;
162 header_len
+= xfrm
[i
]->props
.header_len
;
163 trailer_len
+= xfrm
[i
]->props
.trailer_len
;
165 if (xfrm
[i
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
166 unsigned short encap_family
= xfrm
[i
]->props
.family
;
167 switch(encap_family
) {
169 fl_tunnel
.fl4_dst
= xfrm
[i
]->id
.daddr
.a4
;
170 fl_tunnel
.fl4_src
= xfrm
[i
]->props
.saddr
.a4
;
173 ipv6_addr_copy(&fl_tunnel
.fl6_dst
, __xfrm6_bundle_addr_remote(xfrm
[i
], &fl
->fl6_dst
));
175 ipv6_addr_copy(&fl_tunnel
.fl6_src
, __xfrm6_bundle_addr_local(xfrm
[i
], &fl
->fl6_src
));
181 err
= xfrm_dst_lookup((struct xfrm_dst
**) &rt
,
182 &fl_tunnel
, encap_family
);
186 dst_hold(&rt
->u
.dst
);
189 dst_prev
->child
= &rt
->u
.dst
;
190 dst
->path
= &rt
->u
.dst
;
192 /* Copy neighbour for reachability confirmation */
193 dst
->neighbour
= neigh_clone(rt
->u
.dst
.neighbour
);
196 ((struct xfrm_dst
*)dst
)->path_cookie
= rt
->rt6i_node
->fn_sernum
;
203 for (; dst_prev
!= &rt
->u
.dst
; dst_prev
= dst_prev
->child
) {
204 struct xfrm_dst
*x
= (struct xfrm_dst
*)dst_prev
;
206 dst_prev
->xfrm
= xfrm
[i
++];
207 dst_prev
->dev
= rt
->u
.dst
.dev
;
209 dev_hold(rt
->u
.dst
.dev
);
210 dst_prev
->obsolete
= -1;
211 dst_prev
->flags
|= DST_HOST
;
212 dst_prev
->lastuse
= jiffies
;
213 dst_prev
->header_len
= header_len
;
214 dst_prev
->trailer_len
= trailer_len
;
215 memcpy(&dst_prev
->metrics
, &x
->route
->metrics
, sizeof(dst_prev
->metrics
));
217 dst_prev
->input
= rt
->u
.dst
.input
;
218 dst_prev
->output
= dst_prev
->xfrm
->outer_mode
->afinfo
->output
;
219 /* Sheit... I remember I did this right. Apparently,
220 * it was magically lost, so this code needs audit */
221 x
->u
.rt6
.rt6i_flags
= rt0
->rt6i_flags
&(RTF_ANYCAST
|RTF_LOCAL
);
222 x
->u
.rt6
.rt6i_metric
= rt0
->rt6i_metric
;
223 x
->u
.rt6
.rt6i_node
= rt0
->rt6i_node
;
224 x
->u
.rt6
.rt6i_gateway
= rt0
->rt6i_gateway
;
225 memcpy(&x
->u
.rt6
.rt6i_gateway
, &rt0
->rt6i_gateway
, sizeof(x
->u
.rt6
.rt6i_gateway
));
226 x
->u
.rt6
.rt6i_dst
= rt0
->rt6i_dst
;
227 x
->u
.rt6
.rt6i_src
= rt0
->rt6i_src
;
228 x
->u
.rt6
.rt6i_idev
= rt0
->rt6i_idev
;
229 in6_dev_hold(rt0
->rt6i_idev
);
230 header_len
-= x
->u
.dst
.xfrm
->props
.header_len
;
231 trailer_len
-= x
->u
.dst
.xfrm
->props
.trailer_len
;
244 _decode_session6(struct sk_buff
*skb
, struct flowi
*fl
)
246 u16 offset
= skb_network_header_len(skb
);
247 struct ipv6hdr
*hdr
= ipv6_hdr(skb
);
248 struct ipv6_opt_hdr
*exthdr
;
249 const unsigned char *nh
= skb_network_header(skb
);
250 u8 nexthdr
= nh
[IP6CB(skb
)->nhoff
];
252 memset(fl
, 0, sizeof(struct flowi
));
253 ipv6_addr_copy(&fl
->fl6_dst
, &hdr
->daddr
);
254 ipv6_addr_copy(&fl
->fl6_src
, &hdr
->saddr
);
256 while (pskb_may_pull(skb
, nh
+ offset
+ 1 - skb
->data
)) {
257 nh
= skb_network_header(skb
);
258 exthdr
= (struct ipv6_opt_hdr
*)(nh
+ offset
);
261 case NEXTHDR_ROUTING
:
264 offset
+= ipv6_optlen(exthdr
);
265 nexthdr
= exthdr
->nexthdr
;
266 exthdr
= (struct ipv6_opt_hdr
*)(nh
+ offset
);
270 case IPPROTO_UDPLITE
:
274 if (pskb_may_pull(skb
, nh
+ offset
+ 4 - skb
->data
)) {
275 __be16
*ports
= (__be16
*)exthdr
;
277 fl
->fl_ip_sport
= ports
[0];
278 fl
->fl_ip_dport
= ports
[1];
284 if (pskb_may_pull(skb
, nh
+ offset
+ 2 - skb
->data
)) {
285 u8
*icmp
= (u8
*)exthdr
;
287 fl
->fl_icmp_type
= icmp
[0];
288 fl
->fl_icmp_code
= icmp
[1];
293 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
295 if (pskb_may_pull(skb
, nh
+ offset
+ 3 - skb
->data
)) {
297 mh
= (struct ip6_mh
*)exthdr
;
299 fl
->fl_mh_type
= mh
->ip6mh_type
;
305 /* XXX Why are there these headers? */
310 fl
->fl_ipsec_spi
= 0;
317 static inline int xfrm6_garbage_collect(void)
319 xfrm6_policy_afinfo
.garbage_collect();
320 return (atomic_read(&xfrm6_dst_ops
.entries
) > xfrm6_dst_ops
.gc_thresh
*2);
323 static void xfrm6_update_pmtu(struct dst_entry
*dst
, u32 mtu
)
325 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
326 struct dst_entry
*path
= xdst
->route
;
328 path
->ops
->update_pmtu(path
, mtu
);
331 static void xfrm6_dst_destroy(struct dst_entry
*dst
)
333 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
335 if (likely(xdst
->u
.rt6
.rt6i_idev
))
336 in6_dev_put(xdst
->u
.rt6
.rt6i_idev
);
337 xfrm_dst_destroy(xdst
);
340 static void xfrm6_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
,
343 struct xfrm_dst
*xdst
;
348 xdst
= (struct xfrm_dst
*)dst
;
349 if (xdst
->u
.rt6
.rt6i_idev
->dev
== dev
) {
350 struct inet6_dev
*loopback_idev
= in6_dev_get(init_net
.loopback_dev
);
351 BUG_ON(!loopback_idev
);
354 in6_dev_put(xdst
->u
.rt6
.rt6i_idev
);
355 xdst
->u
.rt6
.rt6i_idev
= loopback_idev
;
356 in6_dev_hold(loopback_idev
);
357 xdst
= (struct xfrm_dst
*)xdst
->u
.dst
.child
;
358 } while (xdst
->u
.dst
.xfrm
);
360 __in6_dev_put(loopback_idev
);
363 xfrm_dst_ifdown(dst
, dev
);
366 static struct dst_ops xfrm6_dst_ops
= {
368 .protocol
= __constant_htons(ETH_P_IPV6
),
369 .gc
= xfrm6_garbage_collect
,
370 .update_pmtu
= xfrm6_update_pmtu
,
371 .destroy
= xfrm6_dst_destroy
,
372 .ifdown
= xfrm6_dst_ifdown
,
374 .entry_size
= sizeof(struct xfrm_dst
),
377 static struct xfrm_policy_afinfo xfrm6_policy_afinfo
= {
379 .dst_ops
= &xfrm6_dst_ops
,
380 .dst_lookup
= xfrm6_dst_lookup
,
381 .get_saddr
= xfrm6_get_saddr
,
382 .find_bundle
= __xfrm6_find_bundle
,
383 .bundle_create
= __xfrm6_bundle_create
,
384 .decode_session
= _decode_session6
,
387 static void __init
xfrm6_policy_init(void)
389 xfrm_policy_register_afinfo(&xfrm6_policy_afinfo
);
392 static void xfrm6_policy_fini(void)
394 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo
);
397 void __init
xfrm6_init(void)
403 void xfrm6_fini(void)
405 //xfrm6_input_fini();