netfilter: nf_tables: add and use nft_thoff helper

[ Upstream commit 2d7b4ace07 ]

This allows to change storage placement later on without changing readers.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Stable-dep-of: 28427f368f ("netfilter: nft_exthdr: Fix non-linear header modification")
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Florian Westphal
2021-05-28 12:30:06 +02:00
committed by Greg Kroah-Hartman
parent 39546418b8
commit 45b3eb6afc
8 changed files with 23 additions and 18 deletions
+5
View File
@@ -33,6 +33,11 @@ static inline struct sock *nft_sk(const struct nft_pktinfo *pkt)
return pkt->xt.state->sk; return pkt->xt.state->sk;
} }
static inline unsigned int nft_thoff(const struct nft_pktinfo *pkt)
{
return pkt->xt.thoff;
}
static inline struct net *nft_net(const struct nft_pktinfo *pkt) static inline struct net *nft_net(const struct nft_pktinfo *pkt)
{ {
return pkt->xt.state->net; return pkt->xt.state->net;
+1 -1
View File
@@ -125,7 +125,7 @@ static bool nft_payload_fast_eval(const struct nft_expr *expr,
else { else {
if (!pkt->tprot_set) if (!pkt->tprot_set)
return false; return false;
ptr = skb_network_header(skb) + pkt->xt.thoff; ptr = skb_network_header(skb) + nft_thoff(pkt);
} }
ptr += priv->offset; ptr += priv->offset;
+3 -3
View File
@@ -113,17 +113,17 @@ static int nf_trace_fill_pkt_info(struct sk_buff *nlskb,
int off = skb_network_offset(skb); int off = skb_network_offset(skb);
unsigned int len, nh_end; unsigned int len, nh_end;
nh_end = pkt->tprot_set ? pkt->xt.thoff : skb->len; nh_end = pkt->tprot_set ? nft_thoff(pkt) : skb->len;
len = min_t(unsigned int, nh_end - skb_network_offset(skb), len = min_t(unsigned int, nh_end - skb_network_offset(skb),
NFT_TRACETYPE_NETWORK_HSIZE); NFT_TRACETYPE_NETWORK_HSIZE);
if (trace_fill_header(nlskb, NFTA_TRACE_NETWORK_HEADER, skb, off, len)) if (trace_fill_header(nlskb, NFTA_TRACE_NETWORK_HEADER, skb, off, len))
return -1; return -1;
if (pkt->tprot_set) { if (pkt->tprot_set) {
len = min_t(unsigned int, skb->len - pkt->xt.thoff, len = min_t(unsigned int, skb->len - nft_thoff(pkt),
NFT_TRACETYPE_TRANSPORT_HSIZE); NFT_TRACETYPE_TRANSPORT_HSIZE);
if (trace_fill_header(nlskb, NFTA_TRACE_TRANSPORT_HEADER, skb, if (trace_fill_header(nlskb, NFTA_TRACE_TRANSPORT_HEADER, skb,
pkt->xt.thoff, len)) nft_thoff(pkt), len))
return -1; return -1;
} }
+4 -4
View File
@@ -170,7 +170,7 @@ nft_tcp_header_pointer(const struct nft_pktinfo *pkt,
if (!pkt->tprot_set || pkt->tprot != IPPROTO_TCP) if (!pkt->tprot_set || pkt->tprot != IPPROTO_TCP)
return NULL; return NULL;
tcph = skb_header_pointer(pkt->skb, pkt->xt.thoff, sizeof(*tcph), buffer); tcph = skb_header_pointer(pkt->skb, nft_thoff(pkt), sizeof(*tcph), buffer);
if (!tcph) if (!tcph)
return NULL; return NULL;
@@ -178,7 +178,7 @@ nft_tcp_header_pointer(const struct nft_pktinfo *pkt,
if (*tcphdr_len < sizeof(*tcph) || *tcphdr_len > len) if (*tcphdr_len < sizeof(*tcph) || *tcphdr_len > len)
return NULL; return NULL;
return skb_header_pointer(pkt->skb, pkt->xt.thoff, *tcphdr_len, buffer); return skb_header_pointer(pkt->skb, nft_thoff(pkt), *tcphdr_len, buffer);
} }
static void nft_exthdr_tcp_eval(const struct nft_expr *expr, static void nft_exthdr_tcp_eval(const struct nft_expr *expr,
@@ -254,7 +254,7 @@ static void nft_exthdr_tcp_set_eval(const struct nft_expr *expr,
return; return;
if (skb_ensure_writable(pkt->skb, if (skb_ensure_writable(pkt->skb,
pkt->xt.thoff + i + priv->len)) nft_thoff(pkt) + i + priv->len))
return; return;
tcph = nft_tcp_header_pointer(pkt, sizeof(buff), buff, tcph = nft_tcp_header_pointer(pkt, sizeof(buff), buff,
@@ -309,7 +309,7 @@ static void nft_exthdr_sctp_eval(const struct nft_expr *expr,
struct nft_regs *regs, struct nft_regs *regs,
const struct nft_pktinfo *pkt) const struct nft_pktinfo *pkt)
{ {
unsigned int offset = pkt->xt.thoff + sizeof(struct sctphdr); unsigned int offset = nft_thoff(pkt) + sizeof(struct sctphdr);
struct nft_exthdr *priv = nft_expr_priv(expr); struct nft_exthdr *priv = nft_expr_priv(expr);
u32 *dest = &regs->data[priv->dreg]; u32 *dest = &regs->data[priv->dreg];
const struct sctp_chunkhdr *sch; const struct sctp_chunkhdr *sch;
+1 -1
View File
@@ -90,7 +90,7 @@ static void nft_flow_offload_eval(const struct nft_expr *expr,
switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) { switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
case IPPROTO_TCP: case IPPROTO_TCP:
tcph = skb_header_pointer(pkt->skb, pkt->xt.thoff, tcph = skb_header_pointer(pkt->skb, nft_thoff(pkt),
sizeof(_tcph), &_tcph); sizeof(_tcph), &_tcph);
if (unlikely(!tcph || tcph->fin || tcph->rst)) if (unlikely(!tcph || tcph->fin || tcph->rst))
goto out; goto out;
+5 -5
View File
@@ -110,7 +110,7 @@ void nft_payload_eval(const struct nft_expr *expr,
case NFT_PAYLOAD_TRANSPORT_HEADER: case NFT_PAYLOAD_TRANSPORT_HEADER:
if (!pkt->tprot_set) if (!pkt->tprot_set)
goto err; goto err;
offset = pkt->xt.thoff; offset = nft_thoff(pkt);
break; break;
default: default:
BUG(); BUG();
@@ -510,7 +510,7 @@ static int nft_payload_l4csum_offset(const struct nft_pktinfo *pkt,
*l4csum_offset = offsetof(struct tcphdr, check); *l4csum_offset = offsetof(struct tcphdr, check);
break; break;
case IPPROTO_UDP: case IPPROTO_UDP:
if (!nft_payload_udp_checksum(skb, pkt->xt.thoff)) if (!nft_payload_udp_checksum(skb, nft_thoff(pkt)))
return -1; return -1;
fallthrough; fallthrough;
case IPPROTO_UDPLITE: case IPPROTO_UDPLITE:
@@ -523,7 +523,7 @@ static int nft_payload_l4csum_offset(const struct nft_pktinfo *pkt,
return -1; return -1;
} }
*l4csum_offset += pkt->xt.thoff; *l4csum_offset += nft_thoff(pkt);
return 0; return 0;
} }
@@ -615,7 +615,7 @@ static void nft_payload_set_eval(const struct nft_expr *expr,
case NFT_PAYLOAD_TRANSPORT_HEADER: case NFT_PAYLOAD_TRANSPORT_HEADER:
if (!pkt->tprot_set) if (!pkt->tprot_set)
goto err; goto err;
offset = pkt->xt.thoff; offset = nft_thoff(pkt);
break; break;
default: default:
BUG(); BUG();
@@ -646,7 +646,7 @@ static void nft_payload_set_eval(const struct nft_expr *expr,
if (priv->csum_type == NFT_PAYLOAD_CSUM_SCTP && if (priv->csum_type == NFT_PAYLOAD_CSUM_SCTP &&
pkt->tprot == IPPROTO_SCTP && pkt->tprot == IPPROTO_SCTP &&
skb->ip_summed != CHECKSUM_PARTIAL) { skb->ip_summed != CHECKSUM_PARTIAL) {
if (nft_payload_csum_sctp(skb, pkt->xt.thoff)) if (nft_payload_csum_sctp(skb, nft_thoff(pkt)))
goto err; goto err;
} }
+2 -2
View File
@@ -109,7 +109,7 @@ static void nft_synproxy_do_eval(const struct nft_synproxy *priv,
{ {
struct synproxy_options opts = {}; struct synproxy_options opts = {};
struct sk_buff *skb = pkt->skb; struct sk_buff *skb = pkt->skb;
int thoff = pkt->xt.thoff; int thoff = nft_thoff(pkt);
const struct tcphdr *tcp; const struct tcphdr *tcp;
struct tcphdr _tcph; struct tcphdr _tcph;
@@ -123,7 +123,7 @@ static void nft_synproxy_do_eval(const struct nft_synproxy *priv,
return; return;
} }
tcp = skb_header_pointer(skb, pkt->xt.thoff, tcp = skb_header_pointer(skb, thoff,
sizeof(struct tcphdr), sizeof(struct tcphdr),
&_tcph); &_tcph);
if (!tcp) { if (!tcp) {
+2 -2
View File
@@ -88,9 +88,9 @@ static void nft_tproxy_eval_v6(const struct nft_expr *expr,
const struct nft_tproxy *priv = nft_expr_priv(expr); const struct nft_tproxy *priv = nft_expr_priv(expr);
struct sk_buff *skb = pkt->skb; struct sk_buff *skb = pkt->skb;
const struct ipv6hdr *iph = ipv6_hdr(skb); const struct ipv6hdr *iph = ipv6_hdr(skb);
struct in6_addr taddr; int thoff = nft_thoff(pkt);
int thoff = pkt->xt.thoff;
struct udphdr _hdr, *hp; struct udphdr _hdr, *hp;
struct in6_addr taddr;
__be16 tport = 0; __be16 tport = 0;
struct sock *sk; struct sock *sk;
int l4proto; int l4proto;