5 * Kazunori MIYAZAWA @USAGI
6 * YOSHIFUJI Hideaki @USAGI
7 * Split up af-specific portion
11 #include <linux/config.h>
15 static struct dst_ops xfrm4_dst_ops
;
16 static struct xfrm_policy_afinfo xfrm4_policy_afinfo
;
18 static struct xfrm_type_map xfrm4_type_map
= { .lock
= RW_LOCK_UNLOCKED
};
20 static int xfrm4_dst_lookup(struct xfrm_dst
**dst
, struct flowi
*fl
)
22 return __ip_route_output_key((struct rtable
**)dst
, fl
);
25 /* Check that the bundle accepts the flow and its components are
29 static int __xfrm4_bundle_ok(struct xfrm_dst
*xdst
, struct flowi
*fl
)
32 if (xdst
->u
.dst
.ops
!= &xfrm4_dst_ops
)
35 if (!xfrm_selector_match(&xdst
->u
.dst
.xfrm
->sel
, fl
, AF_INET
))
37 if (xdst
->u
.dst
.xfrm
->km
.state
!= XFRM_STATE_VALID
||
38 xdst
->u
.dst
.path
->obsolete
> 0)
40 xdst
= (struct xfrm_dst
*)xdst
->u
.dst
.child
;
45 static struct dst_entry
*
46 __xfrm4_find_bundle(struct flowi
*fl
, struct xfrm_policy
*policy
)
48 struct dst_entry
*dst
;
50 read_lock_bh(&policy
->lock
);
51 for (dst
= policy
->bundles
; dst
; dst
= dst
->next
) {
52 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
53 if (xdst
->u
.rt
.fl
.oif
== fl
->oif
&& /*XXX*/
54 xdst
->u
.rt
.fl
.fl4_dst
== fl
->fl4_dst
&&
55 xdst
->u
.rt
.fl
.fl4_src
== fl
->fl4_src
&&
56 __xfrm4_bundle_ok(xdst
, fl
)) {
61 read_unlock_bh(&policy
->lock
);
65 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
66 * all the metrics... Shortly, bundle a bundle.
70 __xfrm4_bundle_create(struct xfrm_policy
*policy
, struct xfrm_state
**xfrm
, int nx
,
71 struct flowi
*fl
, struct dst_entry
**dst_p
)
73 struct dst_entry
*dst
, *dst_prev
;
74 struct rtable
*rt0
= (struct rtable
*)(*dst_p
);
75 struct rtable
*rt
= rt0
;
76 u32 remote
= fl
->fl4_dst
;
77 u32 local
= fl
->fl4_src
;
83 dst
= dst_prev
= NULL
;
85 for (i
= 0; i
< nx
; i
++) {
86 struct dst_entry
*dst1
= dst_alloc(&xfrm4_dst_ops
);
88 if (unlikely(dst1
== NULL
)) {
96 dst_prev
->child
= dst1
;
97 dst1
->flags
|= DST_NOHASH
;
101 if (xfrm
[i
]->props
.mode
) {
102 remote
= xfrm
[i
]->id
.daddr
.a4
;
103 local
= xfrm
[i
]->props
.saddr
.a4
;
105 header_len
+= xfrm
[i
]->props
.header_len
;
106 trailer_len
+= xfrm
[i
]->props
.trailer_len
;
109 if (remote
!= fl
->fl4_dst
) {
110 struct flowi fl_tunnel
= { .nl_u
= { .ip4_u
=
115 err
= xfrm_dst_lookup((struct xfrm_dst
**)&rt
, &fl_tunnel
, AF_INET
);
119 dst_hold(&rt
->u
.dst
);
121 dst_prev
->child
= &rt
->u
.dst
;
123 for (dst_prev
= dst
; dst_prev
!= &rt
->u
.dst
; dst_prev
= dst_prev
->child
) {
124 struct xfrm_dst
*x
= (struct xfrm_dst
*)dst_prev
;
127 dst_prev
->xfrm
= xfrm
[i
++];
128 dst_prev
->dev
= rt
->u
.dst
.dev
;
130 dev_hold(rt
->u
.dst
.dev
);
131 dst_prev
->obsolete
= -1;
132 dst_prev
->flags
|= DST_HOST
;
133 dst_prev
->lastuse
= jiffies
;
134 dst_prev
->header_len
= header_len
;
135 dst_prev
->trailer_len
= trailer_len
;
136 memcpy(&dst_prev
->metrics
, &rt
->u
.dst
.metrics
, sizeof(dst_prev
->metrics
));
137 dst_prev
->path
= &rt
->u
.dst
;
139 /* Copy neighbout for reachability confirmation */
140 dst_prev
->neighbour
= neigh_clone(rt
->u
.dst
.neighbour
);
141 dst_prev
->input
= rt
->u
.dst
.input
;
142 dst_prev
->output
= xfrm4_output
;
144 atomic_inc(&rt
->peer
->refcnt
);
145 x
->u
.rt
.peer
= rt
->peer
;
146 /* Sheit... I remember I did this right. Apparently,
147 * it was magically lost, so this code needs audit */
148 x
->u
.rt
.rt_flags
= rt0
->rt_flags
&(RTCF_BROADCAST
|RTCF_MULTICAST
|RTCF_LOCAL
);
149 x
->u
.rt
.rt_type
= rt
->rt_type
;
150 x
->u
.rt
.rt_src
= rt0
->rt_src
;
151 x
->u
.rt
.rt_dst
= rt0
->rt_dst
;
152 x
->u
.rt
.rt_gateway
= rt
->rt_gateway
;
153 x
->u
.rt
.rt_spec_dst
= rt0
->rt_spec_dst
;
154 header_len
-= x
->u
.dst
.xfrm
->props
.header_len
;
155 trailer_len
-= x
->u
.dst
.xfrm
->props
.trailer_len
;
167 _decode_session4(struct sk_buff
*skb
, struct flowi
*fl
)
169 struct iphdr
*iph
= skb
->nh
.iph
;
170 u8
*xprth
= skb
->nh
.raw
+ iph
->ihl
*4;
172 memset(fl
, 0, sizeof(struct flowi
));
173 if (!(iph
->frag_off
& htons(IP_MF
| IP_OFFSET
))) {
174 switch (iph
->protocol
) {
178 if (pskb_may_pull(skb
, xprth
+ 4 - skb
->data
)) {
179 u16
*ports
= (u16
*)xprth
;
181 fl
->fl_ip_sport
= ports
[0];
182 fl
->fl_ip_dport
= ports
[1];
187 if (pskb_may_pull(skb
, xprth
+ 2 - skb
->data
)) {
190 fl
->fl_icmp_type
= icmp
[0];
191 fl
->fl_icmp_code
= icmp
[1];
196 if (pskb_may_pull(skb
, xprth
+ 4 - skb
->data
)) {
197 u32
*ehdr
= (u32
*)xprth
;
199 fl
->fl_ipsec_spi
= ehdr
[0];
204 if (pskb_may_pull(skb
, xprth
+ 8 - skb
->data
)) {
205 u32
*ah_hdr
= (u32
*)xprth
;
207 fl
->fl_ipsec_spi
= ah_hdr
[1];
212 if (pskb_may_pull(skb
, xprth
+ 4 - skb
->data
)) {
213 u16
*ipcomp_hdr
= (u16
*)xprth
;
215 fl
->fl_ipsec_spi
= ntohl(ntohs(ipcomp_hdr
[1]));
219 fl
->fl_ipsec_spi
= 0;
223 fl
->proto
= iph
->protocol
;
224 fl
->fl4_dst
= iph
->daddr
;
225 fl
->fl4_src
= iph
->saddr
;
228 static inline int xfrm4_garbage_collect(void)
230 read_lock(&xfrm4_policy_afinfo
.lock
);
231 xfrm4_policy_afinfo
.garbage_collect();
232 read_unlock(&xfrm4_policy_afinfo
.lock
);
233 return (atomic_read(&xfrm4_dst_ops
.entries
) > xfrm4_dst_ops
.gc_thresh
*2);
236 static void xfrm4_update_pmtu(struct dst_entry
*dst
, u32 mtu
)
238 struct dst_entry
*path
= dst
->path
;
240 if (mtu
< 68 + dst
->header_len
)
243 path
->ops
->update_pmtu(path
, mtu
);
246 static struct dst_ops xfrm4_dst_ops
= {
248 .protocol
= __constant_htons(ETH_P_IP
),
249 .gc
= xfrm4_garbage_collect
,
250 .update_pmtu
= xfrm4_update_pmtu
,
252 .entry_size
= sizeof(struct xfrm_dst
),
255 static struct xfrm_policy_afinfo xfrm4_policy_afinfo
= {
257 .lock
= RW_LOCK_UNLOCKED
,
258 .type_map
= &xfrm4_type_map
,
259 .dst_ops
= &xfrm4_dst_ops
,
260 .dst_lookup
= xfrm4_dst_lookup
,
261 .find_bundle
= __xfrm4_find_bundle
,
262 .bundle_create
= __xfrm4_bundle_create
,
263 .decode_session
= _decode_session4
,
266 static void __init
xfrm4_policy_init(void)
268 xfrm_policy_register_afinfo(&xfrm4_policy_afinfo
);
271 static void __exit
xfrm4_policy_fini(void)
273 xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo
);
276 void __init
xfrm4_init(void)
282 void __exit
xfrm4_fini(void)
284 //xfrm4_input_fini();