1 // SPDX-License-Identifier: GPL-2.0
6 * YOSHIFUJI Hideaki @USAGI
7 * Split up af-specific portion
11 #include <linux/bottom_half.h>
12 #include <linux/cache.h>
13 #include <linux/interrupt.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/netdevice.h>
17 #include <linux/percpu.h>
21 #include <net/ip_tunnels.h>
22 #include <net/ip6_tunnel.h>
24 struct xfrm_trans_tasklet
{
25 struct tasklet_struct tasklet
;
26 struct sk_buff_head queue
;
29 struct xfrm_trans_cb
{
31 struct inet_skb_parm h4
;
32 #if IS_ENABLED(CONFIG_IPV6)
33 struct inet6_skb_parm h6
;
36 int (*finish
)(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
);
39 #define XFRM_TRANS_SKB_CB(__skb) ((struct xfrm_trans_cb *)&((__skb)->cb[0]))
41 static DEFINE_SPINLOCK(xfrm_input_afinfo_lock
);
42 static struct xfrm_input_afinfo
const __rcu
*xfrm_input_afinfo
[AF_INET6
+ 1];
44 static struct gro_cells gro_cells
;
45 static struct net_device xfrm_napi_dev
;
47 static DEFINE_PER_CPU(struct xfrm_trans_tasklet
, xfrm_trans_tasklet
);
49 int xfrm_input_register_afinfo(const struct xfrm_input_afinfo
*afinfo
)
53 if (WARN_ON(afinfo
->family
>= ARRAY_SIZE(xfrm_input_afinfo
)))
56 spin_lock_bh(&xfrm_input_afinfo_lock
);
57 if (unlikely(xfrm_input_afinfo
[afinfo
->family
] != NULL
))
60 rcu_assign_pointer(xfrm_input_afinfo
[afinfo
->family
], afinfo
);
61 spin_unlock_bh(&xfrm_input_afinfo_lock
);
64 EXPORT_SYMBOL(xfrm_input_register_afinfo
);
66 int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo
*afinfo
)
70 spin_lock_bh(&xfrm_input_afinfo_lock
);
71 if (likely(xfrm_input_afinfo
[afinfo
->family
] != NULL
)) {
72 if (unlikely(xfrm_input_afinfo
[afinfo
->family
] != afinfo
))
75 RCU_INIT_POINTER(xfrm_input_afinfo
[afinfo
->family
], NULL
);
77 spin_unlock_bh(&xfrm_input_afinfo_lock
);
81 EXPORT_SYMBOL(xfrm_input_unregister_afinfo
);
83 static const struct xfrm_input_afinfo
*xfrm_input_get_afinfo(unsigned int family
)
85 const struct xfrm_input_afinfo
*afinfo
;
87 if (WARN_ON_ONCE(family
>= ARRAY_SIZE(xfrm_input_afinfo
)))
91 afinfo
= rcu_dereference(xfrm_input_afinfo
[family
]);
92 if (unlikely(!afinfo
))
97 static int xfrm_rcv_cb(struct sk_buff
*skb
, unsigned int family
, u8 protocol
,
101 const struct xfrm_input_afinfo
*afinfo
= xfrm_input_get_afinfo(family
);
104 return -EAFNOSUPPORT
;
106 ret
= afinfo
->callback(skb
, protocol
, err
);
112 struct sec_path
*secpath_set(struct sk_buff
*skb
)
114 struct sec_path
*sp
, *tmp
= skb_ext_find(skb
, SKB_EXT_SEC_PATH
);
116 sp
= skb_ext_add(skb
, SKB_EXT_SEC_PATH
);
120 if (tmp
) /* reused existing one (was COW'd if needed) */
123 /* allocated new secpath */
124 memset(sp
->ovec
, 0, sizeof(sp
->ovec
));
130 EXPORT_SYMBOL(secpath_set
);
132 /* Fetch spi and seq from ipsec header */
134 int xfrm_parse_spi(struct sk_buff
*skb
, u8 nexthdr
, __be32
*spi
, __be32
*seq
)
136 int offset
, offset_seq
;
141 hlen
= sizeof(struct ip_auth_hdr
);
142 offset
= offsetof(struct ip_auth_hdr
, spi
);
143 offset_seq
= offsetof(struct ip_auth_hdr
, seq_no
);
146 hlen
= sizeof(struct ip_esp_hdr
);
147 offset
= offsetof(struct ip_esp_hdr
, spi
);
148 offset_seq
= offsetof(struct ip_esp_hdr
, seq_no
);
151 if (!pskb_may_pull(skb
, sizeof(struct ip_comp_hdr
)))
153 *spi
= htonl(ntohs(*(__be16
*)(skb_transport_header(skb
) + 2)));
160 if (!pskb_may_pull(skb
, hlen
))
163 *spi
= *(__be32
*)(skb_transport_header(skb
) + offset
);
164 *seq
= *(__be32
*)(skb_transport_header(skb
) + offset_seq
);
167 EXPORT_SYMBOL(xfrm_parse_spi
);
169 int xfrm_prepare_input(struct xfrm_state
*x
, struct sk_buff
*skb
)
171 struct xfrm_mode
*inner_mode
= x
->inner_mode
;
174 err
= x
->outer_mode
->afinfo
->extract_input(x
, skb
);
178 if (x
->sel
.family
== AF_UNSPEC
) {
179 inner_mode
= xfrm_ip2inner_mode(x
, XFRM_MODE_SKB_CB(skb
)->protocol
);
180 if (inner_mode
== NULL
)
181 return -EAFNOSUPPORT
;
184 skb
->protocol
= inner_mode
->afinfo
->eth_proto
;
185 return inner_mode
->input2(x
, skb
);
187 EXPORT_SYMBOL(xfrm_prepare_input
);
189 int xfrm_input(struct sk_buff
*skb
, int nexthdr
, __be32 spi
, int encap_type
)
191 struct net
*net
= dev_net(skb
->dev
);
195 struct xfrm_state
*x
= NULL
;
196 xfrm_address_t
*daddr
;
197 struct xfrm_mode
*inner_mode
;
198 u32 mark
= skb
->mark
;
199 unsigned int family
= AF_UNSPEC
;
202 bool xfrm_gro
= false;
203 bool crypto_done
= false;
204 struct xfrm_offload
*xo
= xfrm_offload(skb
);
207 if (encap_type
< 0) {
208 x
= xfrm_input_state(skb
);
210 if (unlikely(x
->km
.state
!= XFRM_STATE_VALID
)) {
211 if (x
->km
.state
== XFRM_STATE_ACQ
)
212 XFRM_INC_STATS(net
, LINUX_MIB_XFRMACQUIREERROR
);
215 LINUX_MIB_XFRMINSTATEINVALID
);
219 family
= x
->outer_mode
->afinfo
->family
;
221 /* An encap_type of -1 indicates async resumption. */
222 if (encap_type
== -1) {
224 seq
= XFRM_SKB_CB(skb
)->seq
.input
.low
;
228 /* encap_type < -1 indicates a GRO call. */
230 seq
= XFRM_SPI_SKB_CB(skb
)->seq
;
232 if (xo
&& (xo
->flags
& CRYPTO_DONE
)) {
234 family
= XFRM_SPI_SKB_CB(skb
)->family
;
236 if (!(xo
->status
& CRYPTO_SUCCESS
)) {
238 (CRYPTO_TRANSPORT_AH_AUTH_FAILED
|
239 CRYPTO_TRANSPORT_ESP_AUTH_FAILED
|
240 CRYPTO_TUNNEL_AH_AUTH_FAILED
|
241 CRYPTO_TUNNEL_ESP_AUTH_FAILED
)) {
243 xfrm_audit_state_icvfail(x
, skb
,
245 x
->stats
.integrity_failed
++;
246 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEPROTOERROR
);
250 if (xo
->status
& CRYPTO_INVALID_PROTOCOL
) {
251 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEPROTOERROR
);
255 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINBUFFERERROR
);
259 if ((err
= xfrm_parse_spi(skb
, nexthdr
, &spi
, &seq
)) != 0) {
260 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
268 family
= XFRM_SPI_SKB_CB(skb
)->family
;
270 /* if tunnel is present override skb->mark value with tunnel i_key */
273 if (XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip4
)
274 mark
= be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip4
->parms
.i_key
);
277 if (XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip6
)
278 mark
= be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip6
->parms
.i_key
);
282 sp
= secpath_set(skb
);
284 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINERROR
);
289 if (!spi
&& (err
= xfrm_parse_spi(skb
, nexthdr
, &spi
, &seq
)) != 0) {
291 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
295 daddr
= (xfrm_address_t
*)(skb_network_header(skb
) +
296 XFRM_SPI_SKB_CB(skb
)->daddroff
);
298 sp
= skb_sec_path(skb
);
300 if (sp
->len
== XFRM_MAX_DEPTH
) {
302 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINBUFFERERROR
);
306 x
= xfrm_state_lookup(net
, mark
, daddr
, spi
, nexthdr
, family
);
309 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINNOSTATES
);
310 xfrm_audit_state_notfound(skb
, family
, spi
, seq
);
314 skb
->mark
= xfrm_smark_get(skb
->mark
, x
);
316 sp
->xvec
[sp
->len
++] = x
;
320 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINERROR
);
327 if (unlikely(x
->km
.state
!= XFRM_STATE_VALID
)) {
328 if (x
->km
.state
== XFRM_STATE_ACQ
)
329 XFRM_INC_STATS(net
, LINUX_MIB_XFRMACQUIREERROR
);
332 LINUX_MIB_XFRMINSTATEINVALID
);
336 if ((x
->encap
? x
->encap
->encap_type
: 0) != encap_type
) {
337 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMISMATCH
);
341 if (x
->repl
->check(x
, skb
, seq
)) {
342 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATESEQERROR
);
346 if (xfrm_state_check_expire(x
)) {
347 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEEXPIRED
);
351 spin_unlock(&x
->lock
);
353 if (xfrm_tunnel_check(skb
, x
, family
)) {
354 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMODEERROR
);
358 seq_hi
= htonl(xfrm_replay_seqhi(x
, seq
));
360 XFRM_SKB_CB(skb
)->seq
.input
.low
= seq
;
361 XFRM_SKB_CB(skb
)->seq
.input
.hi
= seq_hi
;
366 nexthdr
= x
->type_offload
->input_tail(x
, skb
);
368 nexthdr
= x
->type
->input(x
, skb
);
370 if (nexthdr
== -EINPROGRESS
)
377 if (nexthdr
== -EBADMSG
) {
378 xfrm_audit_state_icvfail(x
, skb
,
380 x
->stats
.integrity_failed
++;
382 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEPROTOERROR
);
386 /* only the first xfrm gets the encap type */
389 if (async
&& x
->repl
->recheck(x
, skb
, seq
)) {
390 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATESEQERROR
);
394 x
->repl
->advance(x
, seq
);
396 x
->curlft
.bytes
+= skb
->len
;
399 spin_unlock(&x
->lock
);
401 XFRM_MODE_SKB_CB(skb
)->protocol
= nexthdr
;
403 inner_mode
= x
->inner_mode
;
405 if (x
->sel
.family
== AF_UNSPEC
) {
406 inner_mode
= xfrm_ip2inner_mode(x
, XFRM_MODE_SKB_CB(skb
)->protocol
);
407 if (inner_mode
== NULL
) {
408 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMODEERROR
);
413 if (inner_mode
->input(x
, skb
)) {
414 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMODEERROR
);
418 if (x
->outer_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
) {
424 * We need the inner address. However, we only get here for
425 * transport mode so the outer address is identical.
427 daddr
= &x
->id
.daddr
;
428 family
= x
->outer_mode
->afinfo
->family
;
430 err
= xfrm_parse_spi(skb
, nexthdr
, &spi
, &seq
);
432 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
438 err
= xfrm_rcv_cb(skb
, family
, x
->type
->proto
, 0);
445 sp
= skb_sec_path(skb
);
449 gro_cells_receive(&gro_cells
, skb
);
452 xo
= xfrm_offload(skb
);
454 xfrm_gro
= xo
->flags
& XFRM_GRO
;
456 err
= x
->inner_mode
->afinfo
->transport_finish(skb
, xfrm_gro
|| async
);
458 sp
= skb_sec_path(skb
);
462 gro_cells_receive(&gro_cells
, skb
);
470 spin_unlock(&x
->lock
);
472 xfrm_rcv_cb(skb
, family
, x
&& x
->type
? x
->type
->proto
: nexthdr
, -1);
476 EXPORT_SYMBOL(xfrm_input
);
478 int xfrm_input_resume(struct sk_buff
*skb
, int nexthdr
)
480 return xfrm_input(skb
, nexthdr
, 0, -1);
482 EXPORT_SYMBOL(xfrm_input_resume
);
484 static void xfrm_trans_reinject(unsigned long data
)
486 struct xfrm_trans_tasklet
*trans
= (void *)data
;
487 struct sk_buff_head queue
;
490 __skb_queue_head_init(&queue
);
491 skb_queue_splice_init(&trans
->queue
, &queue
);
493 while ((skb
= __skb_dequeue(&queue
)))
494 XFRM_TRANS_SKB_CB(skb
)->finish(dev_net(skb
->dev
), NULL
, skb
);
497 int xfrm_trans_queue(struct sk_buff
*skb
,
498 int (*finish
)(struct net
*, struct sock
*,
501 struct xfrm_trans_tasklet
*trans
;
503 trans
= this_cpu_ptr(&xfrm_trans_tasklet
);
505 if (skb_queue_len(&trans
->queue
) >= netdev_max_backlog
)
508 XFRM_TRANS_SKB_CB(skb
)->finish
= finish
;
509 __skb_queue_tail(&trans
->queue
, skb
);
510 tasklet_schedule(&trans
->tasklet
);
513 EXPORT_SYMBOL(xfrm_trans_queue
);
515 void __init
xfrm_input_init(void)
520 init_dummy_netdev(&xfrm_napi_dev
);
521 err
= gro_cells_init(&gro_cells
, &xfrm_napi_dev
);
523 gro_cells
.cells
= NULL
;
525 for_each_possible_cpu(i
) {
526 struct xfrm_trans_tasklet
*trans
;
528 trans
= &per_cpu(xfrm_trans_tasklet
, i
);
529 __skb_queue_head_init(&trans
->queue
);
530 tasklet_init(&trans
->tasklet
, xfrm_trans_reinject
,
531 (unsigned long)trans
);