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