/
vrouter_mod.c
2326 lines (1954 loc) · 65.7 KB
/
vrouter_mod.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* vrouter_mod.c -- linux vrouter module
*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/skbuff.h>
#include <linux/smp.h>
#include <linux/netdevice.h>
#include <linux/cpumask.h>
#include <linux/time.h>
#include <linux/highmem.h>
#include <linux/version.h>
#include <linux/if_vlan.h>
#include <linux/icmp.h>
#include "vr_packet.h"
#include "vr_sandesh.h"
#include "vrouter.h"
#include "vr_linux.h"
#include "vr_os.h"
#include "vr_compat.h"
#include "vr_fragment.h"
unsigned int vr_num_cpus = 1;
__u32 vr_hashrnd = 0;
int hashrnd_inited = 0;
extern int vr_flow_entries;
extern int vr_oflow_entries;
extern unsigned int vr_bridge_entries;
extern unsigned int vr_bridge_oentries;
int vrouter_dbg;
extern struct vr_packet *linux_get_packet(struct sk_buff *,
struct vr_interface *);
extern bool linux_ip_proto_pull(struct iphdr *);
struct work_arg {
struct work_struct wa_work;
void (*fn)(void *);
void *wa_arg;
};
struct rcu_cb_data {
struct rcu_head rcd_rcu;
vr_defer_cb rcd_user_cb;
struct vrouter *rcd_router;
unsigned char rcd_user_data[0];
};
extern int vrouter_init(void);
extern void vrouter_exit(bool);
extern int vr_genetlink_init(void);
extern void vr_genetlink_exit(void);
extern int vr_mem_init(void);
extern void vr_mem_exit(void);
extern void vhost_exit(void);
static void lh_reset_skb_fields(struct vr_packet *pkt);
static void *
lh_malloc(unsigned int size)
{
return kmalloc(size, GFP_ATOMIC);
}
static void *
lh_zalloc(unsigned int size)
{
return kzalloc(size, GFP_ATOMIC);
}
static void
lh_free(void *mem)
{
if (mem)
kfree(mem);
return;
}
static void *
lh_page_alloc(unsigned int size)
{
unsigned int order;
if (size & (PAGE_SIZE - 1)) {
size += PAGE_SIZE;
size &= ~(PAGE_SIZE - 1);
}
order = get_order(size);
return (void *)__get_free_pages(GFP_ATOMIC | __GFP_ZERO | __GFP_COMP, order);
}
static void
lh_page_free(void *address, unsigned int size)
{
unsigned int order;
if (size & (PAGE_SIZE - 1)) {
size += PAGE_SIZE;
size &= ~(PAGE_SIZE - 1);
}
order = get_order(size);
free_pages((unsigned long)address, order);
return;
}
uint64_t
lh_vtop(void *address)
{
return (uint64_t)(virt_to_phys(address));
}
struct vr_packet *
lh_palloc(unsigned int size)
{
struct sk_buff *skb;
skb = alloc_skb(size, GFP_ATOMIC);
if (!skb)
return NULL;
return linux_get_packet(skb, NULL);
}
static struct vr_packet *
lh_pexpand_head(struct vr_packet *pkt, unsigned int hspace)
{
struct sk_buff *skb;
skb = vp_os_packet(pkt);
if (!skb)
return NULL;
if (pskb_expand_head(skb, hspace, 0, GFP_ATOMIC))
return NULL;
pkt->vp_head = skb->head;
pkt->vp_data += hspace;
pkt->vp_tail += hspace;
pkt->vp_end = skb_end_pointer(skb) - skb->head;
pkt->vp_network_h += hspace;
pkt->vp_inner_network_h += hspace;
return pkt;
}
static struct vr_packet *
lh_palloc_head(struct vr_packet *pkt, unsigned int size)
{
struct sk_buff *skb, *skb_head;
struct vr_packet *npkt;
skb = vp_os_packet(pkt);
if (!skb)
return NULL;
skb->data = pkt->vp_head + pkt->vp_data;
skb_set_tail_pointer(skb, pkt->vp_len);
skb->len = pkt->vp_len + skb->data_len;
skb_head = alloc_skb(size, GFP_ATOMIC);
if (!skb_head)
return NULL;
npkt = linux_get_packet(skb_head, pkt->vp_if);
if (!npkt)
return npkt;
npkt->vp_flags = pkt->vp_flags;
skb_frag_list_init(skb_head);
skb_frag_add_head(skb_head, skb);
skb_head->len += skb->len;
skb_head->data_len = skb->len;
skb_head->protocol = skb->protocol;
/* Copy the gso fields too */
skb_shinfo(skb_head)->gso_type = skb_shinfo(skb)->gso_type;
skb_shinfo(skb_head)->gso_size = skb_shinfo(skb)->gso_size;
skb_shinfo(skb_head)->gso_segs = skb_shinfo(skb)->gso_segs;
skb_head->ip_summed = skb->ip_summed;
skb_head->csum = skb->csum;
npkt->vp_network_h += pkt->vp_network_h + npkt->vp_end;
npkt->vp_inner_network_h += pkt->vp_inner_network_h + npkt->vp_end;
return npkt;
}
static struct vr_packet *
lh_pclone(struct vr_packet *pkt)
{
struct sk_buff *skb, *skb_c;
struct vr_packet *pkt_clone;
skb = vp_os_packet(pkt);
skb_c = skb_clone(skb, GFP_ATOMIC);
if (!skb_c)
return NULL;
pkt_clone = (struct vr_packet *)skb_c->cb;
pkt_clone->vp_cpu = vr_get_cpu();
return pkt_clone;
}
static void
lh_preset(struct vr_packet *pkt)
{
struct sk_buff *skb;
skb = vp_os_packet(pkt);
pkt->vp_data = skb->data - skb->head;
pkt->vp_tail = skb_tail_pointer(skb) - skb->head;
pkt->vp_len = skb_headlen(skb);
return;
}
static void
lh_pset_data(struct vr_packet *pkt, unsigned short offset)
{
struct sk_buff *skb;
skb = vp_os_packet(pkt);
skb->data = pkt->vp_head + offset;
return;
}
static unsigned int
lh_pgso_size(struct vr_packet *pkt)
{
struct sk_buff *skb = vp_os_packet(pkt);
return skb_shinfo(skb)->gso_size;
}
static void
lh_pfree(struct vr_packet *pkt, unsigned short reason)
{
struct vrouter *router = vrouter_get(0);
struct sk_buff *skb;
skb = vp_os_packet(pkt);
if (!skb)
return;
if (router)
((uint64_t *)(router->vr_pdrop_stats[pkt->vp_cpu]))[reason]++;
kfree_skb(skb);
return;
}
void
lh_pfree_skb(struct sk_buff *skb, unsigned short reason)
{
struct vrouter *router = vrouter_get(0);
unsigned int cpu;
cpu = vr_get_cpu();
if ((cpu < vr_num_cpus) && (router))
((uint64_t *)(router->vr_pdrop_stats[cpu]))[reason]++;
kfree_skb(skb);
return;
}
static int
lh_pcopy(unsigned char *dst, struct vr_packet *p_src,
unsigned int offset, unsigned int len)
{
int ret;
struct sk_buff *src;
src = vp_os_packet(p_src);
ret = skb_copy_bits(src, offset, dst, len);
if (ret)
return ret;
return len;
}
static unsigned short
lh_pfrag_len(struct vr_packet *pkt)
{
struct sk_buff *skb;
skb = vp_os_packet(pkt);
if (!skb)
return 0;
return skb->data_len;
}
static unsigned short
lh_phead_len(struct vr_packet *pkt)
{
struct sk_buff *skb;
skb = vp_os_packet(pkt);
if (!skb)
return 0;
return skb_headlen(skb);
}
static void
lh_get_time(unsigned int *sec, unsigned int *nsec)
{
struct timespec t;
getnstimeofday(&t);
*sec = t.tv_sec;
*nsec = t.tv_nsec;
return;
}
static unsigned int
lh_get_cpu(void)
{
unsigned int cpu = get_cpu();
put_cpu();
return cpu;
}
static void
lh_get_mono_time(unsigned int *sec, unsigned int *nsec)
{
struct timespec t;
uint64_t jiffies = get_jiffies_64();
jiffies_to_timespec(jiffies, &t);
*sec = t.tv_sec;
*nsec = t.tv_nsec;
return;
}
static void
lh_work(struct work_struct *work)
{
struct work_arg *wa = container_of(work, struct work_arg, wa_work);
rcu_read_lock();
wa->fn(wa->wa_arg);
rcu_read_unlock();
kfree(wa);
return;
}
static void
lh_schedule_work(unsigned int cpu, void (*fn)(void *), void *arg)
{
struct work_arg *wa = kzalloc(sizeof(*wa), GFP_KERNEL);
if (!wa)
return;
wa->fn = fn;
wa->wa_arg = arg;
INIT_WORK(&wa->wa_work, lh_work);
schedule_work_on(cpu, &wa->wa_work);
return;
}
static void
lh_delay_op(void)
{
synchronize_net();
return;
}
static void *
lh_inner_network_header(struct vr_packet *pkt)
{
struct sk_buff *skb;
struct sk_buff *frag;
struct vr_packet *frag_pkt;
unsigned short off = pkt->vp_inner_network_h - pkt->vp_end;
skb = vp_os_packet(pkt);
while (skb_shinfo(skb)->frag_list) {
frag = skb_shinfo(skb)->frag_list;
frag_pkt = (struct vr_packet *)frag->cb;
if (off < frag_pkt->vp_end)
return frag_pkt->vp_head + off;
off -= frag_pkt->vp_end;
skb = frag;
}
return NULL;
}
/*
* lh_pheader_pointer - wrapper for skb_header_pointer
*/
static void *
lh_pheader_pointer(struct vr_packet *pkt, unsigned short hdr_len, void *buf)
{
int offset;
struct sk_buff *skb = vp_os_packet(pkt);
/*
* vp_data is the offset from the skb head. skb_header_pointer expects
* the offset from skb->data, so calculate this offset.
*/
offset = pkt->vp_data - (skb->data - skb->head);
return skb_header_pointer(skb, offset, hdr_len, buf);
}
static void
rcu_cb(struct rcu_head *rh)
{
struct rcu_cb_data *cb_data = (struct rcu_cb_data *)rh;
/* Call the user call back */
cb_data->rcd_user_cb(cb_data->rcd_router, cb_data->rcd_user_data);
lh_free(cb_data);
return;
}
static void
lh_defer(struct vrouter *router, vr_defer_cb user_cb, void *data)
{
struct rcu_cb_data *cb_data;
cb_data = container_of(data, struct rcu_cb_data, rcd_user_data);
cb_data->rcd_user_cb = user_cb;
cb_data->rcd_router = router;
call_rcu(&cb_data->rcd_rcu, rcu_cb);
return;
}
static void *
lh_get_defer_data(unsigned int len)
{
struct rcu_cb_data *cb_data;
if (!len)
return NULL;
cb_data = lh_malloc(sizeof(*cb_data) + len);
if (!cb_data) {
return NULL;
}
return cb_data->rcd_user_data;
}
static void
lh_put_defer_data(void *data)
{
struct rcu_cb_data *cb_data;
if (!data)
return;
cb_data = container_of(data, struct rcu_cb_data, rcd_user_data);
lh_free(cb_data);
return;
}
static int
lh_pcow(struct vr_packet *pkt, unsigned short head_room)
{
unsigned int old_off, new_off;
int data_off = 0;
struct sk_buff *skb = vp_os_packet(pkt);
data_off = pkt->vp_data - (skb->data - skb->head);
#ifdef NET_SKBUFF_DATA_USES_OFFSET
old_off = skb->network_header;
#else
old_off = skb->network_header - skb->head;
#endif
if (skb_cow(skb, head_room))
return -ENOMEM;
/* Now manipulate the offsets as data pointers are modified */
pkt->vp_head = skb->head;
pkt->vp_tail = skb_tail_pointer(skb) - skb->head;
/* The data_off, can be negative here */
pkt->vp_data = skb->data - skb->head + data_off;
pkt->vp_end = skb_end_pointer(skb) - skb->head;
/*
* pkt->vp_len is untouched, as it is going to be same
* before and after cow
*/
#ifdef NET_SKBUFF_DATA_USES_OFFSET
new_off = skb->network_header;
#else
new_off = skb->network_header - skb->head;
#endif
pkt->vp_network_h += new_off - old_off;
pkt->vp_inner_network_h += new_off - old_off;
return 0;
}
/*
* lh_get_udp_src_port - return a source port for the outer UDP header.
* The source port is based on a hash of the inner IP source/dest addresses,
* vrf (and inner TCP/UDP ports in the future). The label from fmd
* will be used in the future to detect whether it is a L2/L3 packet.
* Returns 0 on error, valid source port otherwise.
*/
static __u16
lh_get_udp_src_port(struct vr_packet *pkt, struct vr_forwarding_md *fmd,
unsigned short vrf)
{
struct sk_buff *skb = vp_os_packet(pkt);
int pull_len;
__u32 ip_src, ip_dst, hashval, port_range;
struct vr_ip *iph;
__u32 *data;
__u16 port;
__u16 sport = 0, dport = 0;
struct vr_fragment *frag;
struct vrouter *router = vrouter_get(0);
__u32 hash_key[5];
__u16 *l4_hdr;
if (hashrnd_inited == 0) {
get_random_bytes(&vr_hashrnd, sizeof(vr_hashrnd));
hashrnd_inited = 1;
}
if (pkt->vp_type == VP_TYPE_VXLAN) {
if (pkt_head_len(pkt) < ETH_HLEN)
goto error;
data = (unsigned int *)(skb->head + pkt->vp_data);
hashval = vr_hash(data, ETH_HLEN, vr_hashrnd);
/* Include the VRF to calculate the hash */
hashval = vr_hash_2words(hashval, vrf, vr_hashrnd);
} else if (pkt->vp_type == VP_TYPE_L2) {
/* Lets assume the ethernet header without VLAN headers as of now */
pull_len = ETH_HLEN;
if (pkt_head_len(pkt) < pull_len)
goto error;
data = (unsigned int *)pkt_data(pkt);
/*
* If L2 multicast and control data is zero, ethernet header is after
* VXLAN and control word
*/
if ((pkt->vp_flags & VP_FLAG_MULTICAST) && (!(*data))) {
pull_len += VR_VXLAN_HDR_LEN + VR_L2_MCAST_CTRL_DATA_LEN;
if (pkt_head_len(pkt) < pull_len)
goto error;
data = (unsigned int *)(((unsigned char *)data) +
VR_VXLAN_HDR_LEN + VR_L2_MCAST_CTRL_DATA_LEN);
}
hashval = vr_hash(data, ETH_HLEN, vr_hashrnd);
/* Include the VRF to calculate the hash */
hashval = vr_hash_2words(hashval, vrf, vr_hashrnd);
} else {
/*
* pull_len can be negative in the following calculation. This behavior
* will be true in case of mirroring. In mirroring, we do preset first
* which makes vp_data = skb->data, and then we push mirroring headers,
* which makes pull_len < 0 and thats why pull_len is an integer.
*/
pull_len = sizeof(struct iphdr);
pull_len += pkt->vp_data;
pull_len -= skb_headroom(skb);
/* Lets pull only if ip hdr is beyond this skb */
if ((pkt->vp_data + sizeof(struct iphdr)) > pkt->vp_tail) {
/* We dont handle if tails are different */
#ifdef NET_SKBUFF_DATA_USES_OFFSET
if (pkt->vp_tail != skb->tail)
#else
if (pkt->vp_tail != (skb->tail - skb->head))
goto error;
#endif
/*
* pull_len has to be +ve here and hence additional check is not
* needed
*/
if (!pskb_may_pull(skb, (unsigned int)pull_len)) {
goto error;
}
}
iph = (struct vr_ip *)(skb->head + pkt->vp_data);
if (vr_ip_transport_header_valid(iph)) {
if ((iph->ip_proto == VR_IP_PROTO_TCP) ||
(iph->ip_proto == VR_IP_PROTO_UDP)) {
pull_len += ((iph->ip_hl * 4) - sizeof(struct vr_ip) + 4);
if ((pull_len > 0) &&
!pskb_may_pull(skb,(unsigned int)pull_len)) {
goto error;
}
iph = (struct vr_ip *)(skb->head + pkt->vp_data);
l4_hdr = (__u16 *) (((char *) iph) + (iph->ip_hl * 4));
sport = *l4_hdr;
dport = *(l4_hdr+1);
}
} else {
/*
* If this fragment required flow lookup, get the source and
* dst port from the frag entry. Otherwise, use 0 as the source
* dst port (which could result in fragments getting a different
* outer UDP source port than non-fragments in the same flow).
*/
frag = vr_fragment_get(router, vrf, iph);
if (frag) {
sport = frag->f_sport;
dport = frag->f_dport;
}
}
ip_src = iph->ip_saddr;
ip_dst = iph->ip_daddr;
hash_key[0] = ip_src;
hash_key[1] = ip_dst;
hash_key[2] = vrf;
hash_key[3] = sport;
hash_key[4] = dport;
hashval = jhash(hash_key, 20, vr_hashrnd);
}
lh_reset_skb_fields(pkt);
/*
* Convert the hash value to a value in the port range that we want
* for dynamic UDP ports
*/
port_range = VR_MUDP_PORT_RANGE_END - VR_MUDP_PORT_RANGE_START;
port = (__u16) (((u64) hashval * port_range) >> 32);
if (port > port_range) {
/*
* Shouldn't happen...
*/
port = 0;
}
return (port + VR_MUDP_PORT_RANGE_START);
error:
lh_reset_skb_fields(pkt);
return 0;
}
/*
* lh_adjust_tcp_mss - adjust the TCP MSS in the given packet based on
* vrouter physical interface MTU. Returns 0 on success, non-zero
* otherwise.
*/
static void
lh_adjust_tcp_mss(struct tcphdr *tcph, struct sk_buff *skb, unsigned short overlay_len)
{
int opt_off = sizeof(struct tcphdr);
u8 *opt_ptr = (u8 *) tcph;
u16 pkt_mss, max_mss;
struct net_device *dev;
struct vrouter *router = vrouter_get(0);
if ((tcph == NULL) || (!tcph->syn) || (router == NULL)) {
return;
}
if (router->vr_eth_if == NULL) {
return;
}
while (opt_off < (tcph->doff*4)) {
switch (opt_ptr[opt_off]) {
case TCPOPT_EOL:
return;
case TCPOPT_NOP:
opt_off++;
continue;
case TCPOPT_MSS:
if ((opt_off + TCPOLEN_MSS) > (tcph->doff*4)) {
return;
}
if (opt_ptr[opt_off+1] != TCPOLEN_MSS) {
return;
}
pkt_mss = (opt_ptr[opt_off+2] << 8) | opt_ptr[opt_off+3];
dev = (struct net_device *) router->vr_eth_if->vif_os;
if (dev == NULL) {
return;
}
max_mss = dev->mtu -
(overlay_len + sizeof(struct vr_ip) +
sizeof(struct tcphdr));
if (pkt_mss > max_mss) {
opt_ptr[opt_off+2] = (max_mss & 0xff00) >> 8;
opt_ptr[opt_off+3] = max_mss & 0xff;
inet_proto_csum_replace2(&tcph->check, skb,
htons(pkt_mss),
htons(max_mss), 0);
}
return;
default:
if ((opt_off + 1) == (tcph->doff*4)) {
return;
}
if (opt_ptr[opt_off+1]) {
opt_off += opt_ptr[opt_off+1];
} else {
opt_off++;
}
continue;
} /* switch */
} /* while */
return;
}
/*
* lh_pkt_from_vm_tcp_mss_adj - perform TCP MSS adjust, if required, for packets
* that are sent by a VM. Returns 0 on success, non-zero otherwise.
*/
static int
lh_pkt_from_vm_tcp_mss_adj(struct vr_packet *pkt, unsigned short overlay_len)
{
struct sk_buff *skb = vp_os_packet(pkt);
int hlen, pull_len;
struct vr_ip *iph;
struct tcphdr *tcph;
/*
* Pull enough of the header into the linear part of the skb to be
* able to inspect/modify the TCP header MSS value.
*/
pull_len = pkt->vp_data - (skb_headroom(skb));
pull_len += sizeof(struct vr_ip);
if (!pskb_may_pull(skb, pull_len)) {
return VP_DROP_PULL;
}
iph = (struct vr_ip *) (skb->head + pkt->vp_data);
if (iph->ip_proto != VR_IP_PROTO_TCP) {
goto out;
}
/*
* If this is a fragment and not the first one, it can be ignored
*/
if (iph->ip_frag_off & htons(IP_OFFSET)) {
goto out;
}
hlen = iph->ip_hl * 4;
pull_len += (hlen - sizeof(struct vr_ip));
pull_len += sizeof(struct tcphdr);
if (!pskb_may_pull(skb, pull_len)) {
return VP_DROP_PULL;
}
iph = (struct vr_ip *) (skb->head + pkt->vp_data);
tcph = (struct tcphdr *) ((char *) iph + hlen);
if ((tcph->doff << 2) <= (sizeof(struct tcphdr))) {
/*
* Nothing to do if there are no TCP options
*/
goto out;
}
pull_len += ((tcph->doff << 2) - (sizeof(struct tcphdr)));
if (!pskb_may_pull(skb, pull_len)) {
return VP_DROP_PULL;
}
iph = (struct vr_ip *) (skb->head + pkt->vp_data);
tcph = (struct tcphdr *) ((char *) iph + hlen);
lh_adjust_tcp_mss(tcph, skb, overlay_len);
out:
lh_reset_skb_fields(pkt);
return 0;
}
/*
* lh_reset_skb_fields - if the skb changes, possibley due to pskb_may_pull,
* reset fields of the pkt structure that point at the skb fields.
*/
static void
lh_reset_skb_fields(struct vr_packet *pkt)
{
struct sk_buff *skb = vp_os_packet(pkt);
pkt->vp_head = skb->head;
pkt->vp_tail = skb_tail_pointer(skb) - skb->head;
pkt->vp_end = skb_end_pointer(skb) - skb->head;
pkt->vp_len = pkt->vp_tail - pkt->vp_data;
return;
}
/*
* lh_csum_verify_fast - faster version of skb_checksum which avoids a call
* to kmap_atomic/kunmap_atomic as we already have a pointer obtained
* from an earlier call to kmap_atomic. This function can only be used if
* the skb has a TCP segment contained entirely in a single frag. Returns 0
* if checksum is ok, non-zero otherwise.
*/
static int
lh_csum_verify_fast(struct vr_ip *iph, struct tcphdr *tcph,
unsigned int tcp_size)
{
__wsum csum;
csum = csum_tcpudp_nofold(iph->ip_saddr, iph->ip_daddr,
tcp_size, IPPROTO_TCP, 0);
if (csum_fold(csum_partial(tcph, tcp_size, csum))) {
return -1;
}
return 0;
}
/*
* lh_csum_verify - verifies checksum of skb containing a TCP segment. Returns
* 0 if checksum is ok, non-zero otherwise.
*/
static int
lh_csum_verify(struct sk_buff *skb, struct vr_ip *iph)
{
skb->csum = csum_tcpudp_nofold(iph->ip_saddr, iph->ip_daddr,
ntohs(iph->ip_len) - (iph->ip_hl * 4),
IPPROTO_TCP, 0);
if (__skb_checksum_complete(skb)) {
return -1;
}
return 0;
}
/*
* lh_handle_checksum_complete_skb - if the skb has CHECKSUM_COMPLETE set,
* set it to CHECKSUM_NONE.
*/
static void
lh_handle_checksum_complete_skb(struct sk_buff *skb)
{
if (skb->ip_summed == CHECKSUM_COMPLETE) {
skb->csum = 0;
skb->ip_summed = CHECKSUM_NONE;
}
}
/*
* lh_csum_verify_udp - verifies checksum of skb containing a UDP datagram.
* Returns 0 if checksum is ok, non-zero otherwise.
*/
static int
lh_csum_verify_udp(struct sk_buff *skb, struct vr_ip *iph)
{
if (skb->ip_summed == CHECKSUM_COMPLETE) {
if (!csum_tcpudp_magic(iph->ip_saddr, iph->ip_daddr,
skb->len, IPPROTO_UDP, skb->csum)) {
skb->ip_summed = CHECKSUM_UNNECESSARY;
return 0;
}
}
skb->csum = csum_tcpudp_nofold(iph->ip_saddr, iph->ip_daddr,
skb->len, IPPROTO_UDP, 0);
if (__skb_checksum_complete(skb)) {
return -1;
}
return 0;
}
/*
* vr_kmap_atomic - calls kmap_atomic with right arguments depending on
* kernel version. For now, does nothing on 2.6.32 as we won't call this
* function on 2.6.32.
*/
static void *
vr_kmap_atomic(struct page *page)
{
#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32))
#if defined(RHEL_MAJOR) && defined(RHEL_MINOR) && \
(RHEL_MAJOR == 6) && (RHEL_MINOR == 4)
return kmap_atomic(page, KM_SKB_DATA_SOFTIRQ);
#else
return NULL;
#endif
#else
return kmap_atomic(page);
#endif
}
/*
* vr_kunmap_atomic - calls kunmap_atomic with right arguments depending on
* kernel version. For now, does nothing on 2.6.32 as we won't call this
* function on 2.6.32.
*/
static void
vr_kunmap_atomic(void *va)
{
#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32))
#if defined(RHEL_MAJOR) && defined(RHEL_MINOR) && \
(RHEL_MAJOR == 6) && (RHEL_MINOR == 4)
kunmap_atomic(va, KM_SKB_DATA_SOFTIRQ);
#else
return;
#endif
#else
kunmap_atomic(va);
#endif
}
/*
* lh_pull_inner_headers_fast_udp - implements the functionality of
* lh_pull_inner_headers_fast for UDP packets.
*/
static int
lh_pull_inner_headers_fast_udp(struct vr_ip *outer_iph,
struct vr_packet *pkt, int
(*tunnel_type_cb)(unsigned int, unsigned
int, unsigned short *), int *ret,
int *encap_type)
{
struct sk_buff *skb = vp_os_packet(pkt);
unsigned short pkt_headlen;
unsigned char *va = NULL;
skb_frag_t *frag;
unsigned int frag_size, pull_len, hdr_len, skb_pull_len, tcp_size;
unsigned int tcph_pull_len = 0, hlen = 0;
struct vr_ip *iph = NULL, *icmp_pl_iph = NULL;
struct vr_udp *udph;
struct tcphdr *tcph = NULL;
struct vr_icmp *icmph = NULL;
bool thdr_valid = false;
unsigned int label, control_data;
int pkt_type = 0;
struct vr_eth *eth = NULL;
unsigned short eth_proto;
pkt_headlen = pkt_head_len(pkt);
hdr_len = sizeof(struct udphdr);
if (pkt_headlen) {
if (pkt_headlen != hdr_len) {
goto slow_path;
}
}
/*