netfilter: sparse fixups
Subsystems (3)
Florian Westphal
maintainercover netfilter: sparse fixups
This series adds be16/be32 annotations to reduce the number of endianess warnings generated by the sparse checker.
Compile tested only, but objdump shows almost no code changes except for nft_tunnel.c where a variables type was changed to __be16.
Florian Westphal (6):
netfilter: nfnetlink: add missing __be16 cast
netfilter: x_tables: use correct integer types
netfilter: nf_tables: use the correct get/put helpers
netfilter: nf_tables: add and use BE register load-store helpers
netfilter: nf_tables: use correct integer types
netfilter: nf_tables: move nft_cmp_fast_mask to where its used
include/net/netfilter/nf_tables.h | 15 +++++++++++++++ include/net/netfilter/nf_tables_core.h | 10 ---------- net/bridge/netfilter/nft_meta_bridge.c | 2 +- net/netfilter/nfnetlink.c | 2 +- net/netfilter/nft_byteorder.c | 3 ++- net/netfilter/nft_cmp.c | 18 +++++++++++++++--- net/netfilter/nft_ct.c | 4 ++-- net/netfilter/nft_exthdr.c | 10 +++++----- net/netfilter/nft_osf.c | 2 +- net/netfilter/nft_socket.c | 8 ++++---- net/netfilter/nft_tproxy.c | 6 +++--- net/netfilter/nft_tunnel.c | 3 ++- net/netfilter/nft_xfrm.c | 8 ++++---- net/netfilter/xt_DSCP.c | 8 ++++---- net/netfilter/xt_TCPMSS.c | 4 ++-- net/netfilter/xt_connlimit.c | 6 +++--- 16 files changed, 64 insertions(+), 45 deletions(-)
Signature · 1 line
2.35.1
Florian Westphal
maintainerPATCH 1/6 netfilter: nfnetlink: add missing __be16 cast
Sparse flags this as suspicious, because this compares integer with a be16 with no conversion.
Its a compat check for old userspace that sends host byte order, so force a be16 cast here.
--- net/netfilter/nfnetlink.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nfnetlink.c +1 -1
@@ -626,7 +626,7 @@ static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh) nfgenmsg =nlmsg_data (nlh);skb_pull (skb, msglen);/* Work around old nft using host byte order */ -if (nfgenmsg->res_id ==NFNL_SUBSYS_NFTABLES ) +if (nfgenmsg->res_id == (__force __be16 )NFNL_SUBSYS_NFTABLES ) res_id =NFNL_SUBSYS_NFTABLES ;else res_id =ntohs (nfgenmsg->res_id);
Signature · 1 line
2.35.1
Florian Westphal
maintainerPATCH 2/6 netfilter: x_tables: use correct integer types
In reply to Florian WestphalSparse complains because __be32 and u32 are mixed without conversions. Use the correct types, no code changes.
Furthermore, xt_DSCP generates a bit truncation warning:
"cast truncates bits from constant value (ffffff03 becomes 3)"
The truncation is fine (and wanted). Add a private definition and use that instead.
objdiff shows no changes.
--- net/netfilter/xt_DSCP.c | 8 ++++---- net/netfilter/xt_TCPMSS.c | 4 ++-- net/netfilter/xt_connlimit.c | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-)
net/netfilter/xt_DSCP.c +4 -4
@@ -24,6 +24,8 @@ MODULE_ALIAS("ip6t_DSCP");MODULE_ALIAS ("ipt_TOS" );MODULE_ALIAS ("ip6t_TOS" ); +#define XT_DSCP_ECN_MASK 3u +static unsigned int dscp_tg (struct sk_buff *skb,const struct xt_action_param *par) {
@@ -34,8 +36,7 @@ dscp_tg(struct sk_buff *skb, const struct xt_action_param *par)if (skb_ensure_writable (skb,sizeof (struct iphdr )))return NF_DROP ; -ipv4_change_dsfield (ip_hdr (skb), - (__force __u8)(~XT_DSCP_MASK ), +ipv4_change_dsfield (ip_hdr (skb),XT_DSCP_ECN_MASK , dinfo->dscp <<XT_DSCP_SHIFT ); }
@@ -52,8 +53,7 @@ dscp_tg6(struct sk_buff *skb, const struct xt_action_param *par)if (skb_ensure_writable (skb,sizeof (struct ipv6hdr )))return NF_DROP ; -ipv6_change_dsfield (ipv6_hdr (skb), - (__force __u8)(~XT_DSCP_MASK ), +ipv6_change_dsfield (ipv6_hdr (skb),XT_DSCP_ECN_MASK , dinfo->dscp <<XT_DSCP_SHIFT ); }return XT_CONTINUE ;
net/netfilter/xt_TCPMSS.c +2 -2
@@ -239,8 +239,8 @@ tcpmss_tg6(struct sk_buff *skb, const struct xt_action_param *par) oldlen = ipv6h->payload_len; newlen =htons (ntohs (oldlen) + ret);if (skb->ip_summed ==CHECKSUM_COMPLETE ) - skb->csum =csum_add (csum_sub (skb->csum, oldlen), - newlen); + skb->csum =csum_add (csum_sub (skb->csum, (__force __wsum )oldlen), + (__force __wsum )newlen); ipv6h->payload_len = newlen; }return XT_CONTINUE ;
net/netfilter/xt_connlimit.c +3 -3
@@ -62,10 +62,10 @@ connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par) key[4 ] = zone->id; }else {const struct iphdr *iph =ip_hdr (skb); - key[0 ] = (info->flags &XT_CONNLIMIT_DADDR ) ? - iph->daddr : iph->saddr; - key[0 ] &= info->mask.ip; + key[0 ] = (info->flags &XT_CONNLIMIT_DADDR ) ? + (__force __u32 )iph->daddr : (__force __u32 )iph->saddr; + key[0 ] &= (__force __u32 )info->mask.ip; key[1 ] = zone->id; }
Signature · 1 line
2.35.1
Florian Westphal
maintainerPATCH 3/6 netfilter: nf_tables: use the correct get/put helpers
In reply to Florian WestphalSwitch to be16/32 and u16/32 respectively. No code changes here, the functions do the same thing, this is just for sparse checkers' sake.
objdiff shows no changes.
--- net/netfilter/nft_byteorder.c | 3 ++- net/netfilter/nft_osf.c | 2 +- net/netfilter/nft_socket.c | 8 ++++---- net/netfilter/nft_xfrm.c | 8 ++++---- 4 files changed, 11 insertions(+), 10 deletions(-)
net/netfilter/nft_byteorder.c +2 -1
@@ -44,7 +44,8 @@ void nft_byteorder_eval(const struct nft_expr *expr,case NFT_BYTEORDER_NTOH :for (i =0 ; i < priv->len /8 ; i++) { src64 =nft_reg_load64 (&src[i]); -nft_reg_store64 (&dst[i],be64_to_cpu (src64)); +nft_reg_store64 (&dst[i], +be64_to_cpu ((__force __be64 )src64)); }break ;case NFT_BYTEORDER_HTON :
net/netfilter/nft_osf.c +1 -1
@@ -99,7 +99,7 @@ static int nft_osf_dump(struct sk_buff *skb, const struct nft_expr *expr)if (nla_put_u8 (skb,NFTA_OSF_TTL , priv->ttl)) goto nla_put_failure; -if (nla_put_be32 (skb,NFTA_OSF_FLAGS ,ntohl (priv->flags))) +if (nla_put_u32 (skb,NFTA_OSF_FLAGS ,ntohl ((__force __be32 )priv->flags))) goto nla_put_failure;if (nft_dump_register (skb,NFTA_OSF_DREG , priv->dreg))
net/netfilter/nft_socket.c +4 -4
@@ -163,7 +163,7 @@ static int nft_socket_init(const struct nft_ctx *ctx,return -EOPNOTSUPP ; } - priv->key =ntohl (nla_get_u32 (tb[NFTA_SOCKET_KEY ])); + priv->key =ntohl (nla_get_be32 (tb[NFTA_SOCKET_KEY ]));switch (priv->key) {case NFT_SOCKET_TRANSPARENT :case NFT_SOCKET_WILDCARD :
@@ -179,7 +179,7 @@ static int nft_socket_init(const struct nft_ctx *ctx,if (!tb[NFTA_SOCKET_LEVEL ])return -EINVAL ; - level =ntohl (nla_get_u32 (tb[NFTA_SOCKET_LEVEL ])); + level =ntohl (nla_get_be32 (tb[NFTA_SOCKET_LEVEL ]));if (level >255 )return -EOPNOTSUPP ;
@@ -202,12 +202,12 @@ static int nft_socket_dump(struct sk_buff *skb, {const struct nft_socket *priv =nft_expr_priv (expr); -if (nla_put_u32 (skb,NFTA_SOCKET_KEY ,htonl (priv->key))) +if (nla_put_be32 (skb,NFTA_SOCKET_KEY ,htonl (priv->key)))return -1 ;if (nft_dump_register (skb,NFTA_SOCKET_DREG , priv->dreg))return -1 ;if (priv->key ==NFT_SOCKET_CGROUPV2 && -nla_put_u32 (skb,NFTA_SOCKET_LEVEL ,htonl (priv->level))) +nla_put_be32 (skb,NFTA_SOCKET_LEVEL ,htonl (priv->level)))return -1 ;return 0 ; }
net/netfilter/nft_xfrm.c +4 -4
@@ -51,7 +51,7 @@ static int nft_xfrm_get_init(const struct nft_ctx *ctx,return -EOPNOTSUPP ; } - priv->key =ntohl (nla_get_u32 (tb[NFTA_XFRM_KEY ])); + priv->key =ntohl (nla_get_be32 (tb[NFTA_XFRM_KEY ]));switch (priv->key) {case NFT_XFRM_KEY_REQID :case NFT_XFRM_KEY_SPI :
@@ -134,13 +134,13 @@ static void nft_xfrm_state_get_key(const struct nft_xfrm *priv,WARN_ON_ONCE (1 );break ;case NFT_XFRM_KEY_DADDR_IP4 : - *dest = state->id.daddr.a4; + *dest = (__force __u32 )state->id.daddr.a4;return ;case NFT_XFRM_KEY_DADDR_IP6 :memcpy (dest, &state->id.daddr.in6,sizeof (struct in6_addr ));return ;case NFT_XFRM_KEY_SADDR_IP4 : - *dest = state->props.saddr.a4; + *dest = (__force __u32 )state->props.saddr.a4;return ;case NFT_XFRM_KEY_SADDR_IP6 :memcpy (dest, &state->props.saddr.in6,sizeof (struct in6_addr ));
@@ -149,7 +149,7 @@ static void nft_xfrm_state_get_key(const struct nft_xfrm *priv, *dest = state->props.reqid;return ;case NFT_XFRM_KEY_SPI : - *dest = state->id.spi; + *dest = (__force __u32 )state->id.spi;return ; }
Signature · 1 line
2.35.1
Florian Westphal
maintainerPATCH 4/6 netfilter: nf_tables: add and use BE register load-store helpers
In reply to Florian WestphalSame as the existing ones, no conversions. This is just for sparse sake only so that we no longer mix be16/u16 and be32/u32 types.
Alternative is to add __force __beX in various places, but this seems nicer.
objdiff shows no changes.
--- include/net/netfilter/nf_tables.h | 15 +++++++++++++++ net/bridge/netfilter/nft_meta_bridge.c | 2 +- net/netfilter/nft_tproxy.c | 6 +++--- 3 files changed, 19 insertions(+), 4 deletions(-)
include/net/netfilter/nf_tables.h +15 -0
@@ -157,11 +157,26 @@ static inline void nft_reg_store16(u32 *dreg, u16 val) *(u16 *)dreg = val; } +static inline void nft_reg_store_be16 (u32 *dreg,__be16 val) +{ +nft_reg_store16 (dreg, (__force __u16 )val); +} +static inline u16 nft_reg_load16 (const u32 *sreg) {return *(u16 *)sreg; } +static inline __be16 nft_reg_load_be16 (const u32 *sreg) +{ +return (__force __be16 )nft_reg_load16 (sreg); +} + +static inline __be32 nft_reg_load_be32 (const u32 *sreg) +{ +return *(__force __be32 *)sreg; +} +static inline void nft_reg_store64 (u32 *dreg,u64 val) {put_unaligned (val, (u64 *)dreg);
net/bridge/netfilter/nft_meta_bridge.c +1 -1
@@ -53,7 +53,7 @@ static void nft_meta_bridge_get_eval(const struct nft_expr *expr, goto err;br_vlan_get_proto (br_dev, &p_proto); -nft_reg_store16 (dest,htons (p_proto)); +nft_reg_store_be16 (dest,htons (p_proto));return ; }default :
net/netfilter/nft_tproxy.c +3 -3
@@ -52,11 +52,11 @@ static void nft_tproxy_eval_v4(const struct nft_expr *expr, skb->dev, NF_TPROXY_LOOKUP_ESTABLISHED);if (priv->sreg_addr) - taddr = regs->data[priv->sreg_addr]; + taddr =nft_reg_load_be32 (®s->data[priv->sreg_addr]); taddr =nf_tproxy_laddr4 (skb, taddr, iph->daddr);if (priv->sreg_port) - tport =nft_reg_load16 (®s->data[priv->sreg_port]); + tport =nft_reg_load_be16 (®s->data[priv->sreg_port]);if (!tport) tport = hp->dest;
@@ -124,7 +124,7 @@ static void nft_tproxy_eval_v6(const struct nft_expr *expr, taddr = *nf_tproxy_laddr6 (skb, &taddr, &iph->daddr);if (priv->sreg_port) - tport =nft_reg_load16 (®s->data[priv->sreg_port]); + tport =nft_reg_load_be16 (®s->data[priv->sreg_port]);if (!tport) tport = hp->dest;
Signature · 1 line
2.35.1
Florian Westphal
maintainerPATCH 5/6 netfilter: nf_tables: use correct integer types
In reply to Florian WestphalSparse tool complains about mixing of different endianess types, so use the correct ones.
Add type casts where needed.
objdiff shows no changes except in nft_tunnel (type is changed).
--- net/netfilter/nft_cmp.c | 6 +++--- net/netfilter/nft_ct.c | 4 ++-- net/netfilter/nft_exthdr.c | 10 +++++----- net/netfilter/nft_tunnel.c | 3 ++- 4 files changed, 12 insertions(+), 11 deletions(-)
net/netfilter/nft_cmp.c +3 -3
@@ -125,13 +125,13 @@ static void nft_payload_n2h(union nft_cmp_offload_data *data, {switch (len) {case 2 : - data->val16 =ntohs (*((u16 *)val)); + data->val16 =ntohs (*((__be16 *)val));break ;case 4 : - data->val32 =ntohl (*((u32 *)val)); + data->val32 =ntohl (*((__be32 *)val));break ;case 8 : - data->val64 =be64_to_cpu (*((u64 *)val)); + data->val64 =be64_to_cpu (*((__be64 *)val));break ;default :WARN_ON_ONCE (1 );
net/netfilter/nft_ct.c +2 -2
@@ -204,12 +204,12 @@ static void nft_ct_get_eval(const struct nft_expr *expr,case NFT_CT_SRC_IP :if (nf_ct_l3num (ct) !=NFPROTO_IPV4 ) goto err; - *dest = tuple->src.u3.ip; + *dest = (__force __u32 )tuple->src.u3.ip;return ;case NFT_CT_DST_IP :if (nf_ct_l3num (ct) !=NFPROTO_IPV4 ) goto err; - *dest = tuple->dst.u3.ip; + *dest = (__force __u32 )tuple->dst.u3.ip;return ;case NFT_CT_SRC_IP6 :if (nf_ct_l3num (ct) !=NFPROTO_IPV6 )
net/netfilter/nft_exthdr.c +5 -5
@@ -266,7 +266,7 @@ static void nft_exthdr_tcp_set_eval(const struct nft_expr *expr,switch (priv->len) {case 2 : - old.v16 =get_unaligned ((u16 *)(opt + offset)); + old.v16 = (__force __be16 )get_unaligned ((u16 *)(opt + offset)); new.v16 = (__force __be16 )nft_reg_load16 ( ®s->data[priv->sreg]);
@@ -281,18 +281,18 @@ static void nft_exthdr_tcp_set_eval(const struct nft_expr *expr,if (old.v16 == new.v16)return ; -put_unaligned (new.v16, (u16 *)(opt + offset)); +put_unaligned (new.v16, (__be16 *)(opt + offset));inet_proto_csum_replace2 (&tcph->check, pkt->skb, old.v16, new.v16, false);break ;case 4 : - new.v32 = regs->data[priv->sreg]; - old.v32 =get_unaligned ((u32 *)(opt + offset)); + new.v32 =nft_reg_load_be32 (®s->data[priv->sreg]); + old.v32 = (__force __be32 )get_unaligned ((u32 *)(opt + offset));if (old.v32 == new.v32)return ; -put_unaligned (new.v32, (u32 *)(opt + offset)); +put_unaligned (new.v32, (__be32 *)(opt + offset));inet_proto_csum_replace4 (&tcph->check, pkt->skb, old.v32, new.v32, false);break ;
net/netfilter/nft_tunnel.c +2 -1
@@ -383,8 +383,9 @@ static int nft_tunnel_obj_opts_init(const struct nft_ctx *ctx,struct ip_tunnel_info *info, struct nft_tunnel_opts *opts) { - int err, rem, type = 0; struct nlattr *nla; +__be16 type =0 ; +int err, rem; err =nla_validate_nested_deprecated (attr,NFTA_TUNNEL_KEY_OPTS_MAX , nft_tunnel_opts_policy,NULL );
Signature · 1 line
2.35.1
Florian Westphal
maintainerPATCH 6/6 netfilter: nf_tables: move nft_cmp_fast_mask to where its used
In reply to Florian Westphal... and cast result to u32 so sparse won't complain anymore.
--- include/net/netfilter/nf_tables_core.h | 10 ---------- net/netfilter/nft_cmp.c | 12 ++++++++++++ 2 files changed, 12 insertions(+), 10 deletions(-)
include/net/netfilter/nf_tables_core.h +0 -10
@@ -56,16 +56,6 @@ struct nft_immediate_expr {u8 dlen; }; -/* Calculate the mask for the nft_cmp_fast expression. On big endian the -* mask needs to include the *upper* bytes when interpreting that data as -* something smaller than the full u32, therefore a cpu_to_le32 is done. -*/ -static inline u32 nft_cmp_fast_mask (unsigned int len) -{ - return cpu_to_le32(~0U >> (sizeof_field(struct nft_cmp_fast_expr, - data) * BITS_PER_BYTE - len)); -} -extern const struct nft_expr_ops nft_cmp_fast_ops;extern const struct nft_expr_ops nft_cmp16_fast_ops;
net/netfilter/nft_cmp.c +12 -0
@@ -197,6 +197,18 @@ static const struct nft_expr_ops nft_cmp_ops = { .offload = nft_cmp_offload, }; +/* Calculate the mask for the nft_cmp_fast expression. On big endian the +* mask needs to include the *upper* bytes when interpreting that data as +* something smaller than the full u32, therefore a cpu_to_le32 is done. +*/ +static u32 nft_cmp_fast_mask (unsigned int len) +{ + __le32 mask = cpu_to_le32(~0U >> (sizeof_field(struct nft_cmp_fast_expr, + data) * BITS_PER_BYTE - len)); + +return (__force u32 )mask; +} +static int nft_cmp_fast_init (const struct nft_ctx *ctx,const struct nft_expr *expr,const struct nlattr *const tb[])
Signature · 1 line
2.35.1
Pablo Neira Ayuso
maintainerQuoted 6 lines · Florian Westphal
This series adds be16/be32 annotations to reduce the number of endianess warnings generated by the sparse checker.
Compile tested only, but objdump shows almost no code changes except for
nft_tunnel.cwhere a variables type was changed to__be16.
Series applied, thanks