Skip to content

Commit 70be6c9

Browse files
committed
xfrm: Add xfrm_tunnel_skb_cb to the skb common buffer
IPsec vti_rcv needs to remind the tunnel pointer to check it later at the vti_rcv_cb callback. So add this pointer to the IPsec common buffer, initialize it and check it to avoid transport state matching of a tunneled packet. Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
1 parent d099160 commit 70be6c9

3 files changed

Lines changed: 50 additions & 12 deletions

File tree

‎include/net/xfrm.h‎

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -599,16 +599,27 @@ struct xfrm_mgr {
599599
int xfrm_register_km(struct xfrm_mgr *km);
600600
int xfrm_unregister_km(struct xfrm_mgr *km);
601601

602+
struct xfrm_tunnel_skb_cb {
603+
union {
604+
struct inet_skb_parm h4;
605+
struct inet6_skb_parm h6;
606+
} header;
607+
608+
union {
609+
struct ip_tunnel *ip4;
610+
struct ip6_tnl *ip6;
611+
} tunnel;
612+
};
613+
614+
#define XFRM_TUNNEL_SKB_CB(__skb) ((struct xfrm_tunnel_skb_cb *)&((__skb)->cb[0]))
615+
602616
/*
603617
* This structure is used for the duration where packets are being
604618
* transformed by IPsec. As soon as the packet leaves IPsec the
605619
* area beyond the generic IP part may be overwritten.
606620
*/
607621
struct xfrm_skb_cb {
608-
union {
609-
struct inet_skb_parm h4;
610-
struct inet6_skb_parm h6;
611-
} header;
622+
struct xfrm_tunnel_skb_cb header;
612623

613624
/* Sequence number for replay protection. */
614625
union {
@@ -630,10 +641,7 @@ struct xfrm_skb_cb {
630641
* to transmit header information to the mode input/output functions.
631642
*/
632643
struct xfrm_mode_skb_cb {
633-
union {
634-
struct inet_skb_parm h4;
635-
struct inet6_skb_parm h6;
636-
} header;
644+
struct xfrm_tunnel_skb_cb header;
637645

638646
/* Copied from header for IPv4, always set to zero and DF for IPv6. */
639647
__be16 id;
@@ -665,10 +673,7 @@ struct xfrm_mode_skb_cb {
665673
* related information.
666674
*/
667675
struct xfrm_spi_skb_cb {
668-
union {
669-
struct inet_skb_parm h4;
670-
struct inet6_skb_parm h6;
671-
} header;
676+
struct xfrm_tunnel_skb_cb header;
672677

673678
unsigned int daddroff;
674679
unsigned int family;
@@ -1510,6 +1515,7 @@ int xfrm4_rcv(struct sk_buff *skb);
15101515

15111516
static inline int xfrm4_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi)
15121517
{
1518+
XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
15131519
XFRM_SPI_SKB_CB(skb)->family = AF_INET;
15141520
XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
15151521
return xfrm_input(skb, nexthdr, spi, 0);
@@ -1781,4 +1787,24 @@ static inline int xfrm_rcv_cb(struct sk_buff *skb, unsigned int family,
17811787
return 0;
17821788
}
17831789

1790+
static inline int xfrm_tunnel_check(struct sk_buff *skb, struct xfrm_state *x,
1791+
unsigned int family)
1792+
{
1793+
bool tunnel = false;
1794+
1795+
switch(family) {
1796+
case AF_INET:
1797+
if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
1798+
tunnel = true;
1799+
break;
1800+
case AF_INET6:
1801+
if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
1802+
tunnel = true;
1803+
break;
1804+
}
1805+
if (tunnel && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL))
1806+
return -EINVAL;
1807+
1808+
return 0;
1809+
}
17841810
#endif /* _NET_XFRM_H */

‎net/ipv4/xfrm4_protocol.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
6565
int ret;
6666
struct xfrm4_protocol *handler;
6767

68+
XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
6869
XFRM_SPI_SKB_CB(skb)->family = AF_INET;
6970
XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
7071

@@ -84,6 +85,8 @@ static int xfrm4_esp_rcv(struct sk_buff *skb)
8485
int ret;
8586
struct xfrm4_protocol *handler;
8687

88+
XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
89+
8790
for_each_protocol_rcu(esp4_handlers, handler)
8891
if ((ret = handler->handler(skb)) != -EINVAL)
8992
return ret;
@@ -108,6 +111,8 @@ static int xfrm4_ah_rcv(struct sk_buff *skb)
108111
int ret;
109112
struct xfrm4_protocol *handler;
110113

114+
XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
115+
111116
for_each_protocol_rcu(ah4_handlers, handler)
112117
if ((ret = handler->handler(skb)) != -EINVAL)
113118
return ret;;
@@ -132,6 +137,8 @@ static int xfrm4_ipcomp_rcv(struct sk_buff *skb)
132137
int ret;
133138
struct xfrm4_protocol *handler;
134139

140+
XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
141+
135142
for_each_protocol_rcu(ipcomp4_handlers, handler)
136143
if ((ret = handler->handler(skb)) != -EINVAL)
137144
return ret;

‎net/xfrm/xfrm_input.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
163163

164164
skb->sp->xvec[skb->sp->len++] = x;
165165

166+
if (xfrm_tunnel_check(skb, x, family)) {
167+
XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
168+
goto drop;
169+
}
170+
166171
spin_lock(&x->lock);
167172
if (unlikely(x->km.state == XFRM_STATE_ACQ)) {
168173
XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);

0 commit comments

Comments
 (0)