5 * Kazunori MIYAZAWA @USAGI
6 * YOSHIFUJI Hideaki @USAGI
7 * Split up af-specific portion
11 #include <linux/compiler.h>
12 #include <linux/inetdevice.h>
16 static struct dst_ops xfrm4_dst_ops
;
17 static struct xfrm_policy_afinfo xfrm4_policy_afinfo
;
19 static int xfrm4_dst_lookup(struct xfrm_dst
**dst
, struct flowi
*fl
)
21 return __ip_route_output_key((struct rtable
**)dst
, fl
);
24 static int xfrm4_get_saddr(xfrm_address_t
*saddr
, xfrm_address_t
*daddr
)
27 struct flowi fl_tunnel
= {
35 if (!xfrm4_dst_lookup((struct xfrm_dst
**)&rt
, &fl_tunnel
)) {
36 saddr
->a4
= rt
->rt_src
;
37 dst_release(&rt
->u
.dst
);
43 static struct dst_entry
*
44 __xfrm4_find_bundle(struct flowi
*fl
, struct xfrm_policy
*policy
)
46 struct dst_entry
*dst
;
48 read_lock_bh(&policy
->lock
);
49 for (dst
= policy
->bundles
; dst
; dst
= dst
->next
) {
50 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
51 if (xdst
->u
.rt
.fl
.oif
== fl
->oif
&& /*XXX*/
52 xdst
->u
.rt
.fl
.fl4_dst
== fl
->fl4_dst
&&
53 xdst
->u
.rt
.fl
.fl4_src
== fl
->fl4_src
&&
54 xdst
->u
.rt
.fl
.fl4_tos
== fl
->fl4_tos
&&
55 xfrm_bundle_ok(policy
, xdst
, fl
, AF_INET
, 0)) {
60 read_unlock_bh(&policy
->lock
);
64 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
65 * all the metrics... Shortly, bundle a bundle.
69 __xfrm4_bundle_create(struct xfrm_policy
*policy
, struct xfrm_state
**xfrm
, int nx
,
70 struct flowi
*fl
, struct dst_entry
**dst_p
)
72 struct dst_entry
*dst
, *dst_prev
;
73 struct rtable
*rt0
= (struct rtable
*)(*dst_p
);
74 struct rtable
*rt
= rt0
;
75 u32 remote
= fl
->fl4_dst
;
76 u32 local
= fl
->fl4_src
;
77 struct flowi fl_tunnel
= {
91 dst
= dst_prev
= NULL
;
94 for (i
= 0; i
< nx
; i
++) {
95 struct dst_entry
*dst1
= dst_alloc(&xfrm4_dst_ops
);
96 struct xfrm_dst
*xdst
;
99 if (unlikely(dst1
== NULL
)) {
101 dst_release(&rt
->u
.dst
);
108 dst_prev
->child
= dst1
;
109 dst1
->flags
|= DST_NOHASH
;
113 xdst
= (struct xfrm_dst
*)dst1
;
114 xdst
->route
= &rt
->u
.dst
;
115 xdst
->genid
= xfrm
[i
]->genid
;
117 dst1
->next
= dst_prev
;
119 if (xfrm
[i
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
120 remote
= xfrm
[i
]->id
.daddr
.a4
;
121 local
= xfrm
[i
]->props
.saddr
.a4
;
124 header_len
+= xfrm
[i
]->props
.header_len
;
125 trailer_len
+= xfrm
[i
]->props
.trailer_len
;
128 fl_tunnel
.fl4_src
= local
;
129 fl_tunnel
.fl4_dst
= remote
;
130 err
= xfrm_dst_lookup((struct xfrm_dst
**)&rt
,
131 &fl_tunnel
, AF_INET
);
135 dst_hold(&rt
->u
.dst
);
138 dst_prev
->child
= &rt
->u
.dst
;
139 dst
->path
= &rt
->u
.dst
;
146 for (; dst_prev
!= &rt
->u
.dst
; dst_prev
= dst_prev
->child
) {
147 struct xfrm_dst
*x
= (struct xfrm_dst
*)dst_prev
;
150 dst_prev
->xfrm
= xfrm
[i
++];
151 dst_prev
->dev
= rt
->u
.dst
.dev
;
153 dev_hold(rt
->u
.dst
.dev
);
154 dst_prev
->obsolete
= -1;
155 dst_prev
->flags
|= DST_HOST
;
156 dst_prev
->lastuse
= jiffies
;
157 dst_prev
->header_len
= header_len
;
158 dst_prev
->nfheader_len
= 0;
159 dst_prev
->trailer_len
= trailer_len
;
160 memcpy(&dst_prev
->metrics
, &x
->route
->metrics
, sizeof(dst_prev
->metrics
));
162 /* Copy neighbout for reachability confirmation */
163 dst_prev
->neighbour
= neigh_clone(rt
->u
.dst
.neighbour
);
164 dst_prev
->input
= rt
->u
.dst
.input
;
165 dst_prev
->output
= xfrm4_output
;
167 atomic_inc(&rt
->peer
->refcnt
);
168 x
->u
.rt
.peer
= rt
->peer
;
169 /* Sheit... I remember I did this right. Apparently,
170 * it was magically lost, so this code needs audit */
171 x
->u
.rt
.rt_flags
= rt0
->rt_flags
&(RTCF_BROADCAST
|RTCF_MULTICAST
|RTCF_LOCAL
);
172 x
->u
.rt
.rt_type
= rt
->rt_type
;
173 x
->u
.rt
.rt_src
= rt0
->rt_src
;
174 x
->u
.rt
.rt_dst
= rt0
->rt_dst
;
175 x
->u
.rt
.rt_gateway
= rt
->rt_gateway
;
176 x
->u
.rt
.rt_spec_dst
= rt0
->rt_spec_dst
;
177 x
->u
.rt
.idev
= rt0
->idev
;
178 in_dev_hold(rt0
->idev
);
179 header_len
-= x
->u
.dst
.xfrm
->props
.header_len
;
180 trailer_len
-= x
->u
.dst
.xfrm
->props
.trailer_len
;
193 _decode_session4(struct sk_buff
*skb
, struct flowi
*fl
)
195 struct iphdr
*iph
= skb
->nh
.iph
;
196 u8
*xprth
= skb
->nh
.raw
+ iph
->ihl
*4;
198 memset(fl
, 0, sizeof(struct flowi
));
199 if (!(iph
->frag_off
& htons(IP_MF
| IP_OFFSET
))) {
200 switch (iph
->protocol
) {
205 if (pskb_may_pull(skb
, xprth
+ 4 - skb
->data
)) {
206 u16
*ports
= (u16
*)xprth
;
208 fl
->fl_ip_sport
= ports
[0];
209 fl
->fl_ip_dport
= ports
[1];
214 if (pskb_may_pull(skb
, xprth
+ 2 - skb
->data
)) {
217 fl
->fl_icmp_type
= icmp
[0];
218 fl
->fl_icmp_code
= icmp
[1];
223 if (pskb_may_pull(skb
, xprth
+ 4 - skb
->data
)) {
224 __be32
*ehdr
= (__be32
*)xprth
;
226 fl
->fl_ipsec_spi
= ehdr
[0];
231 if (pskb_may_pull(skb
, xprth
+ 8 - skb
->data
)) {
232 __be32
*ah_hdr
= (__be32
*)xprth
;
234 fl
->fl_ipsec_spi
= ah_hdr
[1];
239 if (pskb_may_pull(skb
, xprth
+ 4 - skb
->data
)) {
240 __be16
*ipcomp_hdr
= (__be16
*)xprth
;
242 fl
->fl_ipsec_spi
= htonl(ntohs(ipcomp_hdr
[1]));
246 fl
->fl_ipsec_spi
= 0;
250 fl
->proto
= iph
->protocol
;
251 fl
->fl4_dst
= iph
->daddr
;
252 fl
->fl4_src
= iph
->saddr
;
253 fl
->fl4_tos
= iph
->tos
;
256 static inline int xfrm4_garbage_collect(void)
258 xfrm4_policy_afinfo
.garbage_collect();
259 return (atomic_read(&xfrm4_dst_ops
.entries
) > xfrm4_dst_ops
.gc_thresh
*2);
262 static void xfrm4_update_pmtu(struct dst_entry
*dst
, u32 mtu
)
264 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
265 struct dst_entry
*path
= xdst
->route
;
267 path
->ops
->update_pmtu(path
, mtu
);
270 static void xfrm4_dst_destroy(struct dst_entry
*dst
)
272 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
274 if (likely(xdst
->u
.rt
.idev
))
275 in_dev_put(xdst
->u
.rt
.idev
);
276 xfrm_dst_destroy(xdst
);
279 static void xfrm4_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
,
282 struct xfrm_dst
*xdst
;
287 xdst
= (struct xfrm_dst
*)dst
;
288 if (xdst
->u
.rt
.idev
->dev
== dev
) {
289 struct in_device
*loopback_idev
= in_dev_get(&loopback_dev
);
290 BUG_ON(!loopback_idev
);
293 in_dev_put(xdst
->u
.rt
.idev
);
294 xdst
->u
.rt
.idev
= loopback_idev
;
295 in_dev_hold(loopback_idev
);
296 xdst
= (struct xfrm_dst
*)xdst
->u
.dst
.child
;
297 } while (xdst
->u
.dst
.xfrm
);
299 __in_dev_put(loopback_idev
);
302 xfrm_dst_ifdown(dst
, dev
);
305 static struct dst_ops xfrm4_dst_ops
= {
307 .protocol
= __constant_htons(ETH_P_IP
),
308 .gc
= xfrm4_garbage_collect
,
309 .update_pmtu
= xfrm4_update_pmtu
,
310 .destroy
= xfrm4_dst_destroy
,
311 .ifdown
= xfrm4_dst_ifdown
,
313 .entry_size
= sizeof(struct xfrm_dst
),
316 static struct xfrm_policy_afinfo xfrm4_policy_afinfo
= {
318 .dst_ops
= &xfrm4_dst_ops
,
319 .dst_lookup
= xfrm4_dst_lookup
,
320 .get_saddr
= xfrm4_get_saddr
,
321 .find_bundle
= __xfrm4_find_bundle
,
322 .bundle_create
= __xfrm4_bundle_create
,
323 .decode_session
= _decode_session4
,
326 static void __init
xfrm4_policy_init(void)
328 xfrm_policy_register_afinfo(&xfrm4_policy_afinfo
);
331 static void __exit
xfrm4_policy_fini(void)
333 xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo
);
336 void __init
xfrm4_init(void)