Skip to content

Commit fb286bb

Browse files
herbertxdavem330
authored andcommitted
[NET]: Detect hardware rx checksum faults correctly
Here is the patch that introduces the generic skb_checksum_complete which also checks for hardware RX checksum faults. If that happens, it'll call netdev_rx_csum_fault which currently prints out a stack trace with the device name. In future it can turn off RX checksum. I've converted every spot under net/ that does RX checksum checks to use skb_checksum_complete or __skb_checksum_complete with the exceptions of: * Those places where checksums are done bit by bit. These will call netdev_rx_csum_fault directly. * The following have not been completely checked/converted: ipmr ip_vs netfilter dccp This patch is based on patches and suggestions from Stephen Hemminger and David S. Miller. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 1064e94 commit fb286bb

19 files changed

Lines changed: 173 additions & 134 deletions

File tree

‎include/linux/netdevice.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,13 @@ extern int netdev_max_backlog;
927927
extern int weight_p;
928928
extern int netdev_set_master(struct net_device *dev, struct net_device *master);
929929
extern int skb_checksum_help(struct sk_buff *skb, int inward);
930+
#ifdef CONFIG_BUG
931+
extern void netdev_rx_csum_fault(struct net_device *dev);
932+
#else
933+
static inline void netdev_rx_csum_fault(struct net_device *dev)
934+
{
935+
}
936+
#endif
930937
/* rx skb timestamps */
931938
extern void net_enable_timestamp(void);
932939
extern void net_disable_timestamp(void);

‎include/linux/skbuff.h‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,8 +1236,7 @@ extern unsigned int datagram_poll(struct file *file, struct socket *sock,
12361236
extern int skb_copy_datagram_iovec(const struct sk_buff *from,
12371237
int offset, struct iovec *to,
12381238
int size);
1239-
extern int skb_copy_and_csum_datagram_iovec(const
1240-
struct sk_buff *skb,
1239+
extern int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
12411240
int hlen,
12421241
struct iovec *iov);
12431242
extern void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
@@ -1305,6 +1304,30 @@ static inline void skb_set_timestamp(struct sk_buff *skb, const struct timeval *
13051304

13061305
extern void __net_timestamp(struct sk_buff *skb);
13071306

1307+
extern unsigned int __skb_checksum_complete(struct sk_buff *skb);
1308+
1309+
/**
1310+
* skb_checksum_complete - Calculate checksum of an entire packet
1311+
* @skb: packet to process
1312+
*
1313+
* This function calculates the checksum over the entire packet plus
1314+
* the value of skb->csum. The latter can be used to supply the
1315+
* checksum of a pseudo header as used by TCP/UDP. It returns the
1316+
* checksum.
1317+
*
1318+
* For protocols that contain complete checksums such as ICMP/TCP/UDP,
1319+
* this function can be used to verify that checksum on received
1320+
* packets. In that case the function should return zero if the
1321+
* checksum is correct. In particular, this function will return zero
1322+
* if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the
1323+
* hardware has already verified the correctness of the checksum.
1324+
*/
1325+
static inline unsigned int skb_checksum_complete(struct sk_buff *skb)
1326+
{
1327+
return skb->ip_summed != CHECKSUM_UNNECESSARY &&
1328+
__skb_checksum_complete(skb);
1329+
}
1330+
13081331
#ifdef CONFIG_NETFILTER
13091332
static inline void nf_conntrack_put(struct nf_conntrack *nfct)
13101333
{

‎include/net/tcp.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <linux/slab.h>
2828
#include <linux/cache.h>
2929
#include <linux/percpu.h>
30+
#include <linux/skbuff.h>
3031

3132
#include <net/inet_connection_sock.h>
3233
#include <net/inet_timewait_sock.h>
@@ -852,7 +853,7 @@ static __inline__ u16 tcp_v4_check(struct tcphdr *th, int len,
852853

853854
static __inline__ int __tcp_checksum_complete(struct sk_buff *skb)
854855
{
855-
return (unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
856+
return __skb_checksum_complete(skb);
856857
}
857858

858859
static __inline__ int tcp_checksum_complete(struct sk_buff *skb)

‎net/core/datagram.c‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,20 @@ static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
350350
return -EFAULT;
351351
}
352352

353+
unsigned int __skb_checksum_complete(struct sk_buff *skb)
354+
{
355+
unsigned int sum;
356+
357+
sum = (u16)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
358+
if (likely(!sum)) {
359+
if (unlikely(skb->ip_summed == CHECKSUM_HW))
360+
netdev_rx_csum_fault(skb->dev);
361+
skb->ip_summed = CHECKSUM_UNNECESSARY;
362+
}
363+
return sum;
364+
}
365+
EXPORT_SYMBOL(__skb_checksum_complete);
366+
353367
/**
354368
* skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
355369
* @skb: skbuff
@@ -363,7 +377,7 @@ static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
363377
* -EFAULT - fault during copy. Beware, in this case iovec
364378
* can be modified!
365379
*/
366-
int skb_copy_and_csum_datagram_iovec(const struct sk_buff *skb,
380+
int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
367381
int hlen, struct iovec *iov)
368382
{
369383
unsigned int csum;
@@ -376,8 +390,7 @@ int skb_copy_and_csum_datagram_iovec(const struct sk_buff *skb,
376390
iov++;
377391

378392
if (iov->iov_len < chunk) {
379-
if ((unsigned short)csum_fold(skb_checksum(skb, 0, chunk + hlen,
380-
skb->csum)))
393+
if (__skb_checksum_complete(skb))
381394
goto csum_error;
382395
if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
383396
goto fault;
@@ -388,6 +401,8 @@ int skb_copy_and_csum_datagram_iovec(const struct sk_buff *skb,
388401
goto fault;
389402
if ((unsigned short)csum_fold(csum))
390403
goto csum_error;
404+
if (unlikely(skb->ip_summed == CHECKSUM_HW))
405+
netdev_rx_csum_fault(skb->dev);
391406
iov->iov_len -= chunk;
392407
iov->iov_base += chunk;
393408
}

‎net/core/dev.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,18 @@ int skb_checksum_help(struct sk_buff *skb, int inward)
11081108
return ret;
11091109
}
11101110

1111+
/* Take action when hardware reception checksum errors are detected. */
1112+
#ifdef CONFIG_BUG
1113+
void netdev_rx_csum_fault(struct net_device *dev)
1114+
{
1115+
if (net_ratelimit()) {
1116+
printk(KERN_ERR "%s: hw csum failure.\n", dev->name);
1117+
dump_stack();
1118+
}
1119+
}
1120+
EXPORT_SYMBOL(netdev_rx_csum_fault);
1121+
#endif
1122+
11111123
#ifdef CONFIG_HIGHMEM
11121124
/* Actually, we should eliminate this check as soon as we know, that:
11131125
* 1. IOMMU is present and allows to map all the memory.

‎net/core/netpoll.c‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,20 @@ void netpoll_queue(struct sk_buff *skb)
101101
static int checksum_udp(struct sk_buff *skb, struct udphdr *uh,
102102
unsigned short ulen, u32 saddr, u32 daddr)
103103
{
104-
if (uh->check == 0)
104+
unsigned int psum;
105+
106+
if (uh->check == 0 || skb->ip_summed == CHECKSUM_UNNECESSARY)
105107
return 0;
106108

107-
if (skb->ip_summed == CHECKSUM_HW)
108-
return csum_tcpudp_magic(
109-
saddr, daddr, ulen, IPPROTO_UDP, skb->csum);
109+
psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
110+
111+
if (skb->ip_summed == CHECKSUM_HW &&
112+
!(u16)csum_fold(csum_add(psum, skb->csum)))
113+
return 0;
110114

111-
skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
115+
skb->csum = psum;
112116

113-
return csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
117+
return __skb_checksum_complete(skb);
114118
}
115119

116120
/*
@@ -489,7 +493,7 @@ int __netpoll_rx(struct sk_buff *skb)
489493

490494
if (ulen != len)
491495
goto out;
492-
if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr) < 0)
496+
if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
493497
goto out;
494498
if (np->local_ip && np->local_ip != ntohl(iph->daddr))
495499
goto out;

‎net/ipv4/icmp.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -934,11 +934,11 @@ int icmp_rcv(struct sk_buff *skb)
934934
case CHECKSUM_HW:
935935
if (!(u16)csum_fold(skb->csum))
936936
break;
937-
LIMIT_NETDEBUG(KERN_DEBUG "icmp v4 hw csum failure\n");
937+
/* fall through */
938938
case CHECKSUM_NONE:
939-
if ((u16)csum_fold(skb_checksum(skb, 0, skb->len, 0)))
939+
skb->csum = 0;
940+
if (__skb_checksum_complete(skb))
940941
goto error;
941-
default:;
942942
}
943943

944944
if (!pskb_pull(skb, sizeof(struct icmphdr)))

‎net/ipv4/igmp.c‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -872,11 +872,18 @@ int igmp_rcv(struct sk_buff *skb)
872872
return 0;
873873
}
874874

875-
if (!pskb_may_pull(skb, sizeof(struct igmphdr)) ||
876-
(u16)csum_fold(skb_checksum(skb, 0, len, 0))) {
877-
in_dev_put(in_dev);
878-
kfree_skb(skb);
879-
return 0;
875+
if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
876+
goto drop;
877+
878+
switch (skb->ip_summed) {
879+
case CHECKSUM_HW:
880+
if (!(u16)csum_fold(skb->csum))
881+
break;
882+
/* fall through */
883+
case CHECKSUM_NONE:
884+
skb->csum = 0;
885+
if (__skb_checksum_complete(skb))
886+
goto drop;
880887
}
881888

882889
ih = skb->h.igmph;
@@ -906,6 +913,8 @@ int igmp_rcv(struct sk_buff *skb)
906913
default:
907914
NETDEBUG(KERN_DEBUG "New IGMP type=%d, why we do not know about it?\n", ih->type);
908915
}
916+
917+
drop:
909918
in_dev_put(in_dev);
910919
kfree_skb(skb);
911920
return 0;

‎net/ipv4/ip_gre.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -577,15 +577,16 @@ static int ipgre_rcv(struct sk_buff *skb)
577577
goto drop_nolock;
578578

579579
if (flags&GRE_CSUM) {
580-
if (skb->ip_summed == CHECKSUM_HW) {
580+
switch (skb->ip_summed) {
581+
case CHECKSUM_HW:
581582
csum = (u16)csum_fold(skb->csum);
582-
if (csum)
583-
skb->ip_summed = CHECKSUM_NONE;
584-
}
585-
if (skb->ip_summed == CHECKSUM_NONE) {
586-
skb->csum = skb_checksum(skb, 0, skb->len, 0);
583+
if (!csum)
584+
break;
585+
/* fall through */
586+
case CHECKSUM_NONE:
587+
skb->csum = 0;
588+
csum = __skb_checksum_complete(skb);
587589
skb->ip_summed = CHECKSUM_HW;
588-
csum = (u16)csum_fold(skb->csum);
589590
}
590591
offset += 4;
591592
}

‎net/ipv4/netfilter/ip_conntrack_proto_icmp.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/in.h>
1414
#include <linux/icmp.h>
1515
#include <linux/seq_file.h>
16+
#include <linux/skbuff.h>
1617
#include <net/ip.h>
1718
#include <net/checksum.h>
1819
#include <linux/netfilter.h>
@@ -230,19 +231,15 @@ icmp_error(struct sk_buff *skb, enum ip_conntrack_info *ctinfo,
230231
case CHECKSUM_HW:
231232
if (!(u16)csum_fold(skb->csum))
232233
break;
233-
if (LOG_INVALID(IPPROTO_ICMP))
234-
nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
235-
"ip_ct_icmp: bad HW ICMP checksum ");
236-
return -NF_ACCEPT;
234+
/* fall through */
237235
case CHECKSUM_NONE:
238-
if ((u16)csum_fold(skb_checksum(skb, 0, skb->len, 0))) {
236+
skb->csum = 0;
237+
if (__skb_checksum_complete(skb)) {
239238
if (LOG_INVALID(IPPROTO_ICMP))
240239
nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
241240
"ip_ct_icmp: bad ICMP checksum ");
242241
return -NF_ACCEPT;
243242
}
244-
default:
245-
break;
246243
}
247244

248245
checksum_skipped:

0 commit comments

Comments
 (0)