1818#include <net/icmp.h>
1919#include <net/protocol.h>
2020#include <net/udp.h>
21+ #include <net/tcp.h>
22+ #include <net/espintcp.h>
2123
2224#include <linux/highmem.h>
2325
@@ -117,6 +119,132 @@ static void esp_ssg_unref(struct xfrm_state *x, void *tmp)
117119 put_page (sg_page (sg ));
118120}
119121
122+ #ifdef CONFIG_INET_ESPINTCP
123+ struct esp_tcp_sk {
124+ struct sock * sk ;
125+ struct rcu_head rcu ;
126+ };
127+
128+ static void esp_free_tcp_sk (struct rcu_head * head )
129+ {
130+ struct esp_tcp_sk * esk = container_of (head , struct esp_tcp_sk , rcu );
131+
132+ sock_put (esk -> sk );
133+ kfree (esk );
134+ }
135+
136+ static struct sock * esp_find_tcp_sk (struct xfrm_state * x )
137+ {
138+ struct xfrm_encap_tmpl * encap = x -> encap ;
139+ struct esp_tcp_sk * esk ;
140+ __be16 sport , dport ;
141+ struct sock * nsk ;
142+ struct sock * sk ;
143+
144+ sk = rcu_dereference (x -> encap_sk );
145+ if (sk && sk -> sk_state == TCP_ESTABLISHED )
146+ return sk ;
147+
148+ spin_lock_bh (& x -> lock );
149+ sport = encap -> encap_sport ;
150+ dport = encap -> encap_dport ;
151+ nsk = rcu_dereference_protected (x -> encap_sk ,
152+ lockdep_is_held (& x -> lock ));
153+ if (sk && sk == nsk ) {
154+ esk = kmalloc (sizeof (* esk ), GFP_ATOMIC );
155+ if (!esk ) {
156+ spin_unlock_bh (& x -> lock );
157+ return ERR_PTR (- ENOMEM );
158+ }
159+ RCU_INIT_POINTER (x -> encap_sk , NULL );
160+ esk -> sk = sk ;
161+ call_rcu (& esk -> rcu , esp_free_tcp_sk );
162+ }
163+ spin_unlock_bh (& x -> lock );
164+
165+ sk = inet_lookup_established (xs_net (x ), & tcp_hashinfo , x -> id .daddr .a4 ,
166+ dport , x -> props .saddr .a4 , sport , 0 );
167+ if (!sk )
168+ return ERR_PTR (- ENOENT );
169+
170+ if (!tcp_is_ulp_esp (sk )) {
171+ sock_put (sk );
172+ return ERR_PTR (- EINVAL );
173+ }
174+
175+ spin_lock_bh (& x -> lock );
176+ nsk = rcu_dereference_protected (x -> encap_sk ,
177+ lockdep_is_held (& x -> lock ));
178+ if (encap -> encap_sport != sport ||
179+ encap -> encap_dport != dport ) {
180+ sock_put (sk );
181+ sk = nsk ?: ERR_PTR (- EREMCHG );
182+ } else if (sk == nsk ) {
183+ sock_put (sk );
184+ } else {
185+ rcu_assign_pointer (x -> encap_sk , sk );
186+ }
187+ spin_unlock_bh (& x -> lock );
188+
189+ return sk ;
190+ }
191+
192+ static int esp_output_tcp_finish (struct xfrm_state * x , struct sk_buff * skb )
193+ {
194+ struct sock * sk ;
195+ int err ;
196+
197+ rcu_read_lock ();
198+
199+ sk = esp_find_tcp_sk (x );
200+ err = PTR_ERR_OR_ZERO (sk );
201+ if (err )
202+ goto out ;
203+
204+ bh_lock_sock (sk );
205+ if (sock_owned_by_user (sk ))
206+ err = espintcp_queue_out (sk , skb );
207+ else
208+ err = espintcp_push_skb (sk , skb );
209+ bh_unlock_sock (sk );
210+
211+ out :
212+ rcu_read_unlock ();
213+ return err ;
214+ }
215+
216+ static int esp_output_tcp_encap_cb (struct net * net , struct sock * sk ,
217+ struct sk_buff * skb )
218+ {
219+ struct dst_entry * dst = skb_dst (skb );
220+ struct xfrm_state * x = dst -> xfrm ;
221+
222+ return esp_output_tcp_finish (x , skb );
223+ }
224+
225+ static int esp_output_tail_tcp (struct xfrm_state * x , struct sk_buff * skb )
226+ {
227+ int err ;
228+
229+ local_bh_disable ();
230+ err = xfrm_trans_queue_net (xs_net (x ), skb , esp_output_tcp_encap_cb );
231+ local_bh_enable ();
232+
233+ /* EINPROGRESS just happens to do the right thing. It
234+ * actually means that the skb has been consumed and
235+ * isn't coming back.
236+ */
237+ return err ?: - EINPROGRESS ;
238+ }
239+ #else
240+ static int esp_output_tail_tcp (struct xfrm_state * x , struct sk_buff * skb )
241+ {
242+ kfree_skb (skb );
243+
244+ return - EOPNOTSUPP ;
245+ }
246+ #endif
247+
120248static void esp_output_done (struct crypto_async_request * base , int err )
121249{
122250 struct sk_buff * skb = base -> data ;
@@ -147,7 +275,11 @@ static void esp_output_done(struct crypto_async_request *base, int err)
147275 secpath_reset (skb );
148276 xfrm_dev_resume (skb );
149277 } else {
150- xfrm_output_resume (skb , err );
278+ if (!err &&
279+ x -> encap && x -> encap -> encap_type == TCP_ENCAP_ESPINTCP )
280+ esp_output_tail_tcp (x , skb );
281+ else
282+ xfrm_output_resume (skb , err );
151283 }
152284}
153285
@@ -236,7 +368,7 @@ static struct ip_esp_hdr *esp_output_udp_encap(struct sk_buff *skb,
236368 unsigned int len ;
237369
238370 len = skb -> len + esp -> tailen - skb_transport_offset (skb );
239- if (len + sizeof (struct iphdr ) >= IP_MAX_MTU )
371+ if (len + sizeof (struct iphdr ) > IP_MAX_MTU )
240372 return ERR_PTR (- EMSGSIZE );
241373
242374 uh = (struct udphdr * )esp -> esph ;
@@ -256,6 +388,41 @@ static struct ip_esp_hdr *esp_output_udp_encap(struct sk_buff *skb,
256388 return (struct ip_esp_hdr * )(uh + 1 );
257389}
258390
391+ #ifdef CONFIG_INET_ESPINTCP
392+ static struct ip_esp_hdr * esp_output_tcp_encap (struct xfrm_state * x ,
393+ struct sk_buff * skb ,
394+ struct esp_info * esp )
395+ {
396+ __be16 * lenp = (void * )esp -> esph ;
397+ struct ip_esp_hdr * esph ;
398+ unsigned int len ;
399+ struct sock * sk ;
400+
401+ len = skb -> len + esp -> tailen - skb_transport_offset (skb );
402+ if (len > IP_MAX_MTU )
403+ return ERR_PTR (- EMSGSIZE );
404+
405+ rcu_read_lock ();
406+ sk = esp_find_tcp_sk (x );
407+ rcu_read_unlock ();
408+
409+ if (IS_ERR (sk ))
410+ return ERR_CAST (sk );
411+
412+ * lenp = htons (len );
413+ esph = (struct ip_esp_hdr * )(lenp + 1 );
414+
415+ return esph ;
416+ }
417+ #else
418+ static struct ip_esp_hdr * esp_output_tcp_encap (struct xfrm_state * x ,
419+ struct sk_buff * skb ,
420+ struct esp_info * esp )
421+ {
422+ return ERR_PTR (- EOPNOTSUPP );
423+ }
424+ #endif
425+
259426static int esp_output_encap (struct xfrm_state * x , struct sk_buff * skb ,
260427 struct esp_info * esp )
261428{
@@ -276,6 +443,9 @@ static int esp_output_encap(struct xfrm_state *x, struct sk_buff *skb,
276443 case UDP_ENCAP_ESPINUDP_NON_IKE :
277444 esph = esp_output_udp_encap (skb , encap_type , esp , sport , dport );
278445 break ;
446+ case TCP_ENCAP_ESPINTCP :
447+ esph = esp_output_tcp_encap (x , skb , esp );
448+ break ;
279449 }
280450
281451 if (IS_ERR (esph ))
@@ -296,7 +466,7 @@ int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *
296466 struct sk_buff * trailer ;
297467 int tailen = esp -> tailen ;
298468
299- /* this is non-NULL only with UDP Encapsulation */
469+ /* this is non-NULL only with TCP/ UDP Encapsulation */
300470 if (x -> encap ) {
301471 int err = esp_output_encap (x , skb , esp );
302472
@@ -491,6 +661,9 @@ int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *
491661 if (sg != dsg )
492662 esp_ssg_unref (x , tmp );
493663
664+ if (!err && x -> encap && x -> encap -> encap_type == TCP_ENCAP_ESPINTCP )
665+ err = esp_output_tail_tcp (x , skb );
666+
494667error_free :
495668 kfree (tmp );
496669error :
@@ -617,10 +790,14 @@ int esp_input_done2(struct sk_buff *skb, int err)
617790
618791 if (x -> encap ) {
619792 struct xfrm_encap_tmpl * encap = x -> encap ;
793+ struct tcphdr * th = (void * )(skb_network_header (skb ) + ihl );
620794 struct udphdr * uh = (void * )(skb_network_header (skb ) + ihl );
621795 __be16 source ;
622796
623797 switch (x -> encap -> encap_type ) {
798+ case TCP_ENCAP_ESPINTCP :
799+ source = th -> source ;
800+ break ;
624801 case UDP_ENCAP_ESPINUDP :
625802 case UDP_ENCAP_ESPINUDP_NON_IKE :
626803 source = uh -> source ;
@@ -1017,6 +1194,14 @@ static int esp_init_state(struct xfrm_state *x)
10171194 case UDP_ENCAP_ESPINUDP_NON_IKE :
10181195 x -> props .header_len += sizeof (struct udphdr ) + 2 * sizeof (u32 );
10191196 break ;
1197+ #ifdef CONFIG_INET_ESPINTCP
1198+ case TCP_ENCAP_ESPINTCP :
1199+ /* only the length field, TCP encap is done by
1200+ * the socket
1201+ */
1202+ x -> props .header_len += 2 ;
1203+ break ;
1204+ #endif
10201205 }
10211206 }
10221207
0 commit comments