1 // SPDX-License-Identifier: GPL-2.0
6 * YOSHIFUJI Hideaki @USAGI
7 * Split up af-specific portion
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/netdevice.h>
17 #include <net/ip_tunnels.h>
18 #include <net/ip6_tunnel.h>
20 static struct kmem_cache
*secpath_cachep __read_mostly
;
22 static DEFINE_SPINLOCK(xfrm_input_afinfo_lock
);
23 static struct xfrm_input_afinfo
const __rcu
*xfrm_input_afinfo
[AF_INET6
+ 1];
25 static struct gro_cells gro_cells
;
26 static struct net_device xfrm_napi_dev
;
28 int xfrm_input_register_afinfo(const struct xfrm_input_afinfo
*afinfo
)
32 if (WARN_ON(afinfo
->family
>= ARRAY_SIZE(xfrm_input_afinfo
)))
35 spin_lock_bh(&xfrm_input_afinfo_lock
);
36 if (unlikely(xfrm_input_afinfo
[afinfo
->family
] != NULL
))
39 rcu_assign_pointer(xfrm_input_afinfo
[afinfo
->family
], afinfo
);
40 spin_unlock_bh(&xfrm_input_afinfo_lock
);
43 EXPORT_SYMBOL(xfrm_input_register_afinfo
);
45 int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo
*afinfo
)
49 spin_lock_bh(&xfrm_input_afinfo_lock
);
50 if (likely(xfrm_input_afinfo
[afinfo
->family
] != NULL
)) {
51 if (unlikely(xfrm_input_afinfo
[afinfo
->family
] != afinfo
))
54 RCU_INIT_POINTER(xfrm_input_afinfo
[afinfo
->family
], NULL
);
56 spin_unlock_bh(&xfrm_input_afinfo_lock
);
60 EXPORT_SYMBOL(xfrm_input_unregister_afinfo
);
62 static const struct xfrm_input_afinfo
*xfrm_input_get_afinfo(unsigned int family
)
64 const struct xfrm_input_afinfo
*afinfo
;
66 if (WARN_ON_ONCE(family
>= ARRAY_SIZE(xfrm_input_afinfo
)))
70 afinfo
= rcu_dereference(xfrm_input_afinfo
[family
]);
71 if (unlikely(!afinfo
))
76 static int xfrm_rcv_cb(struct sk_buff
*skb
, unsigned int family
, u8 protocol
,
80 const struct xfrm_input_afinfo
*afinfo
= xfrm_input_get_afinfo(family
);
85 ret
= afinfo
->callback(skb
, protocol
, err
);
91 void __secpath_destroy(struct sec_path
*sp
)
94 for (i
= 0; i
< sp
->len
; i
++)
95 xfrm_state_put(sp
->xvec
[i
]);
96 kmem_cache_free(secpath_cachep
, sp
);
98 EXPORT_SYMBOL(__secpath_destroy
);
100 struct sec_path
*secpath_dup(struct sec_path
*src
)
104 sp
= kmem_cache_alloc(secpath_cachep
, GFP_ATOMIC
);
111 memset(sp
->ovec
, 0, sizeof(sp
->ovec
[XFRM_MAX_OFFLOAD_DEPTH
]));
116 memcpy(sp
, src
, sizeof(*sp
));
117 for (i
= 0; i
< sp
->len
; i
++)
118 xfrm_state_hold(sp
->xvec
[i
]);
120 refcount_set(&sp
->refcnt
, 1);
123 EXPORT_SYMBOL(secpath_dup
);
125 int secpath_set(struct sk_buff
*skb
)
129 /* Allocate new secpath or COW existing one. */
130 if (!skb
->sp
|| refcount_read(&skb
->sp
->refcnt
) != 1) {
131 sp
= secpath_dup(skb
->sp
);
136 secpath_put(skb
->sp
);
141 EXPORT_SYMBOL(secpath_set
);
143 /* Fetch spi and seq from ipsec header */
145 int xfrm_parse_spi(struct sk_buff
*skb
, u8 nexthdr
, __be32
*spi
, __be32
*seq
)
147 int offset
, offset_seq
;
152 hlen
= sizeof(struct ip_auth_hdr
);
153 offset
= offsetof(struct ip_auth_hdr
, spi
);
154 offset_seq
= offsetof(struct ip_auth_hdr
, seq_no
);
157 hlen
= sizeof(struct ip_esp_hdr
);
158 offset
= offsetof(struct ip_esp_hdr
, spi
);
159 offset_seq
= offsetof(struct ip_esp_hdr
, seq_no
);
162 if (!pskb_may_pull(skb
, sizeof(struct ip_comp_hdr
)))
164 *spi
= htonl(ntohs(*(__be16
*)(skb_transport_header(skb
) + 2)));
171 if (!pskb_may_pull(skb
, hlen
))
174 *spi
= *(__be32
*)(skb_transport_header(skb
) + offset
);
175 *seq
= *(__be32
*)(skb_transport_header(skb
) + offset_seq
);
178 EXPORT_SYMBOL(xfrm_parse_spi
);
180 int xfrm_prepare_input(struct xfrm_state
*x
, struct sk_buff
*skb
)
182 struct xfrm_mode
*inner_mode
= x
->inner_mode
;
185 err
= x
->outer_mode
->afinfo
->extract_input(x
, skb
);
189 if (x
->sel
.family
== AF_UNSPEC
) {
190 inner_mode
= xfrm_ip2inner_mode(x
, XFRM_MODE_SKB_CB(skb
)->protocol
);
191 if (inner_mode
== NULL
)
192 return -EAFNOSUPPORT
;
195 skb
->protocol
= inner_mode
->afinfo
->eth_proto
;
196 return inner_mode
->input2(x
, skb
);
198 EXPORT_SYMBOL(xfrm_prepare_input
);
200 int xfrm_input(struct sk_buff
*skb
, int nexthdr
, __be32 spi
, int encap_type
)
202 struct net
*net
= dev_net(skb
->dev
);
206 struct xfrm_state
*x
= NULL
;
207 xfrm_address_t
*daddr
;
208 struct xfrm_mode
*inner_mode
;
209 u32 mark
= skb
->mark
;
213 bool xfrm_gro
= false;
214 bool crypto_done
= false;
215 struct xfrm_offload
*xo
= xfrm_offload(skb
);
217 if (encap_type
< 0) {
218 x
= xfrm_input_state(skb
);
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 x
= xfrm_input_state(skb
);
235 family
= XFRM_SPI_SKB_CB(skb
)->family
;
237 if (!(xo
->status
& CRYPTO_SUCCESS
)) {
239 (CRYPTO_TRANSPORT_AH_AUTH_FAILED
|
240 CRYPTO_TRANSPORT_ESP_AUTH_FAILED
|
241 CRYPTO_TUNNEL_AH_AUTH_FAILED
|
242 CRYPTO_TUNNEL_ESP_AUTH_FAILED
)) {
244 xfrm_audit_state_icvfail(x
, skb
,
246 x
->stats
.integrity_failed
++;
247 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEPROTOERROR
);
251 if (xo
->status
& CRYPTO_INVALID_PROTOCOL
) {
252 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEPROTOERROR
);
256 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINBUFFERERROR
);
260 if ((err
= xfrm_parse_spi(skb
, nexthdr
, &spi
, &seq
)) != 0) {
261 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
269 family
= XFRM_SPI_SKB_CB(skb
)->family
;
271 /* if tunnel is present override skb->mark value with tunnel i_key */
274 if (XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip4
)
275 mark
= be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip4
->parms
.i_key
);
278 if (XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip6
)
279 mark
= be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip6
->parms
.i_key
);
283 err
= secpath_set(skb
);
285 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINERROR
);
290 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 if (skb
->sp
->len
== XFRM_MAX_DEPTH
) {
299 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINBUFFERERROR
);
303 x
= xfrm_state_lookup(net
, mark
, daddr
, spi
, nexthdr
, family
);
305 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINNOSTATES
);
306 xfrm_audit_state_notfound(skb
, family
, spi
, seq
);
310 skb
->sp
->xvec
[skb
->sp
->len
++] = x
;
315 if (unlikely(x
->km
.state
!= XFRM_STATE_VALID
)) {
316 if (x
->km
.state
== XFRM_STATE_ACQ
)
317 XFRM_INC_STATS(net
, LINUX_MIB_XFRMACQUIREERROR
);
320 LINUX_MIB_XFRMINSTATEINVALID
);
324 if ((x
->encap
? x
->encap
->encap_type
: 0) != encap_type
) {
325 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMISMATCH
);
329 if (x
->repl
->check(x
, skb
, seq
)) {
330 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATESEQERROR
);
334 if (xfrm_state_check_expire(x
)) {
335 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEEXPIRED
);
339 spin_unlock(&x
->lock
);
341 if (xfrm_tunnel_check(skb
, x
, family
)) {
342 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMODEERROR
);
346 seq_hi
= htonl(xfrm_replay_seqhi(x
, seq
));
348 XFRM_SKB_CB(skb
)->seq
.input
.low
= seq
;
349 XFRM_SKB_CB(skb
)->seq
.input
.hi
= seq_hi
;
355 nexthdr
= x
->type_offload
->input_tail(x
, skb
);
357 nexthdr
= x
->type
->input(x
, skb
);
359 if (nexthdr
== -EINPROGRESS
)
366 if (nexthdr
== -EBADMSG
) {
367 xfrm_audit_state_icvfail(x
, skb
,
369 x
->stats
.integrity_failed
++;
371 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEPROTOERROR
);
375 /* only the first xfrm gets the encap type */
378 if (async
&& x
->repl
->recheck(x
, skb
, seq
)) {
379 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATESEQERROR
);
383 x
->repl
->advance(x
, seq
);
385 x
->curlft
.bytes
+= skb
->len
;
388 spin_unlock(&x
->lock
);
390 XFRM_MODE_SKB_CB(skb
)->protocol
= nexthdr
;
392 inner_mode
= x
->inner_mode
;
394 if (x
->sel
.family
== AF_UNSPEC
) {
395 inner_mode
= xfrm_ip2inner_mode(x
, XFRM_MODE_SKB_CB(skb
)->protocol
);
396 if (inner_mode
== NULL
) {
397 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMODEERROR
);
402 if (inner_mode
->input(x
, skb
)) {
403 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMODEERROR
);
407 if (x
->outer_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
) {
413 * We need the inner address. However, we only get here for
414 * transport mode so the outer address is identical.
416 daddr
= &x
->id
.daddr
;
417 family
= x
->outer_mode
->afinfo
->family
;
419 err
= xfrm_parse_spi(skb
, nexthdr
, &spi
, &seq
);
421 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
426 err
= xfrm_rcv_cb(skb
, family
, x
->type
->proto
, 0);
436 gro_cells_receive(&gro_cells
, skb
);
439 xo
= xfrm_offload(skb
);
441 xfrm_gro
= xo
->flags
& XFRM_GRO
;
443 err
= x
->inner_mode
->afinfo
->transport_finish(skb
, xfrm_gro
|| async
);
448 gro_cells_receive(&gro_cells
, skb
);
456 spin_unlock(&x
->lock
);
458 xfrm_rcv_cb(skb
, family
, x
&& x
->type
? x
->type
->proto
: nexthdr
, -1);
462 EXPORT_SYMBOL(xfrm_input
);
464 int xfrm_input_resume(struct sk_buff
*skb
, int nexthdr
)
466 return xfrm_input(skb
, nexthdr
, 0, -1);
468 EXPORT_SYMBOL(xfrm_input_resume
);
470 void __init
xfrm_input_init(void)
474 init_dummy_netdev(&xfrm_napi_dev
);
475 err
= gro_cells_init(&gro_cells
, &xfrm_napi_dev
);
477 gro_cells
.cells
= NULL
;
479 secpath_cachep
= kmem_cache_create("secpath_cache",
480 sizeof(struct sec_path
),
481 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,