-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.c
1434 lines (1299 loc) · 39.2 KB
/
main.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
/* SPDX-License-Identifier: Apache-2.0
* Copyright(c) 2019 Intel Corporation
*/
#include <arpa/inet.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <getopt.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <rte_memory.h>
#include <rte_memzone.h>
#include <rte_launch.h>
#include <rte_eal.h>
#include <rte_per_lcore.h>
#include <rte_lcore.h>
#include <rte_debug.h>
#include <rte_errno.h>
#include <rte_ip.h>
#include <rte_udp.h>
#include <rte_ether.h>
#include <rte_common.h>
#include <rte_acl.h>
#include <rte_ip.h>
#include <sys/timeb.h>
#include "gtpv2c.h"
#include "gtpv2c_ie.h"
#include "debug_str.h"
#include "interface.h"
#include "packet_filters.h"
#include "dp_ipc_api.h"
#include "cp.h"
#include "cp_stats.h"
#ifdef ZMQ_COMM
#include "gtpv2c_set_ie.h"
#endif /* ZMQ_COMM */
#ifdef SDN_ODL_BUILD
#include "nb.h"
#endif
#define PCAP_TTL (64)
#define PCAP_VIHL (0x0045)
#define S11_MME_IP_SET (0x0001)
#define S11_SGW_IP_SET (0x0002)
#define S5S8_SGWC_IP_SET (0x0004)
#define S5S8_PGWC_IP_SET (0x0008)
#define S1U_SGW_IP_SET (0x0010)
#define S5S8_SGWU_IP_SET (0x0020)
#define S5S8_PGWU_IP_SET (0x0040)
#define IP_POOL_IP_SET (0x0080)
#define IP_POOL_MASK_SET (0x0100)
#define APN_NAME_SET (0x0200)
#define LOG_LEVEL_SET (0x0300)
#define REQ_ARGS (S11_MME_IP_SET | \
S11_SGW_IP_SET | \
S1U_SGW_IP_SET | IP_POOL_IP_SET | \
IP_POOL_MASK_SET | APN_NAME_SET | \
LOG_LEVEL_SET)
#define RTE_LOGTYPE_CP RTE_LOGTYPE_USER4
#ifdef ZMQ_COMM
#define OP_ID_HASH_SIZE (1 << 18)
struct rte_hash *resp_op_id_hash;
#endif /* ZMQ_COMM */
enum cp_config spgw_cfg;
int s11_fd = -1;
int s11_pcap_fd = -1;
int s5s8_sgwc_fd = -1;
int s5s8_pgwc_fd = -1;
pcap_dumper_t *pcap_dumper;
pcap_t *pcap_reader;
struct cp_params cp_params;
/**
* Setting/enable CP RTE LOG_LEVEL.
*/
static void
set_log_level(uint8_t log_level)
{
/** Note :In dpdk set max log level is INFO, here override the
* max value of RTE_LOG_INFO for enable DEBUG logs (dpdk-16.11.4
* and dpdk-18.02).
*/
if (log_level == DEBUG)
rte_log_set_level(RTE_LOGTYPE_CP, RTE_LOG_DEBUG);
else if (log_level == NOTICE)
rte_log_set_global_level(RTE_LOG_NOTICE);
else rte_log_set_global_level(RTE_LOG_INFO);
}
#define SLEEP_SEC 5
#define NUM_RETRIES 10
/**
* Parses c-string containing dotted decimal ipv4 or hostname and stores the
* value within the in_addr type
*
* @param optarg
* c-string containing dotted decimal ipv4 address or hostname
* @param addr
* destination of parsed IP string
*/
void
parse_arg_host(const char *optarg, struct in_addr *addr)
{
int ret = -1, retries = NUM_RETRIES;
struct addrinfo hints, *servinfo;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
/*NUM_RETRIES with SLEEP_SEC in between to resolve the name*/
while(retries-- > 0 && (ret = getaddrinfo(optarg, NULL, &hints, &servinfo) !=0)){
RTE_LOG(ERR, CP, "Unable to resolve %s. Retrying in %d sec\n",
optarg, SLEEP_SEC);
sleep(SLEEP_SEC);
}
if(ret)
rte_exit(EXIT_FAILURE, "Unable to resolve %s. Exiting\n", optarg);
struct sockaddr_in *h = (struct sockaddr_in *) servinfo->ai_addr;
memcpy(addr, &h->sin_addr, sizeof(struct in_addr));
freeaddrinfo(servinfo);
}
/**
*
* Parses non-dpdk command line program arguments for control plane
*
* @param argc
* number of arguments
* @param argv
* array of c-string arguments
*/
static void
parse_arg(int argc, char **argv)
{
char errbuff[PCAP_ERRBUF_SIZE];
int args_set = 0;
int c = 0;
pcap_t *pcap;
int apnidx = 0;
const struct option long_options[] = {
{"spgw_cfg", required_argument, NULL, 'd'},
{"s11_mme_ip", required_argument, NULL, 'm'},
{"s11_sgw_ip", required_argument, NULL, 's'},
{"s5s8_sgwc_ip", optional_argument, NULL, 'r'},
{"s5s8_pgwc_ip", optional_argument, NULL, 'g'},
{"s1u_sgw_ip", required_argument, NULL, 'w'},
{"s5s8_sgwu_ip", optional_argument, NULL, 'v'},
{"s5s8_pgwu_ip", optional_argument, NULL, 'u'},
{"ip_pool_ip", required_argument, NULL, 'i'},
{"ip_pool_mask", required_argument, NULL, 'p'},
{"apn_name", required_argument, NULL, 'a'},
{"log_level", required_argument, NULL, 'l'},
{"pcap_file_in", required_argument, NULL, 'x'},
{"pcap_file_out", required_argument, NULL, 'y'},
{0, 0, 0, 0}
};
do {
int option_index = 0;
c = getopt_long(argc, argv, "d:m:s:r:g:w:v:u:i:p:a:l:x:y:", long_options,
&option_index);
if (c == -1)
break;
switch (c) {
case 'd':
spgw_cfg = (uint8_t)atoi(optarg);
args_set |= S11_MME_IP_SET;
break;
case 'm':
parse_arg_host(optarg, &s11_mme_ip);
args_set |= S11_MME_IP_SET;
break;
case 's':
parse_arg_host(optarg, &s11_sgw_ip);
args_set |= S11_SGW_IP_SET;
break;
case 'r':
parse_arg_host(optarg, &s5s8_sgwc_ip);
args_set |= S5S8_SGWC_IP_SET;
break;
case 'g':
parse_arg_host(optarg, &s5s8_pgwc_ip);
args_set |= S5S8_PGWC_IP_SET;
break;
case 'w':
parse_arg_host(optarg, &s1u_sgw_ip);
args_set |= IP_POOL_MASK_SET;
break;
case 'v':
parse_arg_host(optarg, &s5s8_sgwu_ip);
args_set |= S5S8_SGWU_IP_SET;
break;
case 'u':
parse_arg_host(optarg, &s5s8_pgwu_ip);
args_set |= S5S8_PGWU_IP_SET;
break;
case 'i':
set_ip_pool_ip(optarg);
args_set |= S1U_SGW_IP_SET;
break;
case 'p':
set_ip_pool_mask(optarg);
args_set |= IP_POOL_IP_SET;
break;
case 'a':
if (apnidx < MAX_NB_DPN) {
set_apn_name(&apn_list[apnidx++], optarg);
args_set |= APN_NAME_SET;
}
break;
case 'l':
set_log_level((uint8_t)atoi(optarg));
args_set |= LOG_LEVEL_SET;
break;
case 'x':
pcap_reader = pcap_open_offline(optarg, errbuff);
break;
case 'y':
pcap = pcap_open_dead(DLT_EN10MB, UINT16_MAX);
pcap_dumper = pcap_dump_open(pcap, optarg);
s11_pcap_fd = pcap_fileno(pcap);
break;
default:
rte_panic("Unknown argument - %s.", argv[optind]);
break;
}
} while (c != -1);
if ((args_set & REQ_ARGS) != REQ_ARGS) {
fprintf(stderr, "Usage: %s\n", argv[0]);
for (c = 0; long_options[c].name; ++c) {
fprintf(stderr, "\t[ -%s | -%c ] %s\n",
long_options[c].name,
long_options[c].val,
long_options[c].name);
}
rte_panic("\n");
}
}
void
initialize_tables_on_dp(void)
{
#ifdef CP_DP_TABLE_CONFIG
struct dp_id dp_id = { .id = DPN_ID };
sprintf(dp_id.name, SDF_FILTER_TABLE);
if (sdf_filter_table_create(dp_id, SDF_FILTER_TABLE_SIZE))
rte_panic("sdf_filter_table creation failed\n");
sprintf(dp_id.name, ADC_TABLE);
if (adc_table_create(dp_id, ADC_TABLE_SIZE))
rte_panic("adc_table creation failed\n");
sprintf(dp_id.name, PCC_TABLE);
if (pcc_table_create(dp_id, PCC_TABLE_SIZE))
rte_panic("pcc_table creation failed\n");
sprintf(dp_id.name, METER_PROFILE_SDF_TABLE);
if (meter_profile_table_create(dp_id, METER_PROFILE_SDF_TABLE_SIZE))
rte_panic("meter_profile_sdf_table creation failed\n");
sprintf(dp_id.name, SESSION_TABLE);
if (session_table_create(dp_id, LDB_ENTRIES_DEFAULT))
rte_panic("session_table creation failed\n");
#endif
}
/**
* @brief Initalizes S11 interface if in use
*/
static void
init_s11(void)
{
int ret;
s11_mme_sockaddr.sin_port = htons(GTPC_UDP_PORT);
s11_port = htons(GTPC_UDP_PORT);
if (pcap_reader != NULL && pcap_dumper != NULL)
return;
s11_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (s11_fd < 0)
rte_panic("Socket call error : %s", strerror(errno));
bzero(s11_sgw_sockaddr.sin_zero,
sizeof(s11_sgw_sockaddr.sin_zero));
s11_sgw_sockaddr.sin_family = AF_INET;
s11_sgw_sockaddr.sin_port = s11_port;
s11_sgw_sockaddr.sin_addr = s11_sgw_ip;
ret = bind(s11_fd, (struct sockaddr *) &s11_sgw_sockaddr,
sizeof(struct sockaddr_in));
RTE_LOG_DP(INFO, CP, "NGIC- main.c::init_s11()"
"\n\ts11_fd= %d :: "
"\n\ts11_sgw_ip= %s : s11_port= %d\n",
s11_fd, inet_ntoa(s11_sgw_ip), ntohs(s11_port));
if (ret < 0) {
rte_panic("Bind error for %s:%u - %s\n",
inet_ntoa(s11_sgw_sockaddr.sin_addr),
ntohs(s11_sgw_sockaddr.sin_port),
strerror(errno));
}
}
/**
* @brief Initalizes s5s8_sgwc interface if in use
*/
static void
init_s5s8_sgwc(void)
{
int ret;
s5s8_sgwc_port = htons(GTPC_UDP_PORT);
if (pcap_reader != NULL && pcap_dumper != NULL)
return;
s5s8_sgwc_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (s5s8_sgwc_fd < 0)
rte_panic("Socket call error : %s", strerror(errno));
bzero(s5s8_sgwc_sockaddr.sin_zero,
sizeof(s5s8_sgwc_sockaddr.sin_zero));
s5s8_sgwc_sockaddr.sin_family = AF_INET;
s5s8_sgwc_sockaddr.sin_port = s5s8_sgwc_port;
s5s8_sgwc_sockaddr.sin_addr = s5s8_sgwc_ip;
ret = bind(s5s8_sgwc_fd, (struct sockaddr *) &s5s8_sgwc_sockaddr,
sizeof(struct sockaddr_in));
RTE_LOG_DP(INFO, CP, "NGIC- main.c::init_s5s8_sgwc()"
"\n\ts5s8_sgwc_fd= %d :: "
"\n\ts5s8_sgwc_ip= %s : s5s8_sgwc_port= %d\n",
s5s8_sgwc_fd, inet_ntoa(s5s8_sgwc_ip),
ntohs(s5s8_sgwc_port));
if (ret < 0) {
rte_panic("Bind error for %s:%u - %s\n",
inet_ntoa(s5s8_sgwc_sockaddr.sin_addr),
ntohs(s5s8_sgwc_sockaddr.sin_port),
strerror(errno));
}
/* Initialize peer pgwc inteface for sendto(.., dest_addr) */
s5s8_pgwc_port = htons(GTPC_UDP_PORT);
bzero(s5s8_pgwc_sockaddr.sin_zero,
sizeof(s5s8_pgwc_sockaddr.sin_zero));
s5s8_pgwc_sockaddr.sin_family = AF_INET;
s5s8_pgwc_sockaddr.sin_port = s5s8_pgwc_port;
s5s8_pgwc_sockaddr.sin_addr = s5s8_pgwc_ip;
}
/**
* @brief Initalizes s5s8_pgwc interface if in use
*/
static void
init_s5s8_pgwc(void)
{
int ret;
s5s8_pgwc_port = htons(GTPC_UDP_PORT);
if (pcap_reader != NULL && pcap_dumper != NULL)
return;
s5s8_pgwc_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (s5s8_pgwc_fd < 0)
rte_panic("Socket call error : %s", strerror(errno));
bzero(s5s8_pgwc_sockaddr.sin_zero,
sizeof(s5s8_pgwc_sockaddr.sin_zero));
s5s8_pgwc_sockaddr.sin_family = AF_INET;
s5s8_pgwc_sockaddr.sin_port = s5s8_pgwc_port;
s5s8_pgwc_sockaddr.sin_addr = s5s8_pgwc_ip;
ret = bind(s5s8_pgwc_fd, (struct sockaddr *) &s5s8_pgwc_sockaddr,
sizeof(struct sockaddr_in));
RTE_LOG_DP(INFO, CP, "NGIC- main.c::init_s5s8_sgwc()"
"\n\ts5s8_pgwc_fd= %d :: "
"\n\ts5s8_pgwc_ip= %s : s5s8_pgwc_port= %d\n",
s5s8_pgwc_fd, inet_ntoa(s5s8_pgwc_ip),
ntohs(s5s8_pgwc_port));
if (ret < 0) {
rte_panic("Bind error for %s:%u - %s\n",
inet_ntoa(s5s8_pgwc_sockaddr.sin_addr),
ntohs(s5s8_pgwc_sockaddr.sin_port),
strerror(errno));
}
/* Initialize peer sgwc inteface for sendto(.., dest_addr) */
s5s8_sgwc_port = htons(GTPC_UDP_PORT);
bzero(s5s8_sgwc_sockaddr.sin_zero,
sizeof(s5s8_sgwc_sockaddr.sin_zero));
s5s8_sgwc_sockaddr.sin_family = AF_INET;
s5s8_sgwc_sockaddr.sin_port = s5s8_sgwc_port;
s5s8_sgwc_sockaddr.sin_addr = s5s8_sgwc_ip;
}
/**
* @brief
* Initializes Control Plane data structures, packet filters, and calls for the
* Data Plane to create required tables
*/
static void
init_cp(void)
{
switch (spgw_cfg) {
case SGWC:
init_s11();
init_s5s8_sgwc();
break;
case PGWC:
init_s5s8_pgwc();
break;
case SPGWC:
init_s11();
break;
default:
rte_panic("main.c::init_cp()-"
"Unknown spgw_cfg= %u.", spgw_cfg);
break;
}
iface_module_constructor();
if (signal(SIGINT, sig_handler) == SIG_ERR)
rte_exit(EXIT_FAILURE, "Error:can't catch SIGINT\n");
if (signal(SIGSEGV, sig_handler) == SIG_ERR)
rte_exit(EXIT_FAILURE, "Error:can't catch SIGSEGV\n");
#ifndef SDN_ODL_BUILD
#ifdef CP_DP_TABLE_CONFIG
initialize_tables_on_dp();
#endif
init_packet_filters();
parse_adc_rules();
#endif
create_ue_hash();
}
/**
* @brief
* Util to send or dump gtpv2c messages
*/
void
gtpv2c_send(int gtpv2c_if_fd, uint8_t *gtpv2c_tx_buf,
uint16_t gtpv2c_pyld_len, struct sockaddr *dest_addr,
socklen_t dest_addr_len)
{
int bytes_tx;
if (pcap_dumper) {
dump_pcap(gtpv2c_pyld_len, gtpv2c_tx_buf);
} else {
bytes_tx = sendto(gtpv2c_if_fd, gtpv2c_tx_buf, gtpv2c_pyld_len, 0,
(struct sockaddr *) dest_addr, dest_addr_len);
RTE_LOG_DP(DEBUG, CP, "NGIC- main.c::gtpv2c_send()"
"\n\tgtpv2c_if_fd= %d\n", gtpv2c_if_fd);
if (bytes_tx != (int) gtpv2c_pyld_len) {
fprintf(stderr, "Transmitted Incomplete GTPv2c Message:"
"%u of %d tx bytes\n",
gtpv2c_pyld_len, bytes_tx);
}
}
}
void
dump_pcap(uint16_t payload_length, uint8_t *tx_buf)
{
static struct pcap_pkthdr pcap_tx_header;
gettimeofday(&pcap_tx_header.ts, NULL);
pcap_tx_header.caplen = payload_length
+ sizeof(struct ether_hdr)
+ sizeof(struct ipv4_hdr)
+ sizeof(struct udp_hdr);
pcap_tx_header.len = payload_length
+ sizeof(struct ether_hdr)
+ sizeof(struct ipv4_hdr)
+ sizeof(struct udp_hdr);
uint8_t dump_buf[MAX_GTPV2C_UDP_LEN
+ sizeof(struct ether_hdr)
+ sizeof(struct ipv4_hdr)
+ sizeof(struct udp_hdr)];
struct ether_hdr *eh = (struct ether_hdr *) dump_buf;
memset(&eh->d_addr, '\0', sizeof(struct ether_addr));
memset(&eh->s_addr, '\0', sizeof(struct ether_addr));
eh->ether_type = htons(ETHER_TYPE_IPv4);
struct ipv4_hdr *ih = (struct ipv4_hdr *) &eh[1];
ih->dst_addr = s11_mme_ip.s_addr;
ih->src_addr = s11_sgw_ip.s_addr;
ih->next_proto_id = IPPROTO_UDP;
ih->version_ihl = PCAP_VIHL;
ih->total_length =
ntohs(payload_length
+ sizeof(struct udp_hdr)
+ sizeof(struct ipv4_hdr));
ih->time_to_live = PCAP_TTL;
struct udp_hdr *uh = (struct udp_hdr *) &ih[1];
uh->dgram_len = htons(
ntohs(ih->total_length) - sizeof(struct ipv4_hdr));
uh->dst_port = htons(GTPC_UDP_PORT);
uh->src_port = htons(GTPC_UDP_PORT);
void *payload = &uh[1];
memcpy(payload, tx_buf, payload_length);
pcap_dump((u_char *) pcap_dumper, &pcap_tx_header,
dump_buf);
fflush(pcap_dump_file(pcap_dumper));
}
socklen_t s11_mme_sockaddr_len = sizeof(s11_mme_sockaddr);
socklen_t s5s8_sgwc_sockaddr_len = sizeof(s5s8_sgwc_sockaddr);
socklen_t s5s8_pgwc_sockaddr_len = sizeof(s5s8_pgwc_sockaddr);
void
control_plane(void)
{
bzero(&s11_rx_buf, sizeof(s11_rx_buf));
bzero(&s11_tx_buf, sizeof(s11_tx_buf));
bzero(&s5s8_rx_buf, sizeof(s5s8_rx_buf));
bzero(&s5s8_tx_buf, sizeof(s5s8_tx_buf));
gtpv2c_header *gtpv2c_s11_rx = (gtpv2c_header *) s11_rx_buf;
gtpv2c_header *gtpv2c_s11_tx = (gtpv2c_header *) s11_tx_buf;
gtpv2c_header *gtpv2c_s5s8_rx = (gtpv2c_header *) s5s8_rx_buf;
gtpv2c_header *gtpv2c_s5s8_tx = (gtpv2c_header *) s5s8_tx_buf;
uint16_t payload_length;
uint8_t delay = 0; /*TODO move this when more implemented?*/
int bytes_pcap_rx = 0;
int bytes_s11_rx = 0;
int bytes_s5s8_rx = 0;
static uint8_t s11_msgcnt = 0;
static uint8_t s5s8_sgwc_msgcnt = 0;
static uint8_t s5s8_pgwc_msgcnt = 0;
int ret = 0;
if (pcap_reader) {
static struct pcap_pkthdr *pcap_rx_header;
const u_char *t;
const u_char **tmp = &t;
ret = pcap_next_ex(pcap_reader, &pcap_rx_header, tmp);
if (ret < 0) {
printf("Finished reading from pcap file"
" - exiting\n");
exit(0);
}
bytes_pcap_rx = pcap_rx_header->caplen
- (sizeof(struct ether_hdr)
+ sizeof(struct ipv4_hdr)
+ sizeof(struct udp_hdr));
memcpy(gtpv2c_s11_rx, *tmp
+ (sizeof(struct ether_hdr)
+ sizeof(struct ipv4_hdr)
+ sizeof(struct udp_hdr)), bytes_pcap_rx);
}
if (spgw_cfg == SGWC) {
bytes_s5s8_rx = recvfrom(s5s8_sgwc_fd, s5s8_rx_buf,
MAX_GTPV2C_UDP_LEN, MSG_DONTWAIT,
(struct sockaddr *) &s5s8_sgwc_sockaddr,
&s5s8_sgwc_sockaddr_len);
if (bytes_s5s8_rx == 0) {
fprintf(stderr, "SGWC_s5s8 recvfrom error:"
"\n\ton %s:%u - %s\n",
inet_ntoa(s5s8_sgwc_sockaddr.sin_addr),
s5s8_sgwc_sockaddr.sin_port,
strerror(errno));
}
}
if (spgw_cfg == PGWC) {
bytes_s5s8_rx = recvfrom(s5s8_pgwc_fd, s5s8_rx_buf,
MAX_GTPV2C_UDP_LEN, MSG_DONTWAIT,
(struct sockaddr *) &s5s8_sgwc_sockaddr,
&s5s8_sgwc_sockaddr_len);
if (bytes_s5s8_rx == 0) {
fprintf(stderr, "PGWC_s5s8 recvfrom error:"
"\n\ton %s:%u - %s\n",
inet_ntoa(s5s8_sgwc_sockaddr.sin_addr),
s5s8_sgwc_sockaddr.sin_port,
strerror(errno));
}
}
if ((spgw_cfg == SGWC) || (spgw_cfg == SPGWC)) {
bytes_s11_rx = recvfrom(s11_fd, s11_rx_buf,
MAX_GTPV2C_UDP_LEN, MSG_DONTWAIT,
(struct sockaddr *) &s11_mme_sockaddr,
&s11_mme_sockaddr_len);
if (bytes_s11_rx == 0) {
fprintf(stderr, "SGWC|SPGWC_s11 recvfrom error:"
"\n\ton %s:%u - %s\n",
inet_ntoa(s11_mme_sockaddr.sin_addr),
s11_mme_sockaddr.sin_port,
strerror(errno));
return;
}
}
if (
(bytes_s5s8_rx < 0) && (bytes_s11_rx < 0) &&
(errno == EAGAIN || errno == EWOULDBLOCK)
)
return;
if ((spgw_cfg == SGWC) || (spgw_cfg == PGWC)) {
if ((bytes_s5s8_rx > 0) &&
(unsigned)bytes_s5s8_rx != (
ntohs(gtpv2c_s5s8_rx->gtpc.length)
+ sizeof(gtpv2c_s5s8_rx->gtpc))
) {
ret = GTPV2C_CAUSE_INVALID_LENGTH;
/* According to 29.274 7.7.7, if message is request,
* reply with cause = GTPV2C_CAUSE_INVALID_LENGTH
* should be sent - ignoring packet for now
*/
fprintf(stderr, "SGWC|PGWC_s5s8 Received UDP Payload:"
"\n\t(%d bytes) with gtpv2c + "
"header (%u + %lu) = %lu bytes\n",
bytes_s5s8_rx, ntohs(gtpv2c_s5s8_rx->gtpc.length),
sizeof(gtpv2c_s5s8_rx->gtpc),
ntohs(gtpv2c_s5s8_rx->gtpc.length)
+ sizeof(gtpv2c_s5s8_rx->gtpc));
}
}
if ((spgw_cfg == SGWC) || (spgw_cfg == SPGWC)) {
if (
(bytes_s11_rx > 0) &&
(unsigned)bytes_s11_rx !=
(ntohs(gtpv2c_s11_rx->gtpc.length)
+ sizeof(gtpv2c_s11_rx->gtpc))
) {
ret = GTPV2C_CAUSE_INVALID_LENGTH;
/* According to 29.274 7.7.7, if message is request,
* reply with cause = GTPV2C_CAUSE_INVALID_LENGTH
* should be sent - ignoring packet for now
*/
fprintf(stderr, "SGWC|SPGWC_s11 Received UDP Payload:"
"\n\t(%d bytes) with gtpv2c + "
"header (%u + %lu) = %lu bytes\n",
bytes_s11_rx, ntohs(gtpv2c_s11_rx->gtpc.length),
sizeof(gtpv2c_s11_rx->gtpc),
ntohs(gtpv2c_s11_rx->gtpc.length)
+ sizeof(gtpv2c_s11_rx->gtpc));
return;
}
}
if ((bytes_s5s8_rx > 0) || (bytes_s11_rx > 0))
++cp_stats.rx;
if (!pcap_reader) {
if (
((spgw_cfg == SGWC) || (spgw_cfg == SPGWC)) &&
(bytes_s11_rx > 0) &&
(
(s11_mme_sockaddr.sin_addr.s_addr != s11_mme_ip.s_addr) ||
(gtpv2c_s11_rx->gtpc.version != GTP_VERSION_GTPV2C)
)
) {
fprintf(stderr, "Discarding packet from %s:%u - "
"Expected S11_MME_IP = %s\n",
inet_ntoa(s11_mme_sockaddr.sin_addr),
ntohs(s11_mme_sockaddr.sin_port),
inet_ntoa(s11_mme_ip));
return;
} else if (
((spgw_cfg == PGWC) && (bytes_s5s8_rx > 0)) &&
(
(s5s8_sgwc_sockaddr.sin_addr.s_addr !=
s5s8_sgwc_ip.s_addr) ||
(gtpv2c_s5s8_rx->gtpc.version != GTP_VERSION_GTPV2C)
)
) {
fprintf(stderr, "Discarding packet from %s:%u - "
"Expected S5S8_SGWC_IP = %s\n",
inet_ntoa(s5s8_sgwc_sockaddr.sin_addr),
ntohs(s5s8_sgwc_sockaddr.sin_port),
inet_ntoa(s5s8_sgwc_ip));
return;
}
}
if (bytes_s5s8_rx > 0) {
if (spgw_cfg == SGWC) {
switch (gtpv2c_s5s8_rx->gtpc.type) {
case GTP_CREATE_SESSION_RSP:
/* Check s5s8 GTP_CREATE_SESSION_RSP */
ret = process_sgwc_s5s8_create_session_response(
gtpv2c_s5s8_rx, gtpv2c_s11_tx);
if (ret) {
fprintf(stderr, "main.c::control_plane()::Error"
"\n\tcase SGWC:"
"\n\tprocess_sgwc_s5s8_create_session_response "
"%s: (%d) %s\n",
gtp_type_str(gtpv2c_s5s8_rx->gtpc.type), ret,
(ret < 0 ? strerror(-ret) : cause_str(ret)));
/* Error handling not implemented */
return;
}
payload_length = ntohs(gtpv2c_s11_tx->gtpc.length)
+ sizeof(gtpv2c_s11_tx->gtpc);
RTE_LOG_DP(DEBUG, CP, "NGIC- main.c::control_plane()::"
"\n\tcase GTP_CREATE_SESSION_RSP"
"\n\ts11_msgcnt= %u;"
"\n\tgtpv2c_send :: s11_fd= %d;"
"\n\tdest_addr= %s : dest_addrln= %u;"
"\n\tdest_port= %u\n",
s11_msgcnt, s11_fd,
inet_ntoa(s11_mme_sockaddr.sin_addr),
s11_mme_sockaddr_len,
ntohs(s11_mme_sockaddr.sin_port));
/* Note: s11_tx_buf should be prepared by call to:
* process_sgwc_s5s8_create_session_response
*/
gtpv2c_send(s11_fd, s11_tx_buf, payload_length,
(struct sockaddr *) &s11_mme_sockaddr,
s11_mme_sockaddr_len);
s11_msgcnt++;
break;
case GTP_DELETE_SESSION_RSP:
/* Check s5s8 GTP_DELETE_SESSION_RSP */
ret = process_sgwc_s5s8_delete_session_response(
gtpv2c_s5s8_rx, gtpv2c_s11_tx);
if (ret) {
fprintf(stderr, "main.c::control_plane()::Error"
"\n\tcase SGWC:"
"\n\tprocess_sgwc_s5s8_delete_session_response "
"%s: (%d) %s\n",
gtp_type_str(gtpv2c_s5s8_rx->gtpc.type), ret,
(ret < 0 ? strerror(-ret) : cause_str(ret)));
/* Error handling not implemented */
return;
}
payload_length = ntohs(gtpv2c_s11_tx->gtpc.length)
+ sizeof(gtpv2c_s11_tx->gtpc);
RTE_LOG_DP(DEBUG, CP, "NGIC- main.c::control_plane()::"
"\n\tcase GTP_DELETE_SESSION_RSP"
"\n\ts11_msgcnt= %u;"
"\n\tgtpv2c_send :: s11_fd= %d;"
"\n\tdest_addr= %s : dest_addrln= %u;"
"\n\tdest_port= %u\n",
s11_msgcnt, s11_fd,
inet_ntoa(s11_mme_sockaddr.sin_addr),
s11_mme_sockaddr_len,
ntohs(s11_mme_sockaddr.sin_port));
/* Note: s11_tx_buf should be prepared by call to:
* process_sgwc_s5s8_delete_session_response
*/
gtpv2c_send(s11_fd, s11_tx_buf, payload_length,
(struct sockaddr *) &s11_mme_sockaddr,
s11_mme_sockaddr_len);
s11_msgcnt++;
break;
default:
fprintf(stderr, "main.c::control_plane::process_msgs-"
"\n\tcase: SGWC::spgw_cfg= %d;"
"\n\tReceived unprocessed s5s8 GTPv2c Message Type: "
"%s (%u 0x%x)... Discarding\n",
spgw_cfg, gtp_type_str(gtpv2c_s5s8_rx->gtpc.type),
gtpv2c_s5s8_rx->gtpc.type,
gtpv2c_s5s8_rx->gtpc.type);
return;
break;
}
}
if (spgw_cfg == PGWC) {
switch (gtpv2c_s5s8_rx->gtpc.type) {
case GTP_CREATE_SESSION_REQ:
ret = process_pgwc_s5s8_create_session_request(
gtpv2c_s5s8_rx, gtpv2c_s5s8_tx);
if (ret) {
fprintf(stderr, "main.c::control_plane()::Error"
"\n\tcase PGWC:"
"\n\tprocess_pgwc_s5s8_create_session_request "
"%s: (%d) %s\n",
gtp_type_str(gtpv2c_s5s8_rx->gtpc.type), ret,
(ret < 0 ? strerror(-ret) : cause_str(ret)));
/* Error handling not implemented */
return;
}
payload_length = ntohs(gtpv2c_s5s8_tx->gtpc.length)
+ sizeof(gtpv2c_s5s8_tx->gtpc);
RTE_LOG_DP(DEBUG, CP, "NGIC- main.c::control_plane()::"
"\n\tcase GTP_CREATE_SESSION_REQ"
"\n\ts5s8_pgwc_msgcnt= %u;"
"\n\tgtpv2c_send :: s5s8_pgwc_fd= %d;"
"\n\tdest_addr= %s : dest_addrln= %u;"
"\n\tdest_port= %u\n",
s5s8_pgwc_msgcnt, s5s8_pgwc_fd,
inet_ntoa(s5s8_sgwc_sockaddr.sin_addr),
s5s8_sgwc_sockaddr_len,
ntohs(s5s8_sgwc_sockaddr.sin_port));
#ifndef ZMQ_COMM
gtpv2c_send(s5s8_pgwc_fd, s5s8_tx_buf, payload_length,
(struct sockaddr *) &s5s8_sgwc_sockaddr,
s5s8_sgwc_sockaddr_len);
#endif /* ZMQ_COMM */
s5s8_pgwc_msgcnt++;
break;
case GTP_DELETE_SESSION_REQ:
ret = process_pgwc_s5s8_delete_session_request(
gtpv2c_s5s8_rx, gtpv2c_s5s8_tx);
if (ret) {
fprintf(stderr, "main.c::control_plane()::Error"
"\n\tcase PGWC:"
"\n\tprocess_pgwc_s5s8_delete_session_request "
"%s: (%d) %s\n",
gtp_type_str(gtpv2c_s5s8_rx->gtpc.type), ret,
(ret < 0 ? strerror(-ret) : cause_str(ret)));
/* Error handling not implemented */
return;
}
payload_length = ntohs(gtpv2c_s5s8_tx->gtpc.length)
+ sizeof(gtpv2c_s5s8_tx->gtpc);
RTE_LOG_DP(DEBUG, CP, "NGIC- main.c::control_plane()::"
"\n\tcase GTP_DELETE_SESSION_REQ"
"\n\ts5s8_pgwc_msgcnt= %u;"
"\n\tgtpv2c_send :: s5s8_pgwc_fd= %d;"
"\n\tdest_addr= %s : dest_addrln= %u;"
"\n\tdest_port= %u\n",
s5s8_pgwc_msgcnt, s5s8_pgwc_fd,
inet_ntoa(s5s8_sgwc_sockaddr.sin_addr),
s5s8_sgwc_sockaddr_len,
ntohs(s5s8_sgwc_sockaddr.sin_port));
#ifndef ZMQ_COMM
gtpv2c_send(s5s8_pgwc_fd, s5s8_tx_buf, payload_length,
(struct sockaddr *) &s5s8_sgwc_sockaddr,
s5s8_sgwc_sockaddr_len);
#endif /* ZMQ_COMM */
s5s8_pgwc_msgcnt++;
break;
default:
fprintf(stderr, "main.c::control_plane::process_msgs-"
"\n\tcase: PGWC::spgw_cfg= %d;"
"\n\tReceived unprocessed s5s8 GTPv2c Message Type: "
"%s (%u 0x%x)... Discarding\n",
spgw_cfg, gtp_type_str(gtpv2c_s5s8_rx->gtpc.type),
gtpv2c_s5s8_rx->gtpc.type,
gtpv2c_s5s8_rx->gtpc.type);
return;
break;
}
}
}
if (bytes_s11_rx > 0) {
if ((spgw_cfg == SGWC) || (spgw_cfg == SPGWC)) {
switch (gtpv2c_s11_rx->gtpc.type) {
case GTP_CREATE_SESSION_REQ:
ret = process_create_session_request(
gtpv2c_s11_rx, gtpv2c_s11_tx, gtpv2c_s5s8_tx);
if (ret) {
fprintf(stderr, "main.c::control_plane()::Error"
"\n\tcase SGWC | SPGWC:"
"\n\tprocess_create_session_request "
"%s: (%d) %s\n",
gtp_type_str(gtpv2c_s11_rx->gtpc.type), ret,
(ret < 0 ? strerror(-ret) : cause_str(ret)));
/* Error handling not implemented */
return;
}
if (spgw_cfg == SGWC) {
/* Forward s11 create_session_request on s5s8 */
payload_length = ntohs(gtpv2c_s5s8_tx->gtpc.length)
+ sizeof(gtpv2c_s5s8_tx->gtpc);
RTE_LOG_DP(DEBUG, CP, "NGIC- main.c::control_plane()::"
"\n\tcase GTP_CREATE_SESSION_REQ"
"\n\ts5s8_sgwc_msgcnt= %ui;"
"\n\tgtpv2c_send :: s5s8_sgwc_fd= %d;"
"\n\tdest_addr= %s : dest_addrln= %u;"
"\n\tdest_port= %u\n",
s5s8_sgwc_msgcnt, s5s8_sgwc_fd,
inet_ntoa(s5s8_pgwc_sockaddr.sin_addr),
s5s8_pgwc_sockaddr_len,
ntohs(s5s8_pgwc_sockaddr.sin_port));
gtpv2c_send(s5s8_sgwc_fd, s5s8_tx_buf, payload_length,
(struct sockaddr *) &s5s8_pgwc_sockaddr,
s5s8_pgwc_sockaddr_len);
s5s8_sgwc_msgcnt++;
}
#ifndef ZMQ_COMM
if (spgw_cfg == SPGWC) {
payload_length = ntohs(gtpv2c_s11_tx->gtpc.length)
+ sizeof(gtpv2c_s11_tx->gtpc);
gtpv2c_send(s11_fd, s11_tx_buf, payload_length,
(struct sockaddr *) &s11_mme_sockaddr,
s11_mme_sockaddr_len);
}
#endif /* ZMQ_COMM */
break;
case GTP_DELETE_SESSION_REQ:
ret = process_delete_session_request(
gtpv2c_s11_rx, gtpv2c_s11_tx, gtpv2c_s5s8_tx);
if (ret) {
fprintf(stderr, "main.c::control_plane()::Error"
"\n\tcase SPGWC:"
"\n\tprocess_delete_session_request "
"%s: (%d) %s\n",
gtp_type_str(gtpv2c_s11_rx->gtpc.type), ret,
(ret < 0 ? strerror(-ret) : cause_str(ret)));
/* Error handling not implemented */
return;
}
if (spgw_cfg == SGWC) {
/* Forward s11 delete_session_request on s5s8 */
payload_length = ntohs(gtpv2c_s5s8_tx->gtpc.length)
+ sizeof(gtpv2c_s5s8_tx->gtpc);
RTE_LOG_DP(DEBUG, CP, "NGIC- main.c::control_plane()::"
"\n\tcase GTP_DELETE_SESSION_REQ"
"\n\ts5s8_sgwc_msgcnt= %u;"
"\n\tgtpv2c_send :: s5s8_sgwc_fd= %d;"
"\n\tdest_addr= %s : dest_addrln= %u;"
"\n\tdest_port= %u\n",
s5s8_sgwc_msgcnt, s5s8_sgwc_fd,
inet_ntoa(s5s8_pgwc_sockaddr.sin_addr),
s5s8_pgwc_sockaddr_len,
ntohs(s5s8_pgwc_sockaddr.sin_port));
gtpv2c_send(s5s8_sgwc_fd, s5s8_tx_buf, payload_length,
(struct sockaddr *) &s5s8_pgwc_sockaddr,
s5s8_pgwc_sockaddr_len);
s5s8_sgwc_msgcnt++;
}
#ifndef ZMQ_COMM
if (spgw_cfg == SPGWC) {
payload_length = ntohs(gtpv2c_s11_tx->gtpc.length)
+ sizeof(gtpv2c_s11_tx->gtpc);
gtpv2c_send(s11_fd, s11_tx_buf, payload_length,
(struct sockaddr *) &s11_mme_sockaddr,
s11_mme_sockaddr_len);
}
#endif /* ZMQ_COMM */
break;
case GTP_MODIFY_BEARER_REQ:
ret = process_modify_bearer_request(
gtpv2c_s11_rx, gtpv2c_s11_tx);
if (ret) {
fprintf(stderr, "main.c::control_plane()::Error"
"\n\tcase SPGWC:"
"\n\tprocess_modify_bearer_request "
"%s: (%d) %s\n",
gtp_type_str(gtpv2c_s11_rx->gtpc.type), ret,
(ret < 0 ? strerror(-ret) : cause_str(ret)));
/* Error handling not implemented */
return;
}
#ifndef ZMQ_COMM
payload_length = ntohs(gtpv2c_s11_tx->gtpc.length)
+ sizeof(gtpv2c_s11_tx->gtpc);
gtpv2c_send(s11_fd, s11_tx_buf, payload_length,
(struct sockaddr *) &s11_mme_sockaddr,
s11_mme_sockaddr_len);
#endif /* ZMQ_COMM */
break;
case GTP_RELEASE_ACCESS_BEARERS_REQ:
ret = process_release_access_bearer_request(
gtpv2c_s11_rx, gtpv2c_s11_tx);