* [PATCH net-next 0/3] ip: Support checksum returned in csmg
@ 2014-12-04 0:44 Tom Herbert
2014-12-04 0:44 ` [PATCH net-next 1/3] ip: Move checksum convert defines to inet Tom Herbert
2014-12-04 0:44 ` [PATCH net-next 3/3] ip: Add support for IP_CHECKSUM cmsg Tom Herbert
0 siblings, 2 replies; 5+ messages in thread
From: Tom Herbert @ 2014-12-04 0:44 UTC (permalink / raw)
To: davem, netdev
This patch set allows the packet checksum for a datagram socket
to be returned in csum data in recvmsg. This allows userspace
to implement its own checksum over the data, for instance if an
IP tunnel was be implemented in user space, the inner checksum
could be validated.
Changes in this patch set:
- Move checksum conversion to inet_sock from udp_sock. This
generalizes checksum conversion for use with other protocols.
- Move IP cmsg constants to a header file and make processing
of the flags more efficient in ip_cmsg_recv.
- Return checksum value in cmsg. This is specifically the unfolded
32 bit checksum of the full packet starting from the first byte
returned in recvmsg.
Tested: Wrote a little server to get checksums in cmsg for UDP and
verfied correct checksum is returned.
Tom Herbert (3):
ip: Move checksum convert defines to inet
ip: IP cmsg cleanup
ip: Add support for IP_CHECKSUM cmsg
include/linux/udp.h | 16 +--------
include/net/inet_sock.h | 27 ++++++++++++++
include/uapi/linux/in.h | 1 +
net/ipv4/fou.c | 2 +-
net/ipv4/ip_sockglue.c | 96 +++++++++++++++++++++++++++++++++++--------------
net/ipv4/udp.c | 12 +++++--
net/ipv4/udp_tunnel.c | 2 +-
net/ipv6/udp.c | 2 +-
8 files changed, 111 insertions(+), 47 deletions(-)
--
2.2.0.rc0.207.ga3a616c
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH net-next 1/3] ip: Move checksum convert defines to inet
2014-12-04 0:44 [PATCH net-next 0/3] ip: Support checksum returned in csmg Tom Herbert
@ 2014-12-04 0:44 ` Tom Herbert
2014-12-04 0:44 ` [PATCH net-next 3/3] ip: Add support for IP_CHECKSUM cmsg Tom Herbert
1 sibling, 0 replies; 5+ messages in thread
From: Tom Herbert @ 2014-12-04 0:44 UTC (permalink / raw)
To: davem, netdev
Move convert_csum from udp_sock to inet_sock. This allows the
possibility that we can use convert checksum for different types
of sockets and also allows convert checksum to be enabled from
inet layer (what we'll want to do when enabling IP_CHECKSUM cmsg).
Signed-off-by: Tom Herbert <therbert@google.com>
---
include/linux/udp.h | 16 +---------------
include/net/inet_sock.h | 17 +++++++++++++++++
net/ipv4/fou.c | 2 +-
net/ipv4/udp.c | 2 +-
net/ipv4/udp_tunnel.c | 2 +-
net/ipv6/udp.c | 2 +-
6 files changed, 22 insertions(+), 19 deletions(-)
diff --git a/include/linux/udp.h b/include/linux/udp.h
index ee32775..247cfdc 100644
--- a/include/linux/udp.h
+++ b/include/linux/udp.h
@@ -49,11 +49,7 @@ struct udp_sock {
unsigned int corkflag; /* Cork is required */
__u8 encap_type; /* Is this an Encapsulation socket? */
unsigned char no_check6_tx:1,/* Send zero UDP6 checksums on TX? */
- no_check6_rx:1,/* Allow zero UDP6 checksums on RX? */
- convert_csum:1;/* On receive, convert checksum
- * unnecessary to checksum complete
- * if possible.
- */
+ no_check6_rx:1;/* Allow zero UDP6 checksums on RX? */
/*
* Following member retains the information to create a UDP header
* when the socket is uncorked.
@@ -102,16 +98,6 @@ static inline bool udp_get_no_check6_rx(struct sock *sk)
return udp_sk(sk)->no_check6_rx;
}
-static inline void udp_set_convert_csum(struct sock *sk, bool val)
-{
- udp_sk(sk)->convert_csum = val;
-}
-
-static inline bool udp_get_convert_csum(struct sock *sk)
-{
- return udp_sk(sk)->convert_csum;
-}
-
#define udp_portaddr_for_each_entry(__sk, node, list) \
hlist_nulls_for_each_entry(__sk, node, list, __sk_common.skc_portaddr_node)
diff --git a/include/net/inet_sock.h b/include/net/inet_sock.h
index a829b77..360b110 100644
--- a/include/net/inet_sock.h
+++ b/include/net/inet_sock.h
@@ -184,6 +184,7 @@ struct inet_sock {
mc_all:1,
nodefrag:1;
__u8 rcv_tos;
+ __u8 convert_csum;
int uc_index;
int mc_index;
__be32 mc_addr;
@@ -250,4 +251,20 @@ static inline __u8 inet_sk_flowi_flags(const struct sock *sk)
return flags;
}
+static inline void inet_inc_convert_csum(struct sock *sk)
+{
+ inet_sk(sk)->convert_csum++;
+}
+
+static inline void inet_dec_convert_csum(struct sock *sk)
+{
+ if (inet_sk(sk)->convert_csum > 0)
+ inet_sk(sk)->convert_csum--;
+}
+
+static inline bool inet_get_convert_csum(struct sock *sk)
+{
+ return !!inet_sk(sk)->convert_csum;
+}
+
#endif /* _INET_SOCK_H */
diff --git a/net/ipv4/fou.c b/net/ipv4/fou.c
index b986298..2197c36 100644
--- a/net/ipv4/fou.c
+++ b/net/ipv4/fou.c
@@ -490,7 +490,7 @@ static int fou_create(struct net *net, struct fou_cfg *cfg,
sk->sk_user_data = fou;
fou->sock = sock;
- udp_set_convert_csum(sk, true);
+ inet_inc_convert_csum(sk);
sk->sk_allocation = GFP_ATOMIC;
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index b2d6068..221b53f 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -1793,7 +1793,7 @@ int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
if (sk != NULL) {
int ret;
- if (udp_sk(sk)->convert_csum && uh->check && !IS_UDPLITE(sk))
+ if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
inet_compute_pseudo);
diff --git a/net/ipv4/udp_tunnel.c b/net/ipv4/udp_tunnel.c
index 1671263..9996e63 100644
--- a/net/ipv4/udp_tunnel.c
+++ b/net/ipv4/udp_tunnel.c
@@ -63,7 +63,7 @@ void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
inet_sk(sk)->mc_loop = 0;
/* Enable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */
- udp_set_convert_csum(sk, true);
+ inet_inc_convert_csum(sk);
rcu_assign_sk_user_data(sk, cfg->sk_user_data);
diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c
index 7cfb5d7..5d9900c 100644
--- a/net/ipv6/udp.c
+++ b/net/ipv6/udp.c
@@ -896,7 +896,7 @@ int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
goto csum_error;
}
- if (udp_sk(sk)->convert_csum && uh->check && !IS_UDPLITE(sk))
+ if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
ip6_compute_pseudo);
--
2.2.0.rc0.207.ga3a616c
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH net-next 3/3] ip: Add support for IP_CHECKSUM cmsg
2014-12-04 0:44 [PATCH net-next 0/3] ip: Support checksum returned in csmg Tom Herbert
2014-12-04 0:44 ` [PATCH net-next 1/3] ip: Move checksum convert defines to inet Tom Herbert
@ 2014-12-04 0:44 ` Tom Herbert
2014-12-04 0:56 ` Eric Dumazet
2014-12-05 1:17 ` Eric Dumazet
1 sibling, 2 replies; 5+ messages in thread
From: Tom Herbert @ 2014-12-04 0:44 UTC (permalink / raw)
To: davem, netdev
New cmsg type is IP_CHECKSUM under SOL_IP. Enabled by standard
setsockopt.
The value returned is the unfolded 32 bit checksum of the packet
being received starting from the first byte returned in recvmsg
through the end of the packet (truncation is disregarded).
Modified UDP to postpull checksum beyond UDP header before returning
checksum for UDP data to userspace.
Signed-off-by: Tom Herbert <therbert@google.com>
---
include/net/inet_sock.h | 1 +
include/uapi/linux/in.h | 1 +
net/ipv4/ip_sockglue.c | 34 +++++++++++++++++++++++++++++++++-
net/ipv4/udp.c | 10 +++++++++-
4 files changed, 44 insertions(+), 2 deletions(-)
diff --git a/include/net/inet_sock.h b/include/net/inet_sock.h
index 4091fab..2823fc0 100644
--- a/include/net/inet_sock.h
+++ b/include/net/inet_sock.h
@@ -203,6 +203,7 @@ struct inet_sock {
#define IP_CMSG_RETOPTS (1 << 4)
#define IP_CMSG_PASSSEC (1 << 5)
#define IP_CMSG_ORIGDSTADDR (1 << 6)
+#define IP_CMSG_CHECKSUM (1 << 7)
static inline struct inet_sock *inet_sk(const struct sock *sk)
{
diff --git a/include/uapi/linux/in.h b/include/uapi/linux/in.h
index c33a65e..589ced0 100644
--- a/include/uapi/linux/in.h
+++ b/include/uapi/linux/in.h
@@ -109,6 +109,7 @@ struct in_addr {
#define IP_MINTTL 21
#define IP_NODEFRAG 22
+#define IP_CHECKSUM 23
/* IP_MTU_DISCOVER values */
#define IP_PMTUDISC_DONT 0 /* Never send DF frames */
diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c
index d4406aa..054280f 100644
--- a/net/ipv4/ip_sockglue.c
+++ b/net/ipv4/ip_sockglue.c
@@ -96,6 +96,14 @@ static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
}
+static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb)
+{
+ if (skb->ip_summed != CHECKSUM_COMPLETE)
+ return;
+
+ put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &skb->csum);
+}
+
static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
{
char *secdata;
@@ -190,9 +198,16 @@ void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
return;
}
- if (flags & IP_CMSG_ORIGDSTADDR)
+ if (flags & IP_CMSG_ORIGDSTADDR) {
ip_cmsg_recv_dstaddr(msg, skb);
+ flags &= ~IP_CMSG_ORIGDSTADDR;
+ if (!flags)
+ return;
+ }
+
+ if (flags & IP_CMSG_CHECKSUM)
+ ip_cmsg_recv_checksum(msg, skb);
}
EXPORT_SYMBOL(ip_cmsg_recv);
@@ -512,6 +527,7 @@ static int do_ip_setsockopt(struct sock *sk, int level,
case IP_MULTICAST_ALL:
case IP_MULTICAST_LOOP:
case IP_RECVORIGDSTADDR:
+ case IP_CHECKSUM:
if (optlen >= sizeof(int)) {
if (get_user(val, (int __user *) optval))
return -EFAULT;
@@ -609,6 +625,19 @@ static int do_ip_setsockopt(struct sock *sk, int level,
else
inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
break;
+ case IP_CHECKSUM:
+ if (val) {
+ if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
+ inet_inc_convert_csum(sk);
+ inet->cmsg_flags |= IP_CMSG_CHECKSUM;
+ }
+ } else {
+ if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
+ inet_dec_convert_csum(sk);
+ inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
+ }
+ }
+ break;
case IP_TOS: /* This sets both TOS and Precedence */
if (sk->sk_type == SOCK_STREAM) {
val &= ~INET_ECN_MASK;
@@ -1212,6 +1241,9 @@ static int do_ip_getsockopt(struct sock *sk, int level, int optname,
case IP_RECVORIGDSTADDR:
val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
break;
+ case IP_CHECKSUM:
+ val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
+ break;
case IP_TOS:
val = inet->tos;
break;
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 221b53f..bba2e06 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -1315,8 +1315,16 @@ try_again:
memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
*addr_len = sizeof(*sin);
}
- if (inet->cmsg_flags)
+ if (inet->cmsg_flags) {
+ /* Pull checksum past UDP header in case we are providing
+ * checksum in cmsg.
+ */
+ if (inet->cmsg_flags & IP_CMSG_CHECKSUM)
+ skb_postpull_rcsum(skb, skb->data,
+ sizeof(struct udphdr));
+
ip_cmsg_recv(msg, skb);
+ }
err = copied;
if (flags & MSG_TRUNC)
--
2.2.0.rc0.207.ga3a616c
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH net-next 3/3] ip: Add support for IP_CHECKSUM cmsg
2014-12-04 0:44 ` [PATCH net-next 3/3] ip: Add support for IP_CHECKSUM cmsg Tom Herbert
@ 2014-12-04 0:56 ` Eric Dumazet
2014-12-05 1:17 ` Eric Dumazet
1 sibling, 0 replies; 5+ messages in thread
From: Eric Dumazet @ 2014-12-04 0:56 UTC (permalink / raw)
To: Tom Herbert, Michael Kerrisk; +Cc: davem, netdev
CC Michael Kerrisk <mtk.manpages@gmail.com> for man pages...
On Wed, 2014-12-03 at 16:44 -0800, Tom Herbert wrote:
> New cmsg type is IP_CHECKSUM under SOL_IP. Enabled by standard
> setsockopt.
>
> The value returned is the unfolded 32 bit checksum of the packet
> being received starting from the first byte returned in recvmsg
> through the end of the packet (truncation is disregarded).
>
> Modified UDP to postpull checksum beyond UDP header before returning
> checksum for UDP data to userspace.
>
> Signed-off-by: Tom Herbert <therbert@google.com>
> ---
> include/net/inet_sock.h | 1 +
> include/uapi/linux/in.h | 1 +
> net/ipv4/ip_sockglue.c | 34 +++++++++++++++++++++++++++++++++-
> net/ipv4/udp.c | 10 +++++++++-
> 4 files changed, 44 insertions(+), 2 deletions(-)
>
> diff --git a/include/net/inet_sock.h b/include/net/inet_sock.h
> index 4091fab..2823fc0 100644
> --- a/include/net/inet_sock.h
> +++ b/include/net/inet_sock.h
> @@ -203,6 +203,7 @@ struct inet_sock {
> #define IP_CMSG_RETOPTS (1 << 4)
> #define IP_CMSG_PASSSEC (1 << 5)
> #define IP_CMSG_ORIGDSTADDR (1 << 6)
> +#define IP_CMSG_CHECKSUM (1 << 7)
>
> static inline struct inet_sock *inet_sk(const struct sock *sk)
> {
> diff --git a/include/uapi/linux/in.h b/include/uapi/linux/in.h
> index c33a65e..589ced0 100644
> --- a/include/uapi/linux/in.h
> +++ b/include/uapi/linux/in.h
> @@ -109,6 +109,7 @@ struct in_addr {
>
> #define IP_MINTTL 21
> #define IP_NODEFRAG 22
> +#define IP_CHECKSUM 23
>
> /* IP_MTU_DISCOVER values */
> #define IP_PMTUDISC_DONT 0 /* Never send DF frames */
> diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c
> index d4406aa..054280f 100644
> --- a/net/ipv4/ip_sockglue.c
> +++ b/net/ipv4/ip_sockglue.c
> @@ -96,6 +96,14 @@ static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
> put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
> }
>
> +static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb)
> +{
> + if (skb->ip_summed != CHECKSUM_COMPLETE)
> + return;
> +
> + put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &skb->csum);
> +}
> +
> static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
> {
> char *secdata;
> @@ -190,9 +198,16 @@ void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
> return;
> }
>
> - if (flags & IP_CMSG_ORIGDSTADDR)
> + if (flags & IP_CMSG_ORIGDSTADDR) {
> ip_cmsg_recv_dstaddr(msg, skb);
>
> + flags &= ~IP_CMSG_ORIGDSTADDR;
> + if (!flags)
> + return;
> + }
> +
> + if (flags & IP_CMSG_CHECKSUM)
> + ip_cmsg_recv_checksum(msg, skb);
> }
> EXPORT_SYMBOL(ip_cmsg_recv);
>
> @@ -512,6 +527,7 @@ static int do_ip_setsockopt(struct sock *sk, int level,
> case IP_MULTICAST_ALL:
> case IP_MULTICAST_LOOP:
> case IP_RECVORIGDSTADDR:
> + case IP_CHECKSUM:
> if (optlen >= sizeof(int)) {
> if (get_user(val, (int __user *) optval))
> return -EFAULT;
> @@ -609,6 +625,19 @@ static int do_ip_setsockopt(struct sock *sk, int level,
> else
> inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
> break;
> + case IP_CHECKSUM:
> + if (val) {
> + if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
> + inet_inc_convert_csum(sk);
> + inet->cmsg_flags |= IP_CMSG_CHECKSUM;
> + }
> + } else {
> + if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
> + inet_dec_convert_csum(sk);
> + inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
> + }
> + }
> + break;
> case IP_TOS: /* This sets both TOS and Precedence */
> if (sk->sk_type == SOCK_STREAM) {
> val &= ~INET_ECN_MASK;
> @@ -1212,6 +1241,9 @@ static int do_ip_getsockopt(struct sock *sk, int level, int optname,
> case IP_RECVORIGDSTADDR:
> val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
> break;
> + case IP_CHECKSUM:
> + val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
> + break;
> case IP_TOS:
> val = inet->tos;
> break;
> diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
> index 221b53f..bba2e06 100644
> --- a/net/ipv4/udp.c
> +++ b/net/ipv4/udp.c
> @@ -1315,8 +1315,16 @@ try_again:
> memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
> *addr_len = sizeof(*sin);
> }
> - if (inet->cmsg_flags)
> + if (inet->cmsg_flags) {
> + /* Pull checksum past UDP header in case we are providing
> + * checksum in cmsg.
> + */
> + if (inet->cmsg_flags & IP_CMSG_CHECKSUM)
> + skb_postpull_rcsum(skb, skb->data,
> + sizeof(struct udphdr));
> +
> ip_cmsg_recv(msg, skb);
> + }
>
> err = copied;
> if (flags & MSG_TRUNC)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH net-next 3/3] ip: Add support for IP_CHECKSUM cmsg
2014-12-04 0:44 ` [PATCH net-next 3/3] ip: Add support for IP_CHECKSUM cmsg Tom Herbert
2014-12-04 0:56 ` Eric Dumazet
@ 2014-12-05 1:17 ` Eric Dumazet
1 sibling, 0 replies; 5+ messages in thread
From: Eric Dumazet @ 2014-12-05 1:17 UTC (permalink / raw)
To: Tom Herbert; +Cc: davem, netdev
On Wed, 2014-12-03 at 16:44 -0800, Tom Herbert wrote:
> diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
> index 221b53f..bba2e06 100644
> --- a/net/ipv4/udp.c
> +++ b/net/ipv4/udp.c
> @@ -1315,8 +1315,16 @@ try_again:
> memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
> *addr_len = sizeof(*sin);
> }
> - if (inet->cmsg_flags)
> + if (inet->cmsg_flags) {
> + /* Pull checksum past UDP header in case we are providing
> + * checksum in cmsg.
> + */
> + if (inet->cmsg_flags & IP_CMSG_CHECKSUM)
> + skb_postpull_rcsum(skb, skb->data,
> + sizeof(struct udphdr));
> +
> ip_cmsg_recv(msg, skb);
> + }
Have you tried MSG_PEEK ?
You can not modify the skb, you need to do something else.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2014-12-05 1:17 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-04 0:44 [PATCH net-next 0/3] ip: Support checksum returned in csmg Tom Herbert
2014-12-04 0:44 ` [PATCH net-next 1/3] ip: Move checksum convert defines to inet Tom Herbert
2014-12-04 0:44 ` [PATCH net-next 3/3] ip: Add support for IP_CHECKSUM cmsg Tom Herbert
2014-12-04 0:56 ` Eric Dumazet
2014-12-05 1:17 ` Eric Dumazet
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).