net: qualcomm: rmnet: show that an intermediate sum is zero
[linux-2.6-microblaze.git] / drivers / net / ethernet / qualcomm / rmnet / rmnet_map_data.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2013-2018, 2021, The Linux Foundation. All rights reserved.
3  *
4  * RMNET Data MAP protocol
5  */
6
7 #include <linux/netdevice.h>
8 #include <linux/ip.h>
9 #include <linux/ipv6.h>
10 #include <net/ip6_checksum.h>
11 #include <linux/bitfield.h>
12 #include "rmnet_config.h"
13 #include "rmnet_map.h"
14 #include "rmnet_private.h"
15
16 #define RMNET_MAP_DEAGGR_SPACING  64
17 #define RMNET_MAP_DEAGGR_HEADROOM (RMNET_MAP_DEAGGR_SPACING / 2)
18
19 static __sum16 *rmnet_map_get_csum_field(unsigned char protocol,
20                                          const void *txporthdr)
21 {
22         if (protocol == IPPROTO_TCP)
23                 return &((struct tcphdr *)txporthdr)->check;
24
25         if (protocol == IPPROTO_UDP)
26                 return &((struct udphdr *)txporthdr)->check;
27
28         return NULL;
29 }
30
31 static int
32 rmnet_map_ipv4_dl_csum_trailer(struct sk_buff *skb,
33                                struct rmnet_map_dl_csum_trailer *csum_trailer,
34                                struct rmnet_priv *priv)
35 {
36         struct iphdr *ip4h = (struct iphdr *)skb->data;
37         void *txporthdr = skb->data + ip4h->ihl * 4;
38         __sum16 *csum_field, pseudo_csum;
39         __sum16 ip_payload_csum;
40         __sum16 csum_value_final;
41
42         /* Computing the checksum over just the IPv4 header--including its
43          * checksum field--should yield 0.  If it doesn't, the IP header
44          * is bad, so return an error and let the IP layer drop it.
45          */
46         if (ip_fast_csum(ip4h, ip4h->ihl)) {
47                 priv->stats.csum_ip4_header_bad++;
48                 return -EINVAL;
49         }
50
51         /* We don't support checksum offload on IPv4 fragments */
52         if (ip_is_fragment(ip4h)) {
53                 priv->stats.csum_fragmented_pkt++;
54                 return -EOPNOTSUPP;
55         }
56
57         /* Checksum offload is only supported for UDP and TCP protocols */
58         csum_field = rmnet_map_get_csum_field(ip4h->protocol, txporthdr);
59         if (!csum_field) {
60                 priv->stats.csum_err_invalid_transport++;
61                 return -EPROTONOSUPPORT;
62         }
63
64         /* RFC 768: UDP checksum is optional for IPv4, and is 0 if unused */
65         if (!*csum_field && ip4h->protocol == IPPROTO_UDP) {
66                 priv->stats.csum_skipped++;
67                 return 0;
68         }
69
70         /* The checksum value in the trailer is computed over the entire
71          * IP packet, including the IP header and payload.  To derive the
72          * transport checksum from this, we first subract the contribution
73          * of the IP header from the trailer checksum.  We then add the
74          * checksum computed over the pseudo header.
75          *
76          * We verified above that the IP header contributes zero to the
77          * trailer checksum.  Therefore the checksum in the trailer is
78          * just the checksum computed over the IP payload.
79          */
80         ip_payload_csum = (__force __sum16)~csum_trailer->csum_value;
81
82         pseudo_csum = ~csum_tcpudp_magic(ip4h->saddr, ip4h->daddr,
83                                          ntohs(ip4h->tot_len) - ip4h->ihl * 4,
84                                          ip4h->protocol, 0);
85         pseudo_csum = csum16_add(ip_payload_csum, (__force __be16)pseudo_csum);
86
87         /* The trailer checksum *includes* the checksum in the transport
88          * header.  Adding that to the pseudo checksum will yield 0xffff
89          * ("negative 0") if the message arrived intact.
90          */
91         WARN_ON((__sum16)~pseudo_csum);
92         csum_value_final = ~csum16_sub(pseudo_csum, (__force __be16)*csum_field);
93
94         if (unlikely(!csum_value_final)) {
95                 switch (ip4h->protocol) {
96                 case IPPROTO_UDP:
97                         /* RFC 768 - DL4 1's complement rule for UDP csum 0 */
98                         csum_value_final = ~csum_value_final;
99                         break;
100
101                 case IPPROTO_TCP:
102                         /* DL4 Non-RFC compliant TCP checksum found */
103                         if (*csum_field == (__force __sum16)0xFFFF)
104                                 csum_value_final = ~csum_value_final;
105                         break;
106                 }
107         }
108
109         if (csum_value_final == *csum_field) {
110                 priv->stats.csum_ok++;
111                 return 0;
112         } else {
113                 priv->stats.csum_validation_failed++;
114                 return -EINVAL;
115         }
116 }
117
118 #if IS_ENABLED(CONFIG_IPV6)
119 static int
120 rmnet_map_ipv6_dl_csum_trailer(struct sk_buff *skb,
121                                struct rmnet_map_dl_csum_trailer *csum_trailer,
122                                struct rmnet_priv *priv)
123 {
124         struct ipv6hdr *ip6h = (struct ipv6hdr *)skb->data;
125         void *txporthdr = skb->data + sizeof(*ip6h);
126         __sum16 *csum_field, pseudo_csum;
127         __sum16 ip6_payload_csum;
128         __be16 ip_header_csum;
129         __sum16 csum_value_final;
130         u32 length;
131
132         /* Checksum offload is only supported for UDP and TCP protocols;
133          * the packet cannot include any IPv6 extension headers
134          */
135         csum_field = rmnet_map_get_csum_field(ip6h->nexthdr, txporthdr);
136         if (!csum_field) {
137                 priv->stats.csum_err_invalid_transport++;
138                 return -EPROTONOSUPPORT;
139         }
140
141         /* The checksum value in the trailer is computed over the entire
142          * IP packet, including the IP header and payload.  To derive the
143          * transport checksum from this, we first subract the contribution
144          * of the IP header from the trailer checksum.  We then add the
145          * checksum computed over the pseudo header.
146          */
147         ip_header_csum = (__force __be16)ip_fast_csum(ip6h, sizeof(*ip6h) / 4);
148         ip6_payload_csum = ~csum16_sub((__force __sum16)csum_trailer->csum_value,
149                                        ip_header_csum);
150
151         length = (ip6h->nexthdr == IPPROTO_UDP) ?
152                  ntohs(((struct udphdr *)txporthdr)->len) :
153                  ntohs(ip6h->payload_len);
154         pseudo_csum = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
155                                        length, ip6h->nexthdr, 0);
156         pseudo_csum = csum16_add(ip6_payload_csum, (__force __be16)pseudo_csum);
157
158         /* Adding the payload checksum to the pseudo checksum yields 0xffff
159          * ("negative 0") if the message arrived intact.
160          */
161         WARN_ON((__sum16)~pseudo_csum);
162         csum_value_final = ~csum16_sub(pseudo_csum, (__force __be16)*csum_field);
163
164         if (unlikely(csum_value_final == 0)) {
165                 switch (ip6h->nexthdr) {
166                 case IPPROTO_UDP:
167                         /* RFC 2460 section 8.1
168                          * DL6 One's complement rule for UDP checksum 0
169                          */
170                         csum_value_final = ~csum_value_final;
171                         break;
172
173                 case IPPROTO_TCP:
174                         /* DL6 Non-RFC compliant TCP checksum found */
175                         if (*csum_field == (__force __sum16)0xFFFF)
176                                 csum_value_final = ~csum_value_final;
177                         break;
178                 }
179         }
180
181         if (csum_value_final == *csum_field) {
182                 priv->stats.csum_ok++;
183                 return 0;
184         } else {
185                 priv->stats.csum_validation_failed++;
186                 return -EINVAL;
187         }
188 }
189 #endif
190
191 static void rmnet_map_complement_ipv4_txporthdr_csum_field(void *iphdr)
192 {
193         struct iphdr *ip4h = (struct iphdr *)iphdr;
194         void *txphdr;
195         u16 *csum;
196
197         txphdr = iphdr + ip4h->ihl * 4;
198
199         if (ip4h->protocol == IPPROTO_TCP || ip4h->protocol == IPPROTO_UDP) {
200                 csum = (u16 *)rmnet_map_get_csum_field(ip4h->protocol, txphdr);
201                 *csum = ~(*csum);
202         }
203 }
204
205 static void
206 rmnet_map_ipv4_ul_csum_header(struct iphdr *iphdr,
207                               struct rmnet_map_ul_csum_header *ul_header,
208                               struct sk_buff *skb)
209 {
210         u16 val;
211
212         val = MAP_CSUM_UL_ENABLED_FLAG;
213         if (iphdr->protocol == IPPROTO_UDP)
214                 val |= MAP_CSUM_UL_UDP_FLAG;
215         val |= skb->csum_offset & MAP_CSUM_UL_OFFSET_MASK;
216
217         ul_header->csum_start_offset = htons(skb_network_header_len(skb));
218         ul_header->csum_info = htons(val);
219
220         skb->ip_summed = CHECKSUM_NONE;
221
222         rmnet_map_complement_ipv4_txporthdr_csum_field(iphdr);
223 }
224
225 #if IS_ENABLED(CONFIG_IPV6)
226 static void rmnet_map_complement_ipv6_txporthdr_csum_field(void *ip6hdr)
227 {
228         struct ipv6hdr *ip6h = (struct ipv6hdr *)ip6hdr;
229         void *txphdr;
230         u16 *csum;
231
232         txphdr = ip6hdr + sizeof(struct ipv6hdr);
233
234         if (ip6h->nexthdr == IPPROTO_TCP || ip6h->nexthdr == IPPROTO_UDP) {
235                 csum = (u16 *)rmnet_map_get_csum_field(ip6h->nexthdr, txphdr);
236                 *csum = ~(*csum);
237         }
238 }
239
240 static void
241 rmnet_map_ipv6_ul_csum_header(struct ipv6hdr *ipv6hdr,
242                               struct rmnet_map_ul_csum_header *ul_header,
243                               struct sk_buff *skb)
244 {
245         u16 val;
246
247         val = MAP_CSUM_UL_ENABLED_FLAG;
248         if (ipv6hdr->nexthdr == IPPROTO_UDP)
249                 val |= MAP_CSUM_UL_UDP_FLAG;
250         val |= skb->csum_offset & MAP_CSUM_UL_OFFSET_MASK;
251
252         ul_header->csum_start_offset = htons(skb_network_header_len(skb));
253         ul_header->csum_info = htons(val);
254
255         skb->ip_summed = CHECKSUM_NONE;
256
257         rmnet_map_complement_ipv6_txporthdr_csum_field(ipv6hdr);
258 }
259 #endif
260
261 static void rmnet_map_v5_checksum_uplink_packet(struct sk_buff *skb,
262                                                 struct rmnet_port *port,
263                                                 struct net_device *orig_dev)
264 {
265         struct rmnet_priv *priv = netdev_priv(orig_dev);
266         struct rmnet_map_v5_csum_header *ul_header;
267
268         ul_header = skb_push(skb, sizeof(*ul_header));
269         memset(ul_header, 0, sizeof(*ul_header));
270         ul_header->header_info = u8_encode_bits(RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD,
271                                                 MAPV5_HDRINFO_HDR_TYPE_FMASK);
272
273         if (skb->ip_summed == CHECKSUM_PARTIAL) {
274                 void *iph = ip_hdr(skb);
275                 __sum16 *check;
276                 void *trans;
277                 u8 proto;
278
279                 if (skb->protocol == htons(ETH_P_IP)) {
280                         u16 ip_len = ((struct iphdr *)iph)->ihl * 4;
281
282                         proto = ((struct iphdr *)iph)->protocol;
283                         trans = iph + ip_len;
284                 } else if (IS_ENABLED(CONFIG_IPV6) &&
285                            skb->protocol == htons(ETH_P_IPV6)) {
286                         u16 ip_len = sizeof(struct ipv6hdr);
287
288                         proto = ((struct ipv6hdr *)iph)->nexthdr;
289                         trans = iph + ip_len;
290                 } else {
291                         priv->stats.csum_err_invalid_ip_version++;
292                         goto sw_csum;
293                 }
294
295                 check = rmnet_map_get_csum_field(proto, trans);
296                 if (check) {
297                         skb->ip_summed = CHECKSUM_NONE;
298                         /* Ask for checksum offloading */
299                         ul_header->csum_info |= MAPV5_CSUMINFO_VALID_FLAG;
300                         priv->stats.csum_hw++;
301                         return;
302                 }
303         }
304
305 sw_csum:
306         priv->stats.csum_sw++;
307 }
308
309 /* Adds MAP header to front of skb->data
310  * Padding is calculated and set appropriately in MAP header. Mux ID is
311  * initialized to 0.
312  */
313 struct rmnet_map_header *rmnet_map_add_map_header(struct sk_buff *skb,
314                                                   int hdrlen,
315                                                   struct rmnet_port *port,
316                                                   int pad)
317 {
318         struct rmnet_map_header *map_header;
319         u32 padding, map_datalen;
320         u8 *padbytes;
321
322         map_datalen = skb->len - hdrlen;
323         map_header = (struct rmnet_map_header *)
324                         skb_push(skb, sizeof(struct rmnet_map_header));
325         memset(map_header, 0, sizeof(struct rmnet_map_header));
326
327         /* Set next_hdr bit for csum offload packets */
328         if (port->data_format & RMNET_FLAGS_EGRESS_MAP_CKSUMV5)
329                 map_header->flags |= MAP_NEXT_HEADER_FLAG;
330
331         if (pad == RMNET_MAP_NO_PAD_BYTES) {
332                 map_header->pkt_len = htons(map_datalen);
333                 return map_header;
334         }
335
336         BUILD_BUG_ON(MAP_PAD_LEN_MASK < 3);
337         padding = ALIGN(map_datalen, 4) - map_datalen;
338
339         if (padding == 0)
340                 goto done;
341
342         if (skb_tailroom(skb) < padding)
343                 return NULL;
344
345         padbytes = (u8 *)skb_put(skb, padding);
346         memset(padbytes, 0, padding);
347
348 done:
349         map_header->pkt_len = htons(map_datalen + padding);
350         /* This is a data packet, so the CMD bit is 0 */
351         map_header->flags = padding & MAP_PAD_LEN_MASK;
352
353         return map_header;
354 }
355
356 /* Deaggregates a single packet
357  * A whole new buffer is allocated for each portion of an aggregated frame.
358  * Caller should keep calling deaggregate() on the source skb until 0 is
359  * returned, indicating that there are no more packets to deaggregate. Caller
360  * is responsible for freeing the original skb.
361  */
362 struct sk_buff *rmnet_map_deaggregate(struct sk_buff *skb,
363                                       struct rmnet_port *port)
364 {
365         struct rmnet_map_v5_csum_header *next_hdr = NULL;
366         struct rmnet_map_header *maph;
367         void *data = skb->data;
368         struct sk_buff *skbn;
369         u8 nexthdr_type;
370         u32 packet_len;
371
372         if (skb->len == 0)
373                 return NULL;
374
375         maph = (struct rmnet_map_header *)skb->data;
376         packet_len = ntohs(maph->pkt_len) + sizeof(*maph);
377
378         if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV4) {
379                 packet_len += sizeof(struct rmnet_map_dl_csum_trailer);
380         } else if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV5) {
381                 if (!(maph->flags & MAP_CMD_FLAG)) {
382                         packet_len += sizeof(*next_hdr);
383                         if (maph->flags & MAP_NEXT_HEADER_FLAG)
384                                 next_hdr = data + sizeof(*maph);
385                         else
386                                 /* Mapv5 data pkt without csum hdr is invalid */
387                                 return NULL;
388                 }
389         }
390
391         if (((int)skb->len - (int)packet_len) < 0)
392                 return NULL;
393
394         /* Some hardware can send us empty frames. Catch them */
395         if (!maph->pkt_len)
396                 return NULL;
397
398         if (next_hdr) {
399                 nexthdr_type = u8_get_bits(next_hdr->header_info,
400                                            MAPV5_HDRINFO_HDR_TYPE_FMASK);
401                 if (nexthdr_type != RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD)
402                         return NULL;
403         }
404
405         skbn = alloc_skb(packet_len + RMNET_MAP_DEAGGR_SPACING, GFP_ATOMIC);
406         if (!skbn)
407                 return NULL;
408
409         skb_reserve(skbn, RMNET_MAP_DEAGGR_HEADROOM);
410         skb_put(skbn, packet_len);
411         memcpy(skbn->data, skb->data, packet_len);
412         skb_pull(skb, packet_len);
413
414         return skbn;
415 }
416
417 /* Validates packet checksums. Function takes a pointer to
418  * the beginning of a buffer which contains the IP payload +
419  * padding + checksum trailer.
420  * Only IPv4 and IPv6 are supported along with TCP & UDP.
421  * Fragmented or tunneled packets are not supported.
422  */
423 int rmnet_map_checksum_downlink_packet(struct sk_buff *skb, u16 len)
424 {
425         struct rmnet_priv *priv = netdev_priv(skb->dev);
426         struct rmnet_map_dl_csum_trailer *csum_trailer;
427
428         if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
429                 priv->stats.csum_sw++;
430                 return -EOPNOTSUPP;
431         }
432
433         csum_trailer = (struct rmnet_map_dl_csum_trailer *)(skb->data + len);
434
435         if (!(csum_trailer->flags & MAP_CSUM_DL_VALID_FLAG)) {
436                 priv->stats.csum_valid_unset++;
437                 return -EINVAL;
438         }
439
440         if (skb->protocol == htons(ETH_P_IP))
441                 return rmnet_map_ipv4_dl_csum_trailer(skb, csum_trailer, priv);
442
443         if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6))
444                 return rmnet_map_ipv6_dl_csum_trailer(skb, csum_trailer, priv);
445
446         priv->stats.csum_err_invalid_ip_version++;
447
448         return -EPROTONOSUPPORT;
449 }
450
451 static void rmnet_map_v4_checksum_uplink_packet(struct sk_buff *skb,
452                                                 struct net_device *orig_dev)
453 {
454         struct rmnet_priv *priv = netdev_priv(orig_dev);
455         struct rmnet_map_ul_csum_header *ul_header;
456         void *iphdr;
457
458         ul_header = (struct rmnet_map_ul_csum_header *)
459                     skb_push(skb, sizeof(struct rmnet_map_ul_csum_header));
460
461         if (unlikely(!(orig_dev->features &
462                      (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))))
463                 goto sw_csum;
464
465         if (skb->ip_summed != CHECKSUM_PARTIAL)
466                 goto sw_csum;
467
468         iphdr = (char *)ul_header +
469                 sizeof(struct rmnet_map_ul_csum_header);
470
471         if (skb->protocol == htons(ETH_P_IP)) {
472                 rmnet_map_ipv4_ul_csum_header(iphdr, ul_header, skb);
473                 priv->stats.csum_hw++;
474                 return;
475         }
476
477         if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6)) {
478                 rmnet_map_ipv6_ul_csum_header(iphdr, ul_header, skb);
479                 priv->stats.csum_hw++;
480                 return;
481         }
482
483         priv->stats.csum_err_invalid_ip_version++;
484
485 sw_csum:
486         memset(ul_header, 0, sizeof(*ul_header));
487
488         priv->stats.csum_sw++;
489 }
490
491 /* Generates UL checksum meta info header for IPv4 and IPv6 over TCP and UDP
492  * packets that are supported for UL checksum offload.
493  */
494 void rmnet_map_checksum_uplink_packet(struct sk_buff *skb,
495                                       struct rmnet_port *port,
496                                       struct net_device *orig_dev,
497                                       int csum_type)
498 {
499         switch (csum_type) {
500         case RMNET_FLAGS_EGRESS_MAP_CKSUMV4:
501                 rmnet_map_v4_checksum_uplink_packet(skb, orig_dev);
502                 break;
503         case RMNET_FLAGS_EGRESS_MAP_CKSUMV5:
504                 rmnet_map_v5_checksum_uplink_packet(skb, port, orig_dev);
505                 break;
506         default:
507                 break;
508         }
509 }
510
511 /* Process a MAPv5 packet header */
512 int rmnet_map_process_next_hdr_packet(struct sk_buff *skb,
513                                       u16 len)
514 {
515         struct rmnet_priv *priv = netdev_priv(skb->dev);
516         struct rmnet_map_v5_csum_header *next_hdr;
517         u8 nexthdr_type;
518
519         next_hdr = (struct rmnet_map_v5_csum_header *)(skb->data +
520                         sizeof(struct rmnet_map_header));
521
522         nexthdr_type = u8_get_bits(next_hdr->header_info,
523                                    MAPV5_HDRINFO_HDR_TYPE_FMASK);
524
525         if (nexthdr_type != RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD)
526                 return -EINVAL;
527
528         if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
529                 priv->stats.csum_sw++;
530         } else if (next_hdr->csum_info & MAPV5_CSUMINFO_VALID_FLAG) {
531                 priv->stats.csum_ok++;
532                 skb->ip_summed = CHECKSUM_UNNECESSARY;
533         } else {
534                 priv->stats.csum_valid_unset++;
535         }
536
537         /* Pull csum v5 header */
538         skb_pull(skb, sizeof(*next_hdr));
539
540         return 0;
541 }