blob: cc377bb2e1a3dd48c2800fec0d35cea8a4ef9d8a [file] [log] [blame]
Subash Abhinov Kasiviswanathan2698fe92018-01-02 11:55:05 -07001/* Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -06002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * RMNET Data MAP protocol
13 */
14
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/skbuff.h>
18#include <linux/netdevice.h>
19#include <linux/rmnet_data.h>
20#include <linux/spinlock.h>
Subash Abhinov Kasiviswanathanfe3dede2017-12-01 12:35:18 -070021#include <linux/workqueue.h>
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -060022#include <linux/time.h>
23#include <linux/net_map.h>
24#include <linux/ip.h>
25#include <linux/ipv6.h>
26#include <linux/udp.h>
27#include <linux/tcp.h>
28#include <linux/in.h>
29#include <net/ip.h>
30#include <net/checksum.h>
31#include <net/ip6_checksum.h>
32#include <net/rmnet_config.h>
33#include "rmnet_data_config.h"
34#include "rmnet_map.h"
35#include "rmnet_data_private.h"
36#include "rmnet_data_stats.h"
37#include "rmnet_data_trace.h"
38
39RMNET_LOG_MODULE(RMNET_DATA_LOGMASK_MAPD);
40
41/* Local Definitions */
42
43long agg_time_limit __read_mostly = 1000000L;
44module_param(agg_time_limit, long, 0644);
45MODULE_PARM_DESC(agg_time_limit, "Maximum time packets sit in the agg buf");
46
47long agg_bypass_time __read_mostly = 10000000L;
48module_param(agg_bypass_time, long, 0644);
49MODULE_PARM_DESC(agg_bypass_time, "Skip agg when apart spaced more than this");
50
Subash Abhinov Kasiviswanathanfe3dede2017-12-01 12:35:18 -070051struct agg_work {
Subash Abhinov Kasiviswanathan08fed022017-11-17 17:54:49 -070052 struct work_struct work;
Subash Abhinov Kasiviswanathanfe3dede2017-12-01 12:35:18 -070053 struct rmnet_phys_ep_config *config;
54};
55
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -060056#define RMNET_MAP_DEAGGR_SPACING 64
57#define RMNET_MAP_DEAGGR_HEADROOM (RMNET_MAP_DEAGGR_SPACING / 2)
58
59/* rmnet_map_add_map_header() - Adds MAP header to front of skb->data
60 * @skb: Socket buffer ("packet") to modify
61 * @hdrlen: Number of bytes of header data which should not be included in
62 * MAP length field
63 * @pad: Specify if padding the MAP packet to make it 4 byte aligned is
64 * necessary
65 *
66 * Padding is calculated and set appropriately in MAP header. Mux ID is
67 * initialized to 0.
68 *
69 * Return:
70 * - Pointer to MAP structure
71 * - 0 (null) if insufficient headroom
72 * - 0 (null) if insufficient tailroom for padding bytes
73 */
74struct rmnet_map_header_s *rmnet_map_add_map_header(struct sk_buff *skb,
75 int hdrlen, int pad)
76{
77 u32 padding, map_datalen;
78 u8 *padbytes;
79 struct rmnet_map_header_s *map_header;
80
81 if (skb_headroom(skb) < sizeof(struct rmnet_map_header_s))
82 return 0;
83
84 map_datalen = skb->len - hdrlen;
85 map_header = (struct rmnet_map_header_s *)
86 skb_push(skb, sizeof(struct rmnet_map_header_s));
87 memset(map_header, 0, sizeof(struct rmnet_map_header_s));
88
89 if (pad == RMNET_MAP_NO_PAD_BYTES) {
90 map_header->pkt_len = htons(map_datalen);
91 return map_header;
92 }
93
94 padding = ALIGN(map_datalen, 4) - map_datalen;
95
96 if (padding == 0)
97 goto done;
98
99 if (skb_tailroom(skb) < padding)
100 return 0;
101
102 padbytes = (u8 *)skb_put(skb, padding);
103 LOGD("pad: %d", padding);
104 memset(padbytes, 0, padding);
105
106done:
107 map_header->pkt_len = htons(map_datalen + padding);
108 map_header->pad_len = padding & 0x3F;
109
110 return map_header;
111}
112
113/* rmnet_map_deaggregate() - Deaggregates a single packet
114 * @skb: Source socket buffer containing multiple MAP frames
115 * @config: Physical endpoint configuration of the ingress device
116 *
117 * A whole new buffer is allocated for each portion of an aggregated frame.
118 * Caller should keep calling deaggregate() on the source skb until 0 is
119 * returned, indicating that there are no more packets to deaggregate. Caller
120 * is responsible for freeing the original skb.
121 *
122 * Return:
123 * - Pointer to new skb
124 * - 0 (null) if no more aggregated packets
125 */
126struct sk_buff *rmnet_map_deaggregate(struct sk_buff *skb,
127 struct rmnet_phys_ep_config *config)
128{
129 struct sk_buff *skbn;
130 struct rmnet_map_header_s *maph;
131 u32 packet_len;
132
133 if (skb->len == 0)
134 return 0;
135
136 maph = (struct rmnet_map_header_s *)skb->data;
137 packet_len = ntohs(maph->pkt_len) + sizeof(struct rmnet_map_header_s);
138
139 if ((config->ingress_data_format & RMNET_INGRESS_FORMAT_MAP_CKSUMV3) ||
140 (config->ingress_data_format & RMNET_INGRESS_FORMAT_MAP_CKSUMV4))
141 packet_len += sizeof(struct rmnet_map_dl_checksum_trailer_s);
142
143 if ((((int)skb->len) - ((int)packet_len)) < 0) {
144 LOGM("%s", "Got malformed packet. Dropping");
145 return 0;
146 }
147
148 skbn = alloc_skb(packet_len + RMNET_MAP_DEAGGR_SPACING, GFP_ATOMIC);
149 if (!skbn)
150 return 0;
151
152 skbn->dev = skb->dev;
153 skb_reserve(skbn, RMNET_MAP_DEAGGR_HEADROOM);
154 skb_put(skbn, packet_len);
155 memcpy(skbn->data, skb->data, packet_len);
156 skb_pull(skb, packet_len);
157
158 /* Some hardware can send us empty frames. Catch them */
159 if (ntohs(maph->pkt_len) == 0) {
160 LOGD("Dropping empty MAP frame");
161 rmnet_kfree_skb(skbn, RMNET_STATS_SKBFREE_DEAGG_DATA_LEN_0);
162 return 0;
163 }
164
165 return skbn;
166}
167
Subash Abhinov Kasiviswanathan08fed022017-11-17 17:54:49 -0700168static void rmnet_map_flush_packet_work(struct work_struct *work)
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600169{
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600170 struct rmnet_phys_ep_config *config;
Subash Abhinov Kasiviswanathan08fed022017-11-17 17:54:49 -0700171 struct agg_work *real_work;
172 int rc, agg_count = 0;
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600173 unsigned long flags;
174 struct sk_buff *skb;
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600175
Subash Abhinov Kasiviswanathanfe3dede2017-12-01 12:35:18 -0700176 real_work = (struct agg_work *)work;
177 config = real_work->config;
Subash Abhinov Kasiviswanathan08fed022017-11-17 17:54:49 -0700178 skb = NULL;
179
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600180 LOGD("%s", "Entering flush thread");
181 spin_lock_irqsave(&config->agg_lock, flags);
182 if (likely(config->agg_state == RMNET_MAP_TXFER_SCHEDULED)) {
183 /* Buffer may have already been shipped out */
184 if (likely(config->agg_skb)) {
185 rmnet_stats_agg_pkts(config->agg_count);
186 if (config->agg_count > 1)
187 LOGL("Agg count: %d", config->agg_count);
188 skb = config->agg_skb;
189 agg_count = config->agg_count;
Subash Abhinov Kasiviswanathan08fed022017-11-17 17:54:49 -0700190 config->agg_skb = NULL;
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600191 config->agg_count = 0;
192 memset(&config->agg_time, 0, sizeof(struct timespec));
193 }
194 config->agg_state = RMNET_MAP_AGG_IDLE;
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600195 }
196
197 spin_unlock_irqrestore(&config->agg_lock, flags);
198 if (skb) {
199 trace_rmnet_map_flush_packet_queue(skb, agg_count);
200 rc = dev_queue_xmit(skb);
201 rmnet_stats_queue_xmit(rc, RMNET_STATS_QUEUE_XMIT_AGG_TIMEOUT);
202 }
Subash Abhinov Kasiviswanathan08fed022017-11-17 17:54:49 -0700203
Subash Abhinov Kasiviswanathanfe3dede2017-12-01 12:35:18 -0700204 kfree(work);
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600205}
206
Subash Abhinov Kasiviswanathan08fed022017-11-17 17:54:49 -0700207/* rmnet_map_flush_packet_queue() - Transmits aggregeted frame on timeout
208 *
209 * This function is scheduled to run in a specified number of ns after
210 * the last frame transmitted by the network stack. When run, the buffer
211 * containing aggregated packets is finally transmitted on the underlying link.
212 *
213 */
214enum hrtimer_restart rmnet_map_flush_packet_queue(struct hrtimer *t)
215{
216 struct rmnet_phys_ep_config *config;
217 struct agg_work *work;
218
219 config = container_of(t, struct rmnet_phys_ep_config, hrtimer);
220
221 work = kmalloc(sizeof(*work), GFP_ATOMIC);
222 if (!work) {
223 config->agg_state = RMNET_MAP_AGG_IDLE;
224
225 return HRTIMER_NORESTART;
226 }
227
228 INIT_WORK(&work->work, rmnet_map_flush_packet_work);
229 work->config = config;
230 schedule_work((struct work_struct *)work);
231 return HRTIMER_NORESTART;
232}
233
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600234/* rmnet_map_aggregate() - Software aggregates multiple packets.
235 * @skb: current packet being transmitted
236 * @config: Physical endpoint configuration of the ingress device
237 *
238 * Aggregates multiple SKBs into a single large SKB for transmission. MAP
239 * protocol is used to separate the packets in the buffer. This function
240 * consumes the argument SKB and should not be further processed by any other
241 * function.
242 */
243void rmnet_map_aggregate(struct sk_buff *skb,
244 struct rmnet_phys_ep_config *config) {
245 u8 *dest_buff;
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600246 unsigned long flags;
247 struct sk_buff *agg_skb;
248 struct timespec diff, last;
249 int size, rc, agg_count = 0;
250
251 if (!skb || !config)
252 return;
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600253
254new_packet:
255 spin_lock_irqsave(&config->agg_lock, flags);
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600256 memcpy(&last, &config->agg_last, sizeof(struct timespec));
257 getnstimeofday(&config->agg_last);
258
259 if (!config->agg_skb) {
260 /* Check to see if we should agg first. If the traffic is very
261 * sparse, don't aggregate. We will need to tune this later
262 */
263 diff = timespec_sub(config->agg_last, last);
264
265 if ((diff.tv_sec > 0) || (diff.tv_nsec > agg_bypass_time)) {
266 spin_unlock_irqrestore(&config->agg_lock, flags);
267 LOGL("delta t: %ld.%09lu\tcount: bypass", diff.tv_sec,
268 diff.tv_nsec);
269 rmnet_stats_agg_pkts(1);
270 trace_rmnet_map_aggregate(skb, 0);
271 rc = dev_queue_xmit(skb);
272 rmnet_stats_queue_xmit(rc,
273 RMNET_STATS_QUEUE_XMIT_AGG_SKIP);
274 return;
275 }
276
Subash Abhinov Kasiviswanathanbf783ba2017-11-08 15:55:29 -0700277 size = config->egress_agg_size - skb->len;
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600278 config->agg_skb = skb_copy_expand(skb, 0, size, GFP_ATOMIC);
279 if (!config->agg_skb) {
280 config->agg_skb = 0;
281 config->agg_count = 0;
282 memset(&config->agg_time, 0, sizeof(struct timespec));
283 spin_unlock_irqrestore(&config->agg_lock, flags);
284 rmnet_stats_agg_pkts(1);
285 trace_rmnet_map_aggregate(skb, 0);
286 rc = dev_queue_xmit(skb);
287 rmnet_stats_queue_xmit
288 (rc,
289 RMNET_STATS_QUEUE_XMIT_AGG_CPY_EXP_FAIL);
290 return;
291 }
292 config->agg_count = 1;
293 getnstimeofday(&config->agg_time);
294 trace_rmnet_start_aggregation(skb);
295 rmnet_kfree_skb(skb, RMNET_STATS_SKBFREE_AGG_CPY_EXPAND);
296 goto schedule;
297 }
298 diff = timespec_sub(config->agg_last, config->agg_time);
299
300 if (skb->len > (config->egress_agg_size - config->agg_skb->len) ||
301 (config->agg_count >= config->egress_agg_count) ||
302 (diff.tv_sec > 0) || (diff.tv_nsec > agg_time_limit)) {
303 rmnet_stats_agg_pkts(config->agg_count);
304 agg_skb = config->agg_skb;
305 agg_count = config->agg_count;
306 config->agg_skb = 0;
307 config->agg_count = 0;
308 memset(&config->agg_time, 0, sizeof(struct timespec));
Subash Abhinov Kasiviswanathan08fed022017-11-17 17:54:49 -0700309 config->agg_state = RMNET_MAP_AGG_IDLE;
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600310 spin_unlock_irqrestore(&config->agg_lock, flags);
Subash Abhinov Kasiviswanathan08fed022017-11-17 17:54:49 -0700311 hrtimer_cancel(&config->hrtimer);
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600312 LOGL("delta t: %ld.%09lu\tcount: %d", diff.tv_sec,
313 diff.tv_nsec, agg_count);
314 trace_rmnet_map_aggregate(skb, agg_count);
315 rc = dev_queue_xmit(agg_skb);
316 rmnet_stats_queue_xmit(rc,
317 RMNET_STATS_QUEUE_XMIT_AGG_FILL_BUFFER);
318 goto new_packet;
319 }
320
321 dest_buff = skb_put(config->agg_skb, skb->len);
322 memcpy(dest_buff, skb->data, skb->len);
323 config->agg_count++;
324 rmnet_kfree_skb(skb, RMNET_STATS_SKBFREE_AGG_INTO_BUFF);
325
326schedule:
327 if (config->agg_state != RMNET_MAP_TXFER_SCHEDULED) {
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600328 config->agg_state = RMNET_MAP_TXFER_SCHEDULED;
Subash Abhinov Kasiviswanathan08fed022017-11-17 17:54:49 -0700329 hrtimer_start(&config->hrtimer, ns_to_ktime(3000000),
330 HRTIMER_MODE_REL);
Subash Abhinov Kasiviswanathan2139ce8a2016-10-14 11:01:48 -0600331 }
332 spin_unlock_irqrestore(&config->agg_lock, flags);
333}
334
335/* Checksum Offload */
336
337static inline u16 *rmnet_map_get_checksum_field(unsigned char protocol,
338 const void *txporthdr)
339{
340 u16 *check = 0;
341
342 switch (protocol) {
343 case IPPROTO_TCP:
344 check = &(((struct tcphdr *)txporthdr)->check);
345 break;
346
347 case IPPROTO_UDP:
348 check = &(((struct udphdr *)txporthdr)->check);
349 break;
350
351 default:
352 check = 0;
353 break;
354 }
355
356 return check;
357}
358
359static inline u16 rmnet_map_add_checksums(u16 val1, u16 val2)
360{
361 int sum = val1 + val2;
362
363 sum = (((sum & 0xFFFF0000) >> 16) + sum) & 0x0000FFFF;
364 return (u16)(sum & 0x0000FFFF);
365}
366
367static inline u16 rmnet_map_subtract_checksums(u16 val1, u16 val2)
368{
369 return rmnet_map_add_checksums(val1, ~val2);
370}
371
372/* rmnet_map_validate_ipv4_packet_checksum() - Validates TCP/UDP checksum
373 * value for IPv4 packet
374 * @map_payload: Pointer to the beginning of the map payload
375 * @cksum_trailer: Pointer to the checksum trailer
376 *
377 * Validates the TCP/UDP checksum for the packet using the checksum value
378 * from the checksum trailer added to the packet.
379 * The validation formula is the following:
380 * 1. Performs 1's complement over the checksum value from the trailer
381 * 2. Computes 1's complement checksum over IPv4 header and subtracts it from
382 * the value from step 1
383 * 3. Computes 1's complement checksum over IPv4 pseudo header and adds it to
384 * the value from step 2
385 * 4. Subtracts the checksum value from the TCP/UDP header from the value from
386 * step 3
387 * 5. Compares the value from step 4 to the checksum value from the TCP/UDP
388 * header
389 *
390 * Fragmentation and tunneling are not supported.
391 *
392 * Return: 0 is validation succeeded.
393 */
394static int rmnet_map_validate_ipv4_packet_checksum
395 (unsigned char *map_payload,
396 struct rmnet_map_dl_checksum_trailer_s *cksum_trailer)
397{
398 struct iphdr *ip4h;
399 u16 *checksum_field;
400 void *txporthdr;
401 u16 pseudo_checksum;
402 u16 ip_hdr_checksum;
403 u16 checksum_value;
404 u16 ip_payload_checksum;
405 u16 ip_pseudo_payload_checksum;
406 u16 checksum_value_final;
407
408 ip4h = (struct iphdr *)map_payload;
409 if ((ntohs(ip4h->frag_off) & IP_MF) ||
410 ((ntohs(ip4h->frag_off) & IP_OFFSET) > 0))
411 return RMNET_MAP_CHECKSUM_FRAGMENTED_PACKET;
412
413 txporthdr = map_payload + ip4h->ihl * 4;
414
415 checksum_field = rmnet_map_get_checksum_field(ip4h->protocol,
416 txporthdr);
417
418 if (unlikely(!checksum_field))
419 return RMNET_MAP_CHECKSUM_ERR_UNKNOWN_TRANSPORT;
420
421 /* RFC 768 - Skip IPv4 UDP packets where sender checksum field is 0 */
422 if ((*checksum_field == 0) && (ip4h->protocol == IPPROTO_UDP))
423 return RMNET_MAP_CHECKSUM_SKIPPED;
424
425 checksum_value = ~ntohs(cksum_trailer->checksum_value);
426 ip_hdr_checksum = ~ip_fast_csum(ip4h, (int)ip4h->ihl);
427 ip_payload_checksum = rmnet_map_subtract_checksums(checksum_value,
428 ip_hdr_checksum);
429
430 pseudo_checksum = ~ntohs(csum_tcpudp_magic(ip4h->saddr, ip4h->daddr,
431 (u16)(ntohs(ip4h->tot_len) - ip4h->ihl * 4),
432 (u16)ip4h->protocol, 0));
433 ip_pseudo_payload_checksum = rmnet_map_add_checksums(
434 ip_payload_checksum, pseudo_checksum);
435
436 checksum_value_final = ~rmnet_map_subtract_checksums(
437 ip_pseudo_payload_checksum, ntohs(*checksum_field));
438
439 if (unlikely(checksum_value_final == 0)) {
440 switch (ip4h->protocol) {
441 case IPPROTO_UDP:
442 /* RFC 768 */
443 LOGD("DL4 1's complement rule for UDP checksum 0");
444 checksum_value_final = ~checksum_value_final;
445 break;
446
447 case IPPROTO_TCP:
448 if (*checksum_field == 0xFFFF) {
449 LOGD(
450 "DL4 Non-RFC compliant TCP checksum found");
451 checksum_value_final = ~checksum_value_final;
452 }
453 break;
454 }
455 }
456
457 LOGD(
458 "DL4 cksum: ~HW: %04X, field: %04X, pseudo header: %04X, final: %04X",
459 ~ntohs(cksum_trailer->checksum_value), ntohs(*checksum_field),
460 pseudo_checksum, checksum_value_final);
461
462 if (checksum_value_final == ntohs(*checksum_field))
463 return RMNET_MAP_CHECKSUM_OK;
464 else
465 return RMNET_MAP_CHECKSUM_VALIDATION_FAILED;
466}
467
468/* rmnet_map_validate_ipv6_packet_checksum() - Validates TCP/UDP checksum
469 * value for IPv6 packet
470 * @map_payload: Pointer to the beginning of the map payload
471 * @cksum_trailer: Pointer to the checksum trailer
472 *
473 * Validates the TCP/UDP checksum for the packet using the checksum value
474 * from the checksum trailer added to the packet.
475 * The validation formula is the following:
476 * 1. Performs 1's complement over the checksum value from the trailer
477 * 2. Computes 1's complement checksum over IPv6 header and subtracts it from
478 * the value from step 1
479 * 3. Computes 1's complement checksum over IPv6 pseudo header and adds it to
480 * the value from step 2
481 * 4. Subtracts the checksum value from the TCP/UDP header from the value from
482 * step 3
483 * 5. Compares the value from step 4 to the checksum value from the TCP/UDP
484 * header
485 *
486 * Fragmentation, extension headers and tunneling are not supported.
487 *
488 * Return: 0 is validation succeeded.
489 */
490static int rmnet_map_validate_ipv6_packet_checksum
491 (unsigned char *map_payload,
492 struct rmnet_map_dl_checksum_trailer_s *cksum_trailer)
493{
494 struct ipv6hdr *ip6h;
495 u16 *checksum_field;
496 void *txporthdr;
497 u16 pseudo_checksum;
498 u16 ip_hdr_checksum;
499 u16 checksum_value;
500 u16 ip_payload_checksum;
501 u16 ip_pseudo_payload_checksum;
502 u16 checksum_value_final;
503 u32 length;
504
505 ip6h = (struct ipv6hdr *)map_payload;
506
507 txporthdr = map_payload + sizeof(struct ipv6hdr);
508 checksum_field = rmnet_map_get_checksum_field(ip6h->nexthdr,
509 txporthdr);
510
511 if (unlikely(!checksum_field))
512 return RMNET_MAP_CHECKSUM_ERR_UNKNOWN_TRANSPORT;
513
514 checksum_value = ~ntohs(cksum_trailer->checksum_value);
515 ip_hdr_checksum = ~ntohs(ip_compute_csum(ip6h,
516 (int)(txporthdr - (void *)map_payload)));
517 ip_payload_checksum = rmnet_map_subtract_checksums
518 (checksum_value, ip_hdr_checksum);
519
520 length = (ip6h->nexthdr == IPPROTO_UDP) ?
521 ntohs(((struct udphdr *)txporthdr)->len) :
522 ntohs(ip6h->payload_len);
523 pseudo_checksum = ~ntohs(csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
524 length, ip6h->nexthdr, 0));
525 ip_pseudo_payload_checksum = rmnet_map_add_checksums(
526 ip_payload_checksum, pseudo_checksum);
527
528 checksum_value_final = ~rmnet_map_subtract_checksums(
529 ip_pseudo_payload_checksum, ntohs(*checksum_field));
530
531 if (unlikely(checksum_value_final == 0)) {
532 switch (ip6h->nexthdr) {
533 case IPPROTO_UDP:
534 /* RFC 2460 section 8.1 */
535 LOGD("DL6 One's complement rule for UDP checksum 0");
536 checksum_value_final = ~checksum_value_final;
537 break;
538
539 case IPPROTO_TCP:
540 if (*checksum_field == 0xFFFF) {
541 LOGD(
542 "DL6 Non-RFC compliant TCP checksum found");
543 checksum_value_final = ~checksum_value_final;
544 }
545 break;
546 }
547 }
548
549 LOGD(
550 "DL6 cksum: ~HW: %04X, field: %04X, pseudo header: %04X, final: %04X",
551 ~ntohs(cksum_trailer->checksum_value), ntohs(*checksum_field),
552 pseudo_checksum, checksum_value_final);
553
554 if (checksum_value_final == ntohs(*checksum_field))
555 return RMNET_MAP_CHECKSUM_OK;
556 else
557 return RMNET_MAP_CHECKSUM_VALIDATION_FAILED;
558 }
559
560/* rmnet_map_checksum_downlink_packet() - Validates checksum on
561 * a downlink packet
562 * @skb: Pointer to the packet's skb.
563 *
564 * Validates packet checksums. Function takes a pointer to
565 * the beginning of a buffer which contains the entire MAP
566 * frame: MAP header + IP payload + padding + checksum trailer.
567 * Currently, only IPv4 and IPv6 are supported along with
568 * TCP & UDP. Fragmented or tunneled packets are not supported.
569 *
570 * Return:
571 * - RMNET_MAP_CHECKSUM_OK: Validation of checksum succeeded.
572 * - RMNET_MAP_CHECKSUM_ERR_BAD_BUFFER: Skb buffer given is corrupted.
573 * - RMNET_MAP_CHECKSUM_VALID_FLAG_NOT_SET: Valid flag is not set in the
574 * checksum trailer.
575 * - RMNET_MAP_CHECKSUM_FRAGMENTED_PACKET: The packet is a fragment.
576 * - RMNET_MAP_CHECKSUM_ERR_UNKNOWN_TRANSPORT: The transport header is
577 * not TCP/UDP.
578 * - RMNET_MAP_CHECKSUM_ERR_UNKNOWN_IP_VERSION: Unrecognized IP header.
579 * - RMNET_MAP_CHECKSUM_VALIDATION_FAILED: In case the validation failed.
580 */
581int rmnet_map_checksum_downlink_packet(struct sk_buff *skb)
582{
583 struct rmnet_map_dl_checksum_trailer_s *cksum_trailer;
584 unsigned int data_len;
585 unsigned char *map_payload;
586 unsigned char ip_version;
587
588 data_len = RMNET_MAP_GET_LENGTH(skb);
589
590 if (unlikely(skb->len < (sizeof(struct rmnet_map_header_s) + data_len +
591 sizeof(struct rmnet_map_dl_checksum_trailer_s))))
592 return RMNET_MAP_CHECKSUM_ERR_BAD_BUFFER;
593
594 cksum_trailer = (struct rmnet_map_dl_checksum_trailer_s *)
595 (skb->data + data_len
596 + sizeof(struct rmnet_map_header_s));
597
598 if (unlikely(!ntohs(cksum_trailer->valid)))
599 return RMNET_MAP_CHECKSUM_VALID_FLAG_NOT_SET;
600
601 map_payload = (unsigned char *)(skb->data
602 + sizeof(struct rmnet_map_header_s));
603
604 ip_version = (*map_payload & 0xF0) >> 4;
605 if (ip_version == 0x04)
606 return rmnet_map_validate_ipv4_packet_checksum(map_payload,
607 cksum_trailer);
608 else if (ip_version == 0x06)
609 return rmnet_map_validate_ipv6_packet_checksum(map_payload,
610 cksum_trailer);
611
612 return RMNET_MAP_CHECKSUM_ERR_UNKNOWN_IP_VERSION;
613}
614
615static void rmnet_map_fill_ipv4_packet_ul_checksum_header
616 (void *iphdr, struct rmnet_map_ul_checksum_header_s *ul_header,
617 struct sk_buff *skb)
618{
619 struct iphdr *ip4h = (struct iphdr *)iphdr;
620 unsigned short *hdr = (unsigned short *)ul_header;
621
622 ul_header->checksum_start_offset = htons((unsigned short)
623 (skb_transport_header(skb) - (unsigned char *)iphdr));
624 ul_header->checksum_insert_offset = skb->csum_offset;
625 ul_header->cks_en = 1;
626 if (ip4h->protocol == IPPROTO_UDP)
627 ul_header->udp_ip4_ind = 1;
628 else
629 ul_header->udp_ip4_ind = 0;
630 /* Changing checksum_insert_offset to network order */
631 hdr++;
632 *hdr = htons(*hdr);
633 skb->ip_summed = CHECKSUM_NONE;
634}
635
636static void rmnet_map_fill_ipv6_packet_ul_checksum_header
637 (void *iphdr, struct rmnet_map_ul_checksum_header_s *ul_header,
638 struct sk_buff *skb)
639{
640 unsigned short *hdr = (unsigned short *)ul_header;
641
642 ul_header->checksum_start_offset = htons((unsigned short)
643 (skb_transport_header(skb) - (unsigned char *)iphdr));
644 ul_header->checksum_insert_offset = skb->csum_offset;
645 ul_header->cks_en = 1;
646 ul_header->udp_ip4_ind = 0;
647 /* Changing checksum_insert_offset to network order */
648 hdr++;
649 *hdr = htons(*hdr);
650 skb->ip_summed = CHECKSUM_NONE;
651}
652
653static void rmnet_map_complement_ipv4_txporthdr_csum_field(void *iphdr)
654{
655 struct iphdr *ip4h = (struct iphdr *)iphdr;
656 void *txporthdr;
657 u16 *csum;
658
659 txporthdr = iphdr + ip4h->ihl * 4;
660
661 if ((ip4h->protocol == IPPROTO_TCP) ||
662 (ip4h->protocol == IPPROTO_UDP)) {
663 csum = (u16 *)rmnet_map_get_checksum_field(ip4h->protocol,
664 txporthdr);
665 *csum = ~(*csum);
666 }
667}
668
669static void rmnet_map_complement_ipv6_txporthdr_csum_field(void *ip6hdr)
670{
671 struct ipv6hdr *ip6h = (struct ipv6hdr *)ip6hdr;
672 void *txporthdr;
673 u16 *csum;
674
675 txporthdr = ip6hdr + sizeof(struct ipv6hdr);
676
677 if ((ip6h->nexthdr == IPPROTO_TCP) || (ip6h->nexthdr == IPPROTO_UDP)) {
678 csum = (u16 *)rmnet_map_get_checksum_field(ip6h->nexthdr,
679 txporthdr);
680 *csum = ~(*csum);
681 }
682}
683
684/* rmnet_map_checksum_uplink_packet() - Generates UL checksum
685 * meta info header
686 * @skb: Pointer to the packet's skb.
687 *
688 * Generates UL checksum meta info header for IPv4 and IPv6 over TCP and UDP
689 * packets that are supported for UL checksum offload.
690 *
691 * Return:
692 * - RMNET_MAP_CHECKSUM_OK: Validation of checksum succeeded.
693 * - RMNET_MAP_CHECKSUM_ERR_UNKNOWN_IP_VERSION: Unrecognized IP header.
694 * - RMNET_MAP_CHECKSUM_SW: Unsupported packet for UL checksum offload.
695 */
696int rmnet_map_checksum_uplink_packet(struct sk_buff *skb,
697 struct net_device *orig_dev,
698 u32 egress_data_format)
699{
700 unsigned char ip_version;
701 struct rmnet_map_ul_checksum_header_s *ul_header;
702 void *iphdr;
703 int ret;
704
705 ul_header = (struct rmnet_map_ul_checksum_header_s *)
706 skb_push(skb, sizeof(struct rmnet_map_ul_checksum_header_s));
707
708 if (unlikely(!(orig_dev->features &
709 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))) {
710 ret = RMNET_MAP_CHECKSUM_SW;
711 goto sw_checksum;
712 }
713
714 if (skb->ip_summed == CHECKSUM_PARTIAL) {
715 iphdr = (char *)ul_header +
716 sizeof(struct rmnet_map_ul_checksum_header_s);
717 ip_version = (*(char *)iphdr & 0xF0) >> 4;
718 if (ip_version == 0x04) {
719 rmnet_map_fill_ipv4_packet_ul_checksum_header
720 (iphdr, ul_header, skb);
721 if (egress_data_format &
722 RMNET_EGRESS_FORMAT_MAP_CKSUMV4)
723 rmnet_map_complement_ipv4_txporthdr_csum_field(
724 iphdr);
725 ret = RMNET_MAP_CHECKSUM_OK;
726 goto done;
727 } else if (ip_version == 0x06) {
728 rmnet_map_fill_ipv6_packet_ul_checksum_header
729 (iphdr, ul_header, skb);
730 if (egress_data_format &
731 RMNET_EGRESS_FORMAT_MAP_CKSUMV4)
732 rmnet_map_complement_ipv6_txporthdr_csum_field(
733 iphdr);
734 ret = RMNET_MAP_CHECKSUM_OK;
735 goto done;
736 } else {
737 ret = RMNET_MAP_CHECKSUM_ERR_UNKNOWN_IP_VERSION;
738 goto sw_checksum;
739 }
740 } else {
741 ret = RMNET_MAP_CHECKSUM_SW;
742 goto sw_checksum;
743 }
744
745sw_checksum:
746 ul_header->checksum_start_offset = 0;
747 ul_header->checksum_insert_offset = 0;
748 ul_header->cks_en = 0;
749 ul_header->udp_ip4_ind = 0;
750done:
751 return ret;
752}
Subash Abhinov Kasiviswanathanbf783ba2017-11-08 15:55:29 -0700753
754int rmnet_ul_aggregation_skip(struct sk_buff *skb, int offset)
755{
756 unsigned char *packet_start = skb->data + offset;
757 int is_icmp = 0;
758
759 if ((skb->data[offset]) >> 4 == 0x04) {
760 struct iphdr *ip4h = (struct iphdr *)(packet_start);
761
762 if (ip4h->protocol == IPPROTO_ICMP)
763 is_icmp = 1;
Subash Abhinov Kasiviswanathanb7500cc2017-11-09 17:32:52 -0700764 } else if ((skb->data[offset]) >> 4 == 0x06) {
Subash Abhinov Kasiviswanathanbf783ba2017-11-08 15:55:29 -0700765 struct ipv6hdr *ip6h = (struct ipv6hdr *)(packet_start);
766
Subash Abhinov Kasiviswanathanb7500cc2017-11-09 17:32:52 -0700767 if (ip6h->nexthdr == IPPROTO_ICMPV6) {
768 is_icmp = 1;
769 } else if (ip6h->nexthdr == NEXTHDR_FRAGMENT) {
Subash Abhinov Kasiviswanathanbf783ba2017-11-08 15:55:29 -0700770 struct frag_hdr *frag;
771
772 frag = (struct frag_hdr *)(packet_start
773 + sizeof(struct ipv6hdr));
774 if (frag->nexthdr == IPPROTO_ICMPV6)
775 is_icmp = 1;
Subash Abhinov Kasiviswanathanbf783ba2017-11-08 15:55:29 -0700776 }
777 }
778
779 return is_icmp;
780}