1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2ba4e58ecSGerrit Renker /*
3ba4e58ecSGerrit Renker * Definitions for the UDP-Lite (RFC 3828) code.
4ba4e58ecSGerrit Renker */
5ba4e58ecSGerrit Renker #ifndef _UDPLITE_H
6ba4e58ecSGerrit Renker #define _UDPLITE_H
7ba4e58ecSGerrit Renker
86bb100b9SDavid S. Miller #include <net/ip6_checksum.h>
9949d6b40SJakub Kicinski #include <net/udp.h>
106bb100b9SDavid S. Miller
11ba4e58ecSGerrit Renker /* UDP-Lite socket options */
12ba4e58ecSGerrit Renker #define UDPLITE_SEND_CSCOV 10 /* sender partial coverage (as sent) */
13ba4e58ecSGerrit Renker #define UDPLITE_RECV_CSCOV 11 /* receiver partial coverage (threshold ) */
14ba4e58ecSGerrit Renker
15ba4e58ecSGerrit Renker extern struct proto udplite_prot;
16645ca708SEric Dumazet extern struct udp_table udplite_table;
17ba4e58ecSGerrit Renker
18ba4e58ecSGerrit Renker /*
19ba4e58ecSGerrit Renker * Checksum computation is all in software, hence simpler getfrag.
20ba4e58ecSGerrit Renker */
udplite_getfrag(void * from,char * to,int offset,int len,int odd,struct sk_buff * skb)21ba4e58ecSGerrit Renker static __inline__ int udplite_getfrag(void *from, char *to, int offset,
22ba4e58ecSGerrit Renker int len, int odd, struct sk_buff *skb)
23ba4e58ecSGerrit Renker {
24f69e6d13SAl Viro struct msghdr *msg = from;
250b62fca2SAl Viro return copy_from_iter_full(to, len, &msg->msg_iter) ? 0 : -EFAULT;
26ba4e58ecSGerrit Renker }
27ba4e58ecSGerrit Renker
28ba4e58ecSGerrit Renker /*
29ba4e58ecSGerrit Renker * Checksumming routines
30ba4e58ecSGerrit Renker */
udplite_checksum_init(struct sk_buff * skb,struct udphdr * uh)31ba4e58ecSGerrit Renker static inline int udplite_checksum_init(struct sk_buff *skb, struct udphdr *uh)
32ba4e58ecSGerrit Renker {
33ba4e58ecSGerrit Renker u16 cscov;
34ba4e58ecSGerrit Renker
35ba4e58ecSGerrit Renker /* In UDPv4 a zero checksum means that the transmitter generated no
36ba4e58ecSGerrit Renker * checksum. UDP-Lite (like IPv6) mandates checksums, hence packets
37ba4e58ecSGerrit Renker * with a zero checksum field are illegal. */
38ba4e58ecSGerrit Renker if (uh->check == 0) {
39ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLite: zeroed checksum field\n");
40ba4e58ecSGerrit Renker return 1;
41ba4e58ecSGerrit Renker }
42ba4e58ecSGerrit Renker
43ba4e58ecSGerrit Renker cscov = ntohs(uh->len);
44ba4e58ecSGerrit Renker
45ba4e58ecSGerrit Renker if (cscov == 0) /* Indicates that full coverage is required. */
46759e5d00SHerbert Xu ;
47ba4e58ecSGerrit Renker else if (cscov < 8 || cscov > skb->len) {
48ba4e58ecSGerrit Renker /*
49ba4e58ecSGerrit Renker * Coverage length violates RFC 3828: log and discard silently.
50ba4e58ecSGerrit Renker */
51ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLite: bad csum coverage %d/%d\n",
52ba4e58ecSGerrit Renker cscov, skb->len);
53ba4e58ecSGerrit Renker return 1;
54ba4e58ecSGerrit Renker
55759e5d00SHerbert Xu } else if (cscov < skb->len) {
56ba4e58ecSGerrit Renker UDP_SKB_CB(skb)->partial_cov = 1;
57ba4e58ecSGerrit Renker UDP_SKB_CB(skb)->cscov = cscov;
58759e5d00SHerbert Xu if (skb->ip_summed == CHECKSUM_COMPLETE)
59ba4e58ecSGerrit Renker skb->ip_summed = CHECKSUM_NONE;
6015f35d49SAlexey Kodanev skb->csum_valid = 0;
61759e5d00SHerbert Xu }
62ba4e58ecSGerrit Renker
63ba4e58ecSGerrit Renker return 0;
64ba4e58ecSGerrit Renker }
65ba4e58ecSGerrit Renker
66f36c23bbSGerrit Renker /* Fast-path computation of checksum. Socket may not be locked. */
udplite_csum(struct sk_buff * skb)67f6b9664fSHerbert Xu static inline __wsum udplite_csum(struct sk_buff *skb)
68f6b9664fSHerbert Xu {
69f6b9664fSHerbert Xu const int off = skb_transport_offset(skb);
70*257e8d2eSEric Dumazet const struct sock *sk = skb->sk;
71f36c23bbSGerrit Renker int len = skb->len - off;
72f6b9664fSHerbert Xu
73*257e8d2eSEric Dumazet if (udp_test_bit(UDPLITE_SEND_CC, sk)) {
74*257e8d2eSEric Dumazet u16 pcslen = READ_ONCE(udp_sk(sk)->pcslen);
75*257e8d2eSEric Dumazet
76*257e8d2eSEric Dumazet if (pcslen < len) {
77*257e8d2eSEric Dumazet if (pcslen > 0)
78*257e8d2eSEric Dumazet len = pcslen;
79*257e8d2eSEric Dumazet udp_hdr(skb)->len = htons(pcslen);
80*257e8d2eSEric Dumazet }
81f36c23bbSGerrit Renker }
82f6b9664fSHerbert Xu skb->ip_summed = CHECKSUM_NONE; /* no HW support for checksumming */
83f6b9664fSHerbert Xu
84f36c23bbSGerrit Renker return skb_checksum(skb, off, len, 0);
85f6b9664fSHerbert Xu }
86f6b9664fSHerbert Xu
8716073439SJoe Perches void udplite4_register(void);
88ba4e58ecSGerrit Renker #endif /* _UDPLITE_H */
89