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/interrupt.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/percpu.h>
20 #include <net/ip_tunnels.h>
21 #include <net/ip6_tunnel.h>
23 struct xfrm_trans_tasklet
{
24 struct tasklet_struct tasklet
;
25 struct sk_buff_head queue
;
28 struct xfrm_trans_cb
{
29 int (*finish
)(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
);
32 #define XFRM_TRANS_SKB_CB(__skb) ((struct xfrm_trans_cb *)&((__skb)->cb[0]))
34 static struct kmem_cache
*secpath_cachep __read_mostly
;
36 static DEFINE_SPINLOCK(xfrm_input_afinfo_lock
);
37 static struct xfrm_input_afinfo
const __rcu
*xfrm_input_afinfo
[AF_INET6
+ 1];
39 static struct gro_cells gro_cells
;
40 static struct net_device xfrm_napi_dev
;
42 static DEFINE_PER_CPU(struct xfrm_trans_tasklet
, xfrm_trans_tasklet
);
44 int xfrm_input_register_afinfo(const struct xfrm_input_afinfo
*afinfo
)
48 if (WARN_ON(afinfo
->family
>= ARRAY_SIZE(xfrm_input_afinfo
)))
51 spin_lock_bh(&xfrm_input_afinfo_lock
);
52 if (unlikely(xfrm_input_afinfo
[afinfo
->family
] != NULL
))
55 rcu_assign_pointer(xfrm_input_afinfo
[afinfo
->family
], afinfo
);
56 spin_unlock_bh(&xfrm_input_afinfo_lock
);
59 EXPORT_SYMBOL(xfrm_input_register_afinfo
);
61 int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo
*afinfo
)
65 spin_lock_bh(&xfrm_input_afinfo_lock
);
66 if (likely(xfrm_input_afinfo
[afinfo
->family
] != NULL
)) {
67 if (unlikely(xfrm_input_afinfo
[afinfo
->family
] != afinfo
))
70 RCU_INIT_POINTER(xfrm_input_afinfo
[afinfo
->family
], NULL
);
72 spin_unlock_bh(&xfrm_input_afinfo_lock
);
76 EXPORT_SYMBOL(xfrm_input_unregister_afinfo
);
78 static const struct xfrm_input_afinfo
*xfrm_input_get_afinfo(unsigned int family
)
80 const struct xfrm_input_afinfo
*afinfo
;
82 if (WARN_ON_ONCE(family
>= ARRAY_SIZE(xfrm_input_afinfo
)))
86 afinfo
= rcu_dereference(xfrm_input_afinfo
[family
]);
87 if (unlikely(!afinfo
))
92 static int xfrm_rcv_cb(struct sk_buff
*skb
, unsigned int family
, u8 protocol
,
96 const struct xfrm_input_afinfo
*afinfo
= xfrm_input_get_afinfo(family
);
101 ret
= afinfo
->callback(skb
, protocol
, err
);
107 void __secpath_destroy(struct sec_path
*sp
)
110 for (i
= 0; i
< sp
->len
; i
++)
111 xfrm_state_put(sp
->xvec
[i
]);
112 kmem_cache_free(secpath_cachep
, sp
);
114 EXPORT_SYMBOL(__secpath_destroy
);
116 struct sec_path
*secpath_dup(struct sec_path
*src
)
120 sp
= kmem_cache_alloc(secpath_cachep
, GFP_ATOMIC
);
127 memset(sp
->ovec
, 0, sizeof(sp
->ovec
[XFRM_MAX_OFFLOAD_DEPTH
]));
132 memcpy(sp
, src
, sizeof(*sp
));
133 for (i
= 0; i
< sp
->len
; i
++)
134 xfrm_state_hold(sp
->xvec
[i
]);
136 refcount_set(&sp
->refcnt
, 1);
139 EXPORT_SYMBOL(secpath_dup
);
141 int secpath_set(struct sk_buff
*skb
)
145 /* Allocate new secpath or COW existing one. */
146 if (!skb
->sp
|| refcount_read(&skb
->sp
->refcnt
) != 1) {
147 sp
= secpath_dup(skb
->sp
);
152 secpath_put(skb
->sp
);
157 EXPORT_SYMBOL(secpath_set
);
159 /* Fetch spi and seq from ipsec header */
161 int xfrm_parse_spi(struct sk_buff
*skb
, u8 nexthdr
, __be32
*spi
, __be32
*seq
)
163 int offset
, offset_seq
;
168 hlen
= sizeof(struct ip_auth_hdr
);
169 offset
= offsetof(struct ip_auth_hdr
, spi
);
170 offset_seq
= offsetof(struct ip_auth_hdr
, seq_no
);
173 hlen
= sizeof(struct ip_esp_hdr
);
174 offset
= offsetof(struct ip_esp_hdr
, spi
);
175 offset_seq
= offsetof(struct ip_esp_hdr
, seq_no
);
178 if (!pskb_may_pull(skb
, sizeof(struct ip_comp_hdr
)))
180 *spi
= htonl(ntohs(*(__be16
*)(skb_transport_header(skb
) + 2)));
187 if (!pskb_may_pull(skb
, hlen
))
190 *spi
= *(__be32
*)(skb_transport_header(skb
) + offset
);
191 *seq
= *(__be32
*)(skb_transport_header(skb
) + offset_seq
);
194 EXPORT_SYMBOL(xfrm_parse_spi
);
196 int xfrm_prepare_input(struct xfrm_state
*x
, struct sk_buff
*skb
)
198 struct xfrm_mode
*inner_mode
= x
->inner_mode
;
201 err
= x
->outer_mode
->afinfo
->extract_input(x
, skb
);
205 if (x
->sel
.family
== AF_UNSPEC
) {
206 inner_mode
= xfrm_ip2inner_mode(x
, XFRM_MODE_SKB_CB(skb
)->protocol
);
207 if (inner_mode
== NULL
)
208 return -EAFNOSUPPORT
;
211 skb
->protocol
= inner_mode
->afinfo
->eth_proto
;
212 return inner_mode
->input2(x
, skb
);
214 EXPORT_SYMBOL(xfrm_prepare_input
);
216 int xfrm_input(struct sk_buff
*skb
, int nexthdr
, __be32 spi
, int encap_type
)
218 struct net
*net
= dev_net(skb
->dev
);
222 struct xfrm_state
*x
= NULL
;
223 xfrm_address_t
*daddr
;
224 struct xfrm_mode
*inner_mode
;
225 u32 mark
= skb
->mark
;
226 unsigned int family
= AF_UNSPEC
;
229 bool xfrm_gro
= false;
230 bool crypto_done
= false;
231 struct xfrm_offload
*xo
= xfrm_offload(skb
);
233 if (encap_type
< 0) {
234 x
= xfrm_input_state(skb
);
236 if (unlikely(x
->km
.state
!= XFRM_STATE_VALID
)) {
237 if (x
->km
.state
== XFRM_STATE_ACQ
)
238 XFRM_INC_STATS(net
, LINUX_MIB_XFRMACQUIREERROR
);
241 LINUX_MIB_XFRMINSTATEINVALID
);
245 family
= x
->outer_mode
->afinfo
->family
;
247 /* An encap_type of -1 indicates async resumption. */
248 if (encap_type
== -1) {
250 seq
= XFRM_SKB_CB(skb
)->seq
.input
.low
;
254 /* encap_type < -1 indicates a GRO call. */
256 seq
= XFRM_SPI_SKB_CB(skb
)->seq
;
258 if (xo
&& (xo
->flags
& CRYPTO_DONE
)) {
260 family
= XFRM_SPI_SKB_CB(skb
)->family
;
262 if (!(xo
->status
& CRYPTO_SUCCESS
)) {
264 (CRYPTO_TRANSPORT_AH_AUTH_FAILED
|
265 CRYPTO_TRANSPORT_ESP_AUTH_FAILED
|
266 CRYPTO_TUNNEL_AH_AUTH_FAILED
|
267 CRYPTO_TUNNEL_ESP_AUTH_FAILED
)) {
269 xfrm_audit_state_icvfail(x
, skb
,
271 x
->stats
.integrity_failed
++;
272 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEPROTOERROR
);
276 if (xo
->status
& CRYPTO_INVALID_PROTOCOL
) {
277 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEPROTOERROR
);
281 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINBUFFERERROR
);
285 if ((err
= xfrm_parse_spi(skb
, nexthdr
, &spi
, &seq
)) != 0) {
286 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
294 family
= XFRM_SPI_SKB_CB(skb
)->family
;
296 /* if tunnel is present override skb->mark value with tunnel i_key */
299 if (XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip4
)
300 mark
= be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip4
->parms
.i_key
);
303 if (XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip6
)
304 mark
= be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip6
->parms
.i_key
);
308 err
= secpath_set(skb
);
310 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINERROR
);
315 if (!spi
&& (err
= xfrm_parse_spi(skb
, nexthdr
, &spi
, &seq
)) != 0) {
316 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
320 daddr
= (xfrm_address_t
*)(skb_network_header(skb
) +
321 XFRM_SPI_SKB_CB(skb
)->daddroff
);
323 if (skb
->sp
->len
== XFRM_MAX_DEPTH
) {
324 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINBUFFERERROR
);
328 x
= xfrm_state_lookup(net
, mark
, daddr
, spi
, nexthdr
, family
);
330 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINNOSTATES
);
331 xfrm_audit_state_notfound(skb
, family
, spi
, seq
);
335 skb
->sp
->xvec
[skb
->sp
->len
++] = x
;
340 if (unlikely(x
->km
.state
!= XFRM_STATE_VALID
)) {
341 if (x
->km
.state
== XFRM_STATE_ACQ
)
342 XFRM_INC_STATS(net
, LINUX_MIB_XFRMACQUIREERROR
);
345 LINUX_MIB_XFRMINSTATEINVALID
);
349 if ((x
->encap
? x
->encap
->encap_type
: 0) != encap_type
) {
350 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMISMATCH
);
354 if (x
->repl
->check(x
, skb
, seq
)) {
355 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATESEQERROR
);
359 if (xfrm_state_check_expire(x
)) {
360 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEEXPIRED
);
364 spin_unlock(&x
->lock
);
366 if (xfrm_tunnel_check(skb
, x
, family
)) {
367 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMODEERROR
);
371 seq_hi
= htonl(xfrm_replay_seqhi(x
, seq
));
373 XFRM_SKB_CB(skb
)->seq
.input
.low
= seq
;
374 XFRM_SKB_CB(skb
)->seq
.input
.hi
= seq_hi
;
380 nexthdr
= x
->type_offload
->input_tail(x
, skb
);
382 nexthdr
= x
->type
->input(x
, skb
);
384 if (nexthdr
== -EINPROGRESS
)
391 if (nexthdr
== -EBADMSG
) {
392 xfrm_audit_state_icvfail(x
, skb
,
394 x
->stats
.integrity_failed
++;
396 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEPROTOERROR
);
400 /* only the first xfrm gets the encap type */
403 if (async
&& x
->repl
->recheck(x
, skb
, seq
)) {
404 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATESEQERROR
);
408 x
->repl
->advance(x
, seq
);
410 x
->curlft
.bytes
+= skb
->len
;
413 spin_unlock(&x
->lock
);
415 XFRM_MODE_SKB_CB(skb
)->protocol
= nexthdr
;
417 inner_mode
= x
->inner_mode
;
419 if (x
->sel
.family
== AF_UNSPEC
) {
420 inner_mode
= xfrm_ip2inner_mode(x
, XFRM_MODE_SKB_CB(skb
)->protocol
);
421 if (inner_mode
== NULL
) {
422 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMODEERROR
);
427 if (inner_mode
->input(x
, skb
)) {
428 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMODEERROR
);
432 if (x
->outer_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
) {
438 * We need the inner address. However, we only get here for
439 * transport mode so the outer address is identical.
441 daddr
= &x
->id
.daddr
;
442 family
= x
->outer_mode
->afinfo
->family
;
444 err
= xfrm_parse_spi(skb
, nexthdr
, &spi
, &seq
);
446 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
451 err
= xfrm_rcv_cb(skb
, family
, x
->type
->proto
, 0);
461 gro_cells_receive(&gro_cells
, skb
);
464 xo
= xfrm_offload(skb
);
466 xfrm_gro
= xo
->flags
& XFRM_GRO
;
468 err
= x
->inner_mode
->afinfo
->transport_finish(skb
, xfrm_gro
|| async
);
473 gro_cells_receive(&gro_cells
, skb
);
481 spin_unlock(&x
->lock
);
483 xfrm_rcv_cb(skb
, family
, x
&& x
->type
? x
->type
->proto
: nexthdr
, -1);
487 EXPORT_SYMBOL(xfrm_input
);
489 int xfrm_input_resume(struct sk_buff
*skb
, int nexthdr
)
491 return xfrm_input(skb
, nexthdr
, 0, -1);
493 EXPORT_SYMBOL(xfrm_input_resume
);
495 static void xfrm_trans_reinject(unsigned long data
)
497 struct xfrm_trans_tasklet
*trans
= (void *)data
;
498 struct sk_buff_head queue
;
501 __skb_queue_head_init(&queue
);
502 skb_queue_splice_init(&trans
->queue
, &queue
);
504 while ((skb
= __skb_dequeue(&queue
)))
505 XFRM_TRANS_SKB_CB(skb
)->finish(dev_net(skb
->dev
), NULL
, skb
);
508 int xfrm_trans_queue(struct sk_buff
*skb
,
509 int (*finish
)(struct net
*, struct sock
*,
512 struct xfrm_trans_tasklet
*trans
;
514 trans
= this_cpu_ptr(&xfrm_trans_tasklet
);
516 if (skb_queue_len(&trans
->queue
) >= netdev_max_backlog
)
519 XFRM_TRANS_SKB_CB(skb
)->finish
= finish
;
520 __skb_queue_tail(&trans
->queue
, skb
);
521 tasklet_schedule(&trans
->tasklet
);
524 EXPORT_SYMBOL(xfrm_trans_queue
);
526 void __init
xfrm_input_init(void)
531 init_dummy_netdev(&xfrm_napi_dev
);
532 err
= gro_cells_init(&gro_cells
, &xfrm_napi_dev
);
534 gro_cells
.cells
= NULL
;
536 secpath_cachep
= kmem_cache_create("secpath_cache",
537 sizeof(struct sec_path
),
538 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
541 for_each_possible_cpu(i
) {
542 struct xfrm_trans_tasklet
*trans
;
544 trans
= &per_cpu(xfrm_trans_tasklet
, i
);
545 __skb_queue_head_init(&trans
->queue
);
546 tasklet_init(&trans
->tasklet
, xfrm_trans_reinject
,
547 (unsigned long)trans
);