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