Commit 0780b414 authored by Toke Høiland-Jørgensen's avatar Toke Høiland-Jørgensen Committed by Jakub Kicinski
Browse files

inet_ecn: Use csum16_add() helper for IP_ECN_set_* helpers



Jakub pointed out that the IP_ECN_set* helpers basically open-code
csum16_add(), so let's switch them over to using the helper instead.

v2:
- Use __be16 for check_add stack variable in IP_ECN_set_ce() (kbot)
v3:
- Turns out we need __force casts to do arithmetic on __be16 types

Reported-by: default avatarJakub Kicinski <kuba@kernel.org>
Tested-by: default avatarJonathan Morton <chromatix99@gmail.com>
Tested-by: default avatarPete Heist <pete@heistp.net>
Signed-off-by: default avatarToke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/r/20201211142638.154780-1-toke@redhat.com


Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 989a1db0
Loading
Loading
Loading
Loading
+6 −8
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@

#include <net/inet_sock.h>
#include <net/dsfield.h>
#include <net/checksum.h>

enum {
	INET_ECN_NOT_ECT = 0,
@@ -75,8 +76,8 @@ static inline void INET_ECN_dontxmit(struct sock *sk)

static inline int IP_ECN_set_ce(struct iphdr *iph)
{
	u32 check = (__force u32)iph->check;
	u32 ecn = (iph->tos + 1) & INET_ECN_MASK;
	__be16 check_add;

	/*
	 * After the last operation we have (in binary):
@@ -93,23 +94,20 @@ static inline int IP_ECN_set_ce(struct iphdr *iph)
	 * INET_ECN_ECT_1 => check += htons(0xFFFD)
	 * INET_ECN_ECT_0 => check += htons(0xFFFE)
	 */
	check += (__force u16)htons(0xFFFB) + (__force u16)htons(ecn);
	check_add = (__force __be16)((__force u16)htons(0xFFFB) +
				     (__force u16)htons(ecn));

	iph->check = (__force __sum16)(check + (check>=0xFFFF));
	iph->check = csum16_add(iph->check, check_add);
	iph->tos |= INET_ECN_CE;
	return 1;
}

static inline int IP_ECN_set_ect1(struct iphdr *iph)
{
	u32 check = (__force u32)iph->check;

	if ((iph->tos & INET_ECN_MASK) != INET_ECN_ECT_0)
		return 0;

	check += (__force u16)htons(0x1);

	iph->check = (__force __sum16)(check + (check>=0xFFFF));
	iph->check = csum16_add(iph->check, htons(0x1));
	iph->tos ^= INET_ECN_MASK;
	return 1;
}