/
vr_proto_ip.c
1263 lines (1037 loc) · 34 KB
/
vr_proto_ip.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
/*
* vr_proto_ip.c -- IP protocol handling
*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <vr_os.h>
#include <vr_types.h>
#include <vr_packet.h>
#include "vr_interface.h"
#include "vr_datapath.h"
#include "vr_mpls.h"
#include "vr_vxlan.h"
#include "vr_ip_mtrie.h"
#include "vr_fragment.h"
#include "vr_bridge.h"
static unsigned short vr_ip_id;
extern struct vr_vrf_stats *(*vr_inet_vrf_stats)(unsigned short, unsigned int);
extern struct vr_nexthop *vr_inet6_ip_lookup(unsigned short, uint8_t *);
unsigned short
vr_generate_unique_ip_id()
{
vr_ip_id++;
if (!vr_ip_id)
vr_ip_id++;
return vr_ip_id;
}
struct vr_nexthop *
vr_inet_ip_lookup(unsigned short vrf, uint32_t ip)
{
uint32_t rt_prefix;
struct vr_route_req rt;
rt.rtr_req.rtr_vrf_id = vrf;
rt.rtr_req.rtr_prefix = (uint8_t *)&rt_prefix;
*(uint32_t*)rt.rtr_req.rtr_prefix = ip;
rt.rtr_req.rtr_prefix_size = 4;
rt.rtr_req.rtr_prefix_len = IP4_PREFIX_LEN;
rt.rtr_req.rtr_family = AF_INET;
rt.rtr_req.rtr_marker_size = 0;
rt.rtr_req.rtr_nh_id = 0;
return vr_inet_route_lookup(vrf, &rt);
}
struct vr_nexthop *
vr_inet_src_lookup(unsigned short vrf, struct vr_packet *pkt)
{
struct vr_ip *ip;
struct vr_ip6 *ip6;
if (!pkt)
return NULL;
if (pkt->vp_type == VP_TYPE_IP) {
ip = (struct vr_ip *)pkt_network_header(pkt);
if (!ip)
return NULL;
return vr_inet_ip_lookup(vrf, ip->ip_saddr);
} else if (pkt->vp_type == VP_TYPE_IP6) {
ip6 = (struct vr_ip6 *)pkt_network_header(pkt);
if (!ip6)
return NULL;
return vr_inet6_ip_lookup(vrf, ip6->ip6_src);
}
return NULL;
}
static inline unsigned char
vr_ip_decrement_ttl(struct vr_ip *ip)
{
unsigned int diff = 0xfffe;
unsigned int csum;
csum = (~ip->ip_csum) & 0xffff;
csum += diff;
csum = (csum >> 16) + (csum & 0xffff);
if (csum >> 16)
csum = (csum & 0xffff) + 1;
--ip->ip_ttl;
ip->ip_csum = ~(csum & 0xffff);
return ip->ip_ttl;
}
void
vr_ip_update_csum(struct vr_packet *pkt, unsigned int ip_inc, unsigned int inc)
{
struct vr_ip *ip;
struct vr_tcp *tcp;
struct vr_udp *udp;
unsigned int csum;
unsigned short *csump;
ip = (struct vr_ip *)pkt_network_header(pkt);
ip->ip_csum = vr_ip_csum(ip);
if (ip->ip_proto == VR_IP_PROTO_TCP) {
tcp = (struct vr_tcp *)((unsigned char *)ip + ip->ip_hl * 4);
csump = &tcp->tcp_csum;
} else if (ip->ip_proto == VR_IP_PROTO_UDP) {
udp = (struct vr_udp *)((unsigned char *)ip + ip->ip_hl * 4);
csump = &udp->udp_csum;
} else {
return;
}
if (vr_ip_transport_header_valid(ip)) {
/*
* for partial checksums, the actual value is stored rather
* than the complement
*/
if (pkt->vp_flags & VP_FLAG_CSUM_PARTIAL) {
csum = (*csump) & 0xffff;
inc = ip_inc;
} else {
csum = ~(*csump) & 0xffff;
}
csum += inc;
if (csum < inc)
csum += 1;
csum = (csum & 0xffff) + (csum >> 16);
if (csum >> 16)
csum = (csum & 0xffff) + 1;
if (pkt->vp_flags & VP_FLAG_CSUM_PARTIAL) {
*csump = csum & 0xffff;
} else {
*csump = ~(csum) & 0xffff;
}
}
return;
}
unsigned short
vr_ip_csum(struct vr_ip *ip)
{
int sum = 0;
unsigned short *ptr = (unsigned short *)ip;
unsigned short answer = 0;
unsigned short *w = ptr;
int len = ip->ip_hl * 4;
int nleft = len;
ip->ip_csum = 0;
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
/* mop up an odd byte, if necessary */
if (nleft == 1) {
*(unsigned char *)(&answer) = *(unsigned char *)w;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
answer = ~sum;
return answer;
}
unsigned short
vr_ip6_partial_csum(struct vr_ip6 *ip6)
{
unsigned int sum, i;
unsigned short csum, proto;
unsigned long long s = 0;
for (i = 0; i < 4; i++)
s += *((uint32_t *)ip6->ip6_src + i);
for (i = 0; i < 4; i++)
s += *((uint32_t *)ip6->ip6_dst + i);
s += ip6->ip6_plen;
proto = ip6->ip6_nxt;
s += htons(proto);
s = (s & 0xFFFFFFFF) + (s >> 32);
sum = (s & 0xFFFF) + (s >> 16);
csum = (sum & 0xFFFF) + (sum >> 16);
return csum;
}
unsigned short
vr_ip_partial_csum(struct vr_ip *ip)
{
unsigned long long s;
unsigned int sum;
unsigned short csum, proto;
proto = ip->ip_proto;
s = ip->ip_saddr;
s += ip->ip_daddr;
s += htons(ntohs(ip->ip_len) - (ip->ip_hl * 4));
s += htons(proto);
s = (s & 0xFFFFFFFF) + (s >> 32);
sum = (s & 0xFFFF) + (s >> 16);
csum = (sum & 0xFFFF) + (sum >> 16);
return csum;
}
int
vr_forward(struct vrouter *router, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
int family;
unsigned char ttl;
uint32_t rt_prefix[4];
struct vr_route_req rt;
struct vr_nexthop *nh;
struct vr_ip *ip;
struct vr_ip6 *ip6;
struct vr_forwarding_md rt_fmd;
ip6 = NULL;
ip = (struct vr_ip *)pkt_data(pkt);
if (vr_ip_is_ip6(ip)) {
family = AF_INET6;
ip6 = (struct vr_ip6 *)pkt_data(pkt);
/* ttl = --ip6->ip6_hlim */
ttl = ip6->ip6_hlim;
pkt->vp_type = VP_TYPE_IP6;
} else if (vr_ip_is_ip4(ip)) {
family = AF_INET;
if (!ip->ip_ttl) {
vr_pfree(pkt, VP_DROP_TTL_EXCEEDED);
return 0;
}
ttl = vr_ip_decrement_ttl(ip);
pkt->vp_type = VP_TYPE_IP;
} else {
vr_pfree(pkt, VP_DROP_INVALID_PROTOCOL);
return 0;
}
pkt->vp_ttl = ttl;
rt.rtr_req.rtr_vrf_id = fmd->fmd_dvrf;
rt.rtr_req.rtr_family = family;
if (family == AF_INET) {
rt.rtr_req.rtr_prefix = (uint8_t*)&rt_prefix;
*(uint32_t*)rt.rtr_req.rtr_prefix = (ip->ip_daddr);
rt.rtr_req.rtr_prefix_size = 4;
rt.rtr_req.rtr_prefix_len = IP4_PREFIX_LEN;
} else {
rt.rtr_req.rtr_prefix = (uint8_t*)&rt_prefix;
rt.rtr_req.rtr_prefix_size = 16;
memcpy(rt.rtr_req.rtr_prefix, ip6->ip6_dst, 16);
rt.rtr_req.rtr_prefix_len = IP6_PREFIX_LEN;
}
rt.rtr_req.rtr_nh_id = 0;
rt.rtr_req.rtr_marker_size = 0;
nh = vr_inet_route_lookup(fmd->fmd_dvrf, &rt);
if (rt.rtr_req.rtr_label_flags & VR_RT_LABEL_VALID_FLAG) {
if (!fmd) {
vr_init_forwarding_md(&rt_fmd);
fmd = &rt_fmd;
}
vr_forwarding_md_set_label(fmd, rt.rtr_req.rtr_label,
VR_LABEL_TYPE_UNKNOWN);
}
return nh_output(pkt, nh, fmd);
}
unsigned int
vr_icmp_input(struct vrouter *router, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
int ret;
unsigned int offset = 0, pull_len = 0;
unsigned int unhandled = 1, handled = 0;
struct vr_icmp *icmph;
struct vr_ip *iph;
struct vr_udp *udph;
icmph = (struct vr_icmp *)(pkt_data(pkt) + offset);
pull_len += sizeof(*icmph);
if (vr_icmp_error(icmph)) {
pull_len += sizeof(*iph);
ret = vr_pkt_may_pull(pkt, pull_len);
if (ret)
return unhandled;
offset += sizeof(*icmph);
iph = (struct vr_ip *)(pkt_data(pkt) + offset);
if ((iph->ip_proto != VR_IP_PROTO_GRE) &&
(iph->ip_proto != VR_IP_PROTO_UDP))
return unhandled;
pull_len += ((iph->ip_hl * 4) - sizeof(*iph));
ret = vr_pkt_may_pull(pkt, pull_len);
if (ret)
return unhandled;
iph = (struct vr_ip *)(pkt_data(pkt) + offset);
if (iph->ip_proto == VR_IP_PROTO_UDP) {
/* for sport and dport */
pull_len += 4;
offset += (iph->ip_hl * 4);
ret = vr_pkt_may_pull(pkt, pull_len);
if (ret)
return unhandled;
/*
* Note - we can't look at any other data other than ports
* since we pull only the first 4 bytes
*/
udph = (struct vr_udp *)(pkt_data(pkt) + offset);
if (!vr_mpls_udp_port(ntohs(udph->udp_dport)))
return unhandled;
}
vr_trap(pkt, pkt->vp_if->vif_vrf, AGENT_TRAP_ICMP_ERROR, 0);
return handled;
}
return unhandled;
}
/*
* vr_udp_input - handle incoming UDP packets. If the UDP destination
* port is for MPLS over UDP or VXLAN, decap the packet and forward the inner
* packet. Returns 1 if the packet was not handled, 0 otherwise.
*/
unsigned int
vr_udp_input(struct vrouter *router, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
struct vr_udp *udph, udp;
int handled = 0, ret = PKT_RET_FAST_PATH;
unsigned short reason;
int encap_type = PKT_ENCAP_MPLS;
if (vr_perfp && vr_pull_inner_headers_fast) {
handled = vr_pull_inner_headers_fast(pkt, VR_IP_PROTO_UDP,
vr_mpls_tunnel_type, &ret, &encap_type);
if (!handled) {
return 1;
}
if (ret == PKT_RET_FAST_PATH) {
goto next_encap;
}
if (ret == PKT_RET_ERROR) {
vr_pfree(pkt, VP_DROP_CKSUM_ERR);
return 0;
}
/* Fall through to the slower path */
ASSERT(ret == PKT_RET_SLOW_PATH);
}
udph = (struct vr_udp *)vr_pheader_pointer(pkt, sizeof(struct vr_udp),
&udp);
if (udph == NULL) {
vr_pfree(pkt, VP_DROP_MISC);
return 0;
}
if (vr_mpls_udp_port(ntohs(udph->udp_dport))) {
encap_type = PKT_ENCAP_MPLS;
} else if (ntohs(udph->udp_dport) == VR_VXLAN_UDP_DST_PORT) {
encap_type = PKT_ENCAP_VXLAN;
} else {
return 1;
}
/*
* We are going to handle this packet. Pull as much of the inner packet
* as required into the contiguous part of the pkt.
*/
if (vr_pull_inner_headers) {
if (!vr_pull_inner_headers(pkt, VR_IP_PROTO_UDP,
&reason, vr_mpls_tunnel_type)) {
vr_pfree(pkt, reason);
return 0;
}
}
pkt_pull(pkt, sizeof(struct vr_udp));
next_encap:
if (encap_type == PKT_ENCAP_MPLS) {
vr_mpls_input(router, pkt, fmd);
} else {
vr_vxlan_input(router, pkt, fmd);
}
return 0;
}
unsigned int
vr_gre_input(struct vrouter *router, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
unsigned short *gre_hdr, gre_proto, hdr_len, reason;
char buf[4];
int handled = 0, ret = PKT_RET_FAST_PATH;
int encap_type;
if (vr_perfp && vr_pull_inner_headers_fast) {
handled = vr_pull_inner_headers_fast(pkt, VR_IP_PROTO_GRE,
vr_mpls_tunnel_type, &ret, &encap_type);
if (!handled) {
goto unhandled;
}
if (ret == PKT_RET_FAST_PATH) {
goto mpls_input;
}
if (ret == PKT_RET_ERROR) {
vr_pfree(pkt, VP_DROP_CKSUM_ERR);
return 0;
}
/* Fall through to the slower path */
ASSERT(ret == PKT_RET_SLOW_PATH);
}
/* start with basic GRE header */
hdr_len = 4;
gre_hdr = (unsigned short *) vr_pheader_pointer(pkt, hdr_len, buf);
if (gre_hdr == NULL) {
vr_pfree(pkt, VP_DROP_MISC);
return 0;
}
if (*gre_hdr & VR_GRE_FLAG_CSUM)
hdr_len += 4;
if (*gre_hdr & VR_GRE_FLAG_KEY)
hdr_len += 4;
/* we are not RFC 1701 compliant receiver */
if (*gre_hdr & (~(VR_GRE_FLAG_CSUM | VR_GRE_FLAG_KEY)))
goto unhandled;
/*
* ... and we do not deal with any other protocol other than MPLS
* for now
*/
gre_proto = ntohs(*(gre_hdr + 1));
if (gre_proto != VR_GRE_PROTO_MPLS)
goto unhandled;
/*
* We are going to handle this packet. Pull as much of the inner packet
* as required into the contiguous part of the pkt.
*/
if (vr_pull_inner_headers) {
if (!vr_pull_inner_headers(pkt, VR_IP_PROTO_GRE, &reason,
vr_mpls_tunnel_type)) {
vr_pfree(pkt, reason);
return 0;
}
}
/* pull and junk the GRE header */
pkt_pull(pkt, hdr_len);
mpls_input:
vr_mpls_input(router, pkt, fmd);
return 0;
unhandled:
return 1;
}
int
vr_ip_rcv(struct vrouter *router, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
struct vr_ip *ip;
struct vr_interface *vif = NULL;
unsigned char *l2_hdr;
unsigned int hlen;
unsigned short drop_reason, l4_port = 0;
int ret = 0, unhandled = 1;
struct vr_fragment *frag;
ip = (struct vr_ip *)pkt_data(pkt);
hlen = ip->ip_hl * 4;
pkt_pull(pkt, hlen);
/*
* this is a check to make sure that packets were indeed destined to
* me or not. there are two ways a packet can reach here. either through
*
* route lookup->receive nexthop->vr_ip_rcv or through
* VP_FLAG_TO_ME(NO route lookup(!vp->vp_nh))->vr_ip_rcv
*/
if ((pkt->vp_nh) || (vr_myip(pkt->vp_if, ip->ip_daddr))) {
if (ip->ip_proto == VR_IP_PROTO_GRE) {
unhandled = vr_gre_input(router, pkt, fmd);
} else if (ip->ip_proto == VR_IP_PROTO_UDP) {
unhandled = vr_udp_input(router, pkt, fmd);
} else if (ip->ip_proto == VR_IP_PROTO_ICMP) {
unhandled = vr_icmp_input(router, pkt, fmd);
}
}
if (unhandled) {
/*
* the gre, udp, icmp handlers could have pulled the packet. so
* reset the notion of ip header
*/
if (!(ip = (struct vr_ip *)pkt_push(pkt, hlen))) {
drop_reason = VP_DROP_PUSH;
goto drop_pkt;
}
/* ...and position the data back to l4 header */
pkt_pull(pkt, hlen);
if (pkt->vp_nh) {
vif = pkt->vp_nh->nh_dev;
/*
* If flow processing is already not done, relaxed policy
* enabled, not in cross connect mode, not mirror packet,
* lets subject it to flow processing.
*/
if (pkt->vp_nh->nh_flags & NH_FLAG_RELAXED_POLICY) {
if (!(pkt->vp_flags & VP_FLAG_FLOW_SET) &&
!(pkt->vp_flags & (VP_FLAG_TO_ME | VP_FLAG_FROM_DP))) {
if ((ip->ip_proto == VR_IP_PROTO_UDP) ||
(ip->ip_proto == VR_IP_PROTO_TCP)) {
if (vr_ip_transport_header_valid(ip)) {
l4_port = *(unsigned short *) (pkt_data(pkt) + 2);
} else {
frag = vr_fragment_get(router, fmd->fmd_dvrf, ip);
if (frag)
l4_port = frag->f_dport;
}
if (l4_port && vr_valid_link_local_port(router, AF_INET,
ip->ip_proto, ntohs(l4_port))) {
/* Force the flow lookup */
pkt->vp_flags |= VP_FLAG_FLOW_GET;
/* Get back the IP header */
if (!pkt_push(pkt, hlen)) {
drop_reason = VP_DROP_PUSH;
goto drop_pkt;
}
/* Subject it to flow for Linklocal */
if (!vr_flow_forward(router, pkt, fmd))
return 0;
if (!vr_l3_input(pkt, fmd)) {
drop_reason = VP_DROP_NOWHERE_TO_GO;
goto drop_pkt;
}
return 0;
}
}
}
}
}
if (!vif && !(vif = pkt->vp_if->vif_bridge) &&
!(vif = router->vr_host_if)) {
drop_reason = VP_DROP_TRAP_NO_IF;
goto drop_pkt;
}
if ((pkt->vp_flags & VP_FLAG_FROM_DP) ||
!vr_phead_len(pkt)) {
/* get the ip header back */
if (!pkt_push(pkt, hlen)) {
drop_reason = VP_DROP_PUSH;
goto drop_pkt;
}
/* push the l2 header */
l2_hdr = pkt_push(pkt, sizeof(vif->vif_rewrite));
if (!l2_hdr) {
drop_reason = VP_DROP_PUSH;
goto drop_pkt;
}
memcpy(l2_hdr, vif->vif_rewrite, sizeof(vif->vif_rewrite));
} else {
vr_preset(pkt);
}
ret = vif->vif_tx(vif, pkt, fmd);
}
return ret;
drop_pkt:
vr_pfree(pkt, drop_reason);
return 0;
}
flow_result_t
vr_inet_flow_nat(struct vr_flow_entry *fe, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
bool hdr_update = false;
unsigned int ip_inc, inc = 0;
unsigned short *t_sport, *t_dport;
struct vrouter *router = pkt->vp_if->vif_router;
struct vr_flow_entry *rfe;
struct vr_ip *ip, *icmp_pl_ip;
struct vr_icmp *icmph;
if (fe->fe_rflow < 0)
goto drop;
rfe = vr_get_flow_entry(router, fe->fe_rflow);
if (!rfe)
goto drop;
ip = (struct vr_ip *)pkt_network_header(pkt);
if (ip->ip_proto == VR_IP_PROTO_ICMP) {
icmph = (struct vr_icmp *)((unsigned char *)ip + (ip->ip_hl * 4));
if (vr_icmp_error(icmph)) {
icmp_pl_ip = (struct vr_ip *)(icmph + 1);
if (fe->fe_flags & VR_FLOW_FLAG_SNAT) {
icmp_pl_ip->ip_daddr = rfe->fe_key.flow4_dip;
hdr_update = true;
}
if (fe->fe_flags & VR_FLOW_FLAG_DNAT) {
icmp_pl_ip->ip_saddr = rfe->fe_key.flow4_sip;
hdr_update = true;
}
if (hdr_update)
icmp_pl_ip->ip_csum = vr_ip_csum(icmp_pl_ip);
t_sport = (unsigned short *)((unsigned char *)icmp_pl_ip +
(icmp_pl_ip->ip_hl * 4));
t_dport = t_sport + 1;
if (fe->fe_flags & VR_FLOW_FLAG_SPAT)
*t_dport = rfe->fe_key.flow4_dport;
if (fe->fe_flags & VR_FLOW_FLAG_DPAT)
*t_sport = rfe->fe_key.flow4_sport;
}
}
if ((fe->fe_flags & VR_FLOW_FLAG_SNAT) &&
(ip->ip_saddr == fe->fe_key.flow4_sip)) {
vr_incremental_diff(ip->ip_saddr, rfe->fe_key.flow4_dip, &inc);
ip->ip_saddr = rfe->fe_key.flow4_dip;
}
if (fe->fe_flags & VR_FLOW_FLAG_DNAT) {
vr_incremental_diff(ip->ip_daddr, rfe->fe_key.flow4_sip, &inc);
ip->ip_daddr = rfe->fe_key.flow4_sip;
}
ip_inc = inc;
if (vr_ip_transport_header_valid(ip)) {
t_sport = (unsigned short *)((unsigned char *)ip +
(ip->ip_hl * 4));
t_dport = t_sport + 1;
if (fe->fe_flags & VR_FLOW_FLAG_SPAT) {
vr_incremental_diff(*t_sport, rfe->fe_key.flow4_dport, &inc);
*t_sport = rfe->fe_key.flow4_dport;
}
if (fe->fe_flags & VR_FLOW_FLAG_DPAT) {
vr_incremental_diff(*t_dport, rfe->fe_key.flow4_sport, &inc);
*t_dport = rfe->fe_key.flow4_sport;
}
}
if (!vr_pkt_is_diag(pkt))
vr_ip_update_csum(pkt, ip_inc, inc);
if ((fe->fe_flags & VR_FLOW_FLAG_VRFT) && pkt->vp_nh &&
((pkt->vp_nh->nh_vrf != fmd->fmd_dvrf) ||
(pkt->vp_nh->nh_flags & NH_FLAG_ROUTE_LOOKUP))) {
/* only if pkt->vp_nh was set before... */
pkt->vp_nh = vr_inet_ip_lookup(fmd->fmd_dvrf, ip->ip_daddr);
}
return FLOW_FORWARD;
drop:
vr_pfree(pkt, VP_DROP_FLOW_NAT_NO_RFLOW);
return FLOW_CONSUMED;
}
static void
vr_inet_flow_swap(struct vr_flow *key_p)
{
unsigned short port;
unsigned int ipaddr;
if (key_p->flow4_proto != VR_IP_PROTO_ICMP) {
port = key_p->flow4_sport;
key_p->flow4_sport = key_p->flow4_dport;
key_p->flow4_dport = port;
}
ipaddr = key_p->flow4_sip;
key_p->flow4_sip = key_p->flow4_dip;
key_p->flow4_dip = ipaddr;
return;
}
unsigned short
vr_inet_flow_nexthop(struct vr_packet *pkt, unsigned short vlan)
{
unsigned short nh_id;
if (vif_is_fabric(pkt->vp_if) && pkt->vp_nh) {
/* this is more a requirement from agent */
if ((pkt->vp_nh->nh_type == NH_ENCAP)) {
nh_id = pkt->vp_nh->nh_dev->vif_nh_id;
} else {
nh_id = pkt->vp_nh->nh_id;
}
} else if (vif_is_service(pkt->vp_if)) {
nh_id = vif_vrf_table_get_nh(pkt->vp_if, vlan);
} else {
nh_id = pkt->vp_if->vif_nh_id;
}
return nh_id;
}
void
vr_inet_fill_flow(struct vr_flow *flow_p, unsigned short nh_id,
unsigned char *ip, uint8_t proto, uint16_t sport, uint16_t dport)
{
/* copy both source and destinations */
memcpy(flow_p->flow_ip, ip, 2 * VR_IP_ADDRESS_LEN);
flow_p->flow4_proto = proto;
flow_p->flow4_nh_id = nh_id;
flow_p->flow4_sport = sport;
flow_p->flow4_dport = dport;
flow_p->flow4_family = AF_INET;
flow_p->flow4_unused = 0;
flow_p->flow_key_len = VR_FLOW_IPV4_HASH_SIZE;
return;
}
static int
vr_inet_fragment_flow(struct vrouter *router, unsigned short vrf,
struct vr_packet *pkt, uint16_t vlan, struct vr_flow *flow_p)
{
uint16_t sport, dport;
unsigned short nh_id;
struct vr_fragment *frag;
struct vr_ip *ip = (struct vr_ip *)pkt_network_header(pkt);
frag = vr_fragment_get(router, vrf, ip);
if (!frag) {
return -1;
}
frag->f_received += (ntohs(ip->ip_len) - (ip->ip_hl * 4));
if (vr_ip_fragment_tail(ip)) {
frag->f_expected = ((ntohs(ip->ip_frag_off) & 0x1FFF) * 8) +
ntohs(ip->ip_len) - (ip->ip_hl * 4) ;
}
sport = frag->f_sport;
dport = frag->f_dport;
if (frag->f_received == frag->f_expected)
vr_fragment_del(frag);
nh_id = vr_inet_flow_nexthop(pkt, vlan);
vr_inet_fill_flow(flow_p, nh_id, (unsigned char *)&ip->ip_saddr,
ip->ip_proto, sport, dport);
return 0;
}
bool
vr_inet_flow_allow_new_flow(struct vrouter *router, struct vr_packet *pkt)
{
struct vr_ip *iph = (struct vr_ip *)pkt_network_header(pkt);
if (vr_ip_fragment(iph) && !vr_ip_fragment_head(iph))
return false;
return true;
}
bool
vr_inet_flow_is_fat_flow(struct vrouter *router, struct vr_packet *pkt,
struct vr_flow_entry *fe)
{
if (!fe->fe_key.flow4_sport || !fe->fe_key.flow4_dport) {
if ((fe->fe_key.flow4_proto == VR_IP_PROTO_TCP) ||
(fe->fe_key.flow4_proto == VR_IP_PROTO_UDP) ||
(fe->fe_key.flow4_proto == VR_IP_PROTO_SCTP)) {
return true;
}
}
return false;
}
static int
vr_inet_proto_flow(struct vrouter *router, unsigned short vrf,
struct vr_packet *pkt, uint16_t vlan, struct vr_ip *ip,
struct vr_flow *flow_p)
{
int ret = 0;
unsigned short *t_hdr, sport, dport;
unsigned short nh_id;
struct vr_icmp *icmph;
fat_flow_port_mask_t port_mask;
t_hdr = (unsigned short *)((char *)ip + (ip->ip_hl * 4));
if (ip->ip_proto == VR_IP_PROTO_ICMP) {
icmph = (struct vr_icmp *)t_hdr;
if (vr_icmp_error(icmph)) {
/* we will generate a flow only for the first icmp error */
if ((unsigned char *)ip == pkt_network_header(pkt)) {
ret = vr_inet_proto_flow(router, vrf, pkt, vlan,
(struct vr_ip *)(icmph + 1), flow_p);
if (ret)
return ret;
vr_inet_flow_swap(flow_p);
} else {
/* for icmp error for icmp error, we will drop the packet */
return -1;
}
sport = flow_p->flow4_sport;
dport = flow_p->flow4_dport;
} else if (vr_icmp_echo(icmph)) {
sport = icmph->icmp_eid;
dport = VR_ICMP_TYPE_ECHO_REPLY;
} else {
sport = 0;
dport = icmph->icmp_type;
}
} else if ((ip->ip_proto == VR_IP_PROTO_TCP) ||
(ip->ip_proto == VR_IP_PROTO_UDP) ||
(ip->ip_proto == VR_IP_PROTO_SCTP)) {
sport = *t_hdr;
dport = *(t_hdr + 1);
} else {
sport = 0;
dport = 0;
}
port_mask = vr_flow_fat_flow_lookup(router, pkt, ip->ip_proto,
sport, dport);
switch (port_mask) {
case SOURCE_PORT_MASK:
sport = 0;
break;
case DESTINATION_PORT_MASK:
dport = 0;
break;
case ALL_PORT_MASK:
sport = dport = 0;
break;
default:
break;
}
nh_id = vr_inet_flow_nexthop(pkt, vlan);
vr_inet_fill_flow(flow_p, nh_id, (unsigned char *)&ip->ip_saddr,
ip->ip_proto, sport, dport);
return 0;
}
int
vr_inet_form_flow(struct vrouter *router, unsigned short vrf,
struct vr_packet *pkt, uint16_t vlan, struct vr_flow *flow_p)
{
int ret;
struct vr_ip *ip = (struct vr_ip *)pkt_network_header(pkt);
if (vr_ip_transport_header_valid(ip)) {
ret = vr_inet_proto_flow(router, vrf, pkt, vlan, ip, flow_p);
} else {
ret = vr_inet_fragment_flow(router, vrf, pkt, vlan, flow_p);
}
return ret;
}
static bool
vr_inet_should_trap(struct vr_packet *pkt, struct vr_flow *flow_p)
{
uint32_t proto_port;
/*
* dhcp packet handling:
*
* for now we handle dhcp requests from only VMs and that too only
* for VMs that are not in the fabric VRF. dhcp refresh packets will
* anyway hit the route entry and get trapped from there.
*/
if (vif_is_virtual(pkt->vp_if) && vif_dhcp_enabled(pkt->vp_if)) {
proto_port = (flow_p->flow4_proto << VR_FLOW_PROTO_SHIFT) |
flow_p->flow4_sport;
if (proto_port == VR_UDP_DHCP_CPORT) {
return true;
}
}
return false;
}
int
vr_inet_get_flow_key(struct vrouter *router, struct vr_packet *pkt,
struct vr_forwarding_md *fmd, struct vr_flow *flow)
{
int ret;
struct vr_ip *ip;
ret = vr_inet_form_flow(router, fmd->fmd_dvrf, pkt, fmd->fmd_vlan, flow);
if (ret < 0)
return ret;
ip = (struct vr_ip *)pkt_network_header(pkt);
if (vr_ip_fragment_head(ip)) {
vr_fragment_add(router, fmd->fmd_dvrf, ip, flow->flow4_sport,
flow->flow4_dport);
}
return 0;
}
flow_result_t
vr_inet_flow_lookup(struct vrouter *router, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
int ret;
bool lookup = false;
struct vr_flow flow, *flow_p = &flow;
struct vr_ip *ip = (struct vr_ip *)pkt_network_header(pkt);
struct vr_packet *pkt_c;
/*
* if the packet has already done one round of flow lookup, there
* is no point in doing it again, eh?
*/
if (pkt->vp_flags & VP_FLAG_FLOW_SET)
return FLOW_FORWARD;
/*
* if the interface is policy enabled, or if somebody else (eg:nexthop)
* has requested for a policy lookup, packet has to go through a lookup
*/
if ((pkt->vp_if->vif_flags & VIF_FLAG_POLICY_ENABLED) ||
(pkt->vp_flags & VP_FLAG_FLOW_GET)) {
lookup = true;
}
if (!lookup)
return FLOW_FORWARD;