summaryrefstree
path: root/kernel/net/ncm/ncm.c
blob: 796ee5ec610fd08ac4455472c06ea4b60e3dc265 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
 *
 * Network Context Metadata Module[NCM]:Implementation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// KNOX NPA - START

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/sctp.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/time.h>
#include <linux/err.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter_ipv6.h>
#include <linux/errno.h>
#include <linux/device.h>
#include <linux/workqueue.h>
#include <linux/sched.h>
#include <linux/mutex.h>
#include <linux/kfifo.h>
#include <linux/kthread.h>
#include <linux/interrupt.h>
#include <linux/poll.h>
#include <linux/udp.h>
#include <linux/sctp.h>
#include <linux/slab.h>
#include <linux/pid.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/in.h>
#include <linux/in6.h>
#include <linux/net.h>
#include <linux/inet.h>

#include <net/sock.h>
#include <net/ncm.h>
#include <net/ip.h>
#include <net/protocol.h>

#include <asm/current.h>

#define SUCCESS 0

#define FAILURE 1
/* fifo size in elements (bytes) */
#define FIFO_SIZE   1024
#define WAIT_TIMEOUT  10000 /*milliseconds */
/* Lock to maintain orderly insertion of elements into kfifo */
static DEFINE_MUTEX(ncm_lock);

static unsigned int ncm_activated_flag = 1;

static unsigned int ncm_deactivated_flag; // default = 0

static unsigned int intermediate_activated_flag = 1;

static unsigned int intermediate_deactivated_flag; // default = 0

static unsigned int device_open_count; // default = 0

static int ncm_activated_type = NCM_FLOW_TYPE_DEFAULT;

static struct nf_hook_ops nfho_ipv4_pr_conntrack;

static struct nf_hook_ops nfho_ipv6_pr_conntrack;

static struct nf_hook_ops nfho_ipv4_li_conntrack;

static struct nf_hook_ops nfho_ipv6_li_conntrack;

static struct workqueue_struct *eWq; // default = 0

wait_queue_head_t ncm_wq;

static atomic_t isNCMEnabled = ATOMIC_INIT(0);

static atomic_t isIntermediateFlowEnabled = ATOMIC_INIT(0);

static unsigned int intermediate_flow_timeout; // default = 0

extern struct knox_socket_metadata knox_socket_metadata;

DECLARE_KFIFO(knox_sock_info, struct knox_socket_metadata, FIFO_SIZE);

/* The function is used to check if ncm feature has been enabled or not; The default value is disabled */
unsigned int check_ncm_flag(void) {
	return atomic_read(&isNCMEnabled);
}
EXPORT_SYMBOL(check_ncm_flag);

/* This function is used to check if ncm feature has been enabled with intermediate flow feature */
unsigned int check_intermediate_flag(void) {
	return atomic_read(&isIntermediateFlowEnabled);
}
EXPORT_SYMBOL(check_intermediate_flag);

/** The funcation is used to chedk if the kfifo is active or not;
 *  If the kfifo is active, then the socket metadata would be inserted into the queue which will be read by the user-space;
 *  By default the kfifo is inactive;
 */
bool kfifo_status(void) {
	bool isKfifoActive = false;
	if (kfifo_initialized(&knox_sock_info)) {
		NCM_LOGD("The fifo queue for ncm was already intialized \n");
		isKfifoActive = true;
	} else {
		NCM_LOGE("The fifo queue for ncm is not intialized \n");
		isKfifoActive = false;
	}
	return isKfifoActive;
}
EXPORT_SYMBOL(kfifo_status);

/** The function is used to insert the socket meta-data into the fifo queue; insertion of data will happen in a seperate kernel thread;
 *  The meta data information will be collected from the context of the process which originates it;
 *  If the kfifo is full, then the kfifo is freed before inserting new meta-data;
 */
void insert_data_kfifo(struct work_struct *pwork) {
	struct knox_socket_metadata *knox_socket_metadata;

	knox_socket_metadata = container_of(pwork, struct knox_socket_metadata, work_kfifo);
	if (IS_ERR(knox_socket_metadata)) {
		NCM_LOGE("inserting data into the kfifo failed due to unknown error \n");
		goto err;
	}

	if (mutex_lock_interruptible(&ncm_lock)) {
		NCM_LOGE("inserting data into the kfifo failed due to an interuppt \n");
		goto err;
	}

	if (kfifo_initialized(&knox_sock_info)) {
		if (kfifo_is_full(&knox_sock_info)) {
			NCM_LOGD("The kfifo is full and need to free it \n");
			kfree(knox_socket_metadata);
		} else {
			kfifo_in(&knox_sock_info, knox_socket_metadata, 1);
			kfree(knox_socket_metadata);
		}
	} else {
		kfree(knox_socket_metadata);
	}
	mutex_unlock(&ncm_lock);
	return;

	err:
	if (knox_socket_metadata != NULL)
		kfree(knox_socket_metadata);
	return;
}

/** The function is used to insert the socket meta-data into the kfifo in a seperate kernel thread;
 *  The kernel threads which handles the responsibility of inserting the meta-data into the kfifo is manintained by the workqueue function;
 */
void insert_data_kfifo_kthread(struct knox_socket_metadata* knox_socket_metadata) {
	if (knox_socket_metadata != NULL)
	{
		INIT_WORK(&(knox_socket_metadata->work_kfifo), insert_data_kfifo);
		if (!eWq) {
			NCM_LOGD("ewq ncmworkqueue not initialized. Data not collected\r\n");
			kfree(knox_socket_metadata);
		}
		if (eWq) {
			queue_work(eWq, &(knox_socket_metadata->work_kfifo));
		}
	}
}
EXPORT_SYMBOL(insert_data_kfifo_kthread);


/* The function is used to check if the caller is system server or not; */
static int is_system_server(void) {
	uid_t uid = current_uid().val;
	switch (uid) {
	case 1000:
		return 1;
	case 0:
		return 1;
	default:
		break;
	}
	return 0;
}

/* The function is used to intialize the kfifo */
static void initialize_kfifo(void) {
	INIT_KFIFO(knox_sock_info);
	if (kfifo_initialized(&knox_sock_info)) {
		NCM_LOGD("The kfifo for knox ncm has been initialized \n");
		init_waitqueue_head(&ncm_wq);
	}
}

/* The function is used to create work queue */
static void initialize_ncmworkqueue(void) {
	if (!eWq) {
		NCM_LOGD("ewq..Single Thread created\r\n");
		eWq = create_workqueue("ncmworkqueue");
	}
}

/* The function is ued to free the kfifo */
static void free_kfifo(void) {
	if (kfifo_status()) {
		NCM_LOGD("The kfifo for knox ncm which was intialized is freed \n");
		kfifo_free(&knox_sock_info);
	}
}

/* The function is used to update the flag indicating whether the feature has been enabled or not */
static void update_ncm_flag(unsigned int ncmFlag) {
	if (ncmFlag == ncm_activated_flag)
		atomic_set(&isNCMEnabled, ncm_activated_flag);
	else
		atomic_set(&isNCMEnabled, ncm_deactivated_flag);
}

/* The function is used to update the flag indicating whether the intermediate flow feature has been enabled or not */
static void update_intermediate_flag(unsigned int ncmIntermediateFlag) {
	if (ncmIntermediateFlag == intermediate_activated_flag)
		atomic_set(&isIntermediateFlowEnabled, intermediate_activated_flag);
	else
		atomic_set(&isIntermediateFlowEnabled, intermediate_deactivated_flag);
}

/* The function is used to update the flag indicating start or stop flow  */
static void update_ncm_flow_type(int ncmFlowType) {
	ncm_activated_type = ncmFlowType;
}

/* This function is used to update the intermediate flow timeout value */
static void update_intermediate_timeout(unsigned int timeout) {
	intermediate_flow_timeout = timeout;
}

/* This function is used to get the intermediate flow timeout value */
unsigned int get_intermediate_timeout(void) {
	return intermediate_flow_timeout; 
}
EXPORT_SYMBOL(get_intermediate_timeout);

/* IPv4 hook function to copy information from struct socket into struct nf_conn during first packet of the network flow */
static unsigned int hook_func_ipv4_out_conntrack(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) {

	struct iphdr *ip_header = NULL;
	struct tcphdr *tcp_header = NULL;
	struct udphdr *udp_header = NULL;
	struct nf_conn *ct = NULL;
	enum ip_conntrack_info ctinfo;
	struct nf_conntrack_tuple *tuple = NULL;
	char srcaddr[INET6_ADDRSTRLEN_NAP];
	char dstaddr[INET6_ADDRSTRLEN_NAP];

	if ( (skb) && (skb->sk) ) {
		if ( (skb->sk->knox_pid == INIT_PID_NAP) && (skb->sk->knox_uid == INIT_UID_NAP) && (skb->sk->sk_protocol == IPPROTO_TCP) ) {
			return NF_ACCEPT;
		}
		if ( (skb->sk->sk_protocol == IPPROTO_UDP) || (skb->sk->sk_protocol == IPPROTO_TCP) || (skb->sk->sk_protocol == IPPROTO_ICMP) || (skb->sk->sk_protocol == IPPROTO_SCTP) || (skb->sk->sk_protocol == IPPROTO_ICMPV6) ) {
			ct = nf_ct_get(skb, &ctinfo);
			if ( (ct) && (!atomic_read(&ct->startFlow)) && (!nf_ct_is_dying(ct)) ) {
				tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
				if (tuple) {
					sprintf(srcaddr,"%pI4",(void *)&tuple->src.u3.ip);
					sprintf(dstaddr,"%pI4",(void *)&tuple->dst.u3.ip);
					if ( isIpv4AddressEqualsNull(srcaddr, dstaddr) ) {
						return NF_ACCEPT;	
					}	
				} else {
					return NF_ACCEPT;
				}
				atomic_set(&ct->startFlow, 1);
				if ( check_intermediate_flag() ) {
					/* Use 'atomic_set(&ct->intermediateFlow, 1); ct->npa_timeout = ((u32)(jiffies)) + (get_intermediate_timeout() * HZ);' if struct nf_conn->timeout is of type u32; */
					ct->npa_timeout = ((u32)(jiffies)) + (get_intermediate_timeout() * HZ);
					atomic_set(&ct->intermediateFlow, 1);
					/* Use 'unsigned long timeout = ct->timeout.expires - jiffies;
							if ( (timeout > 0) && ((timeout/HZ) > 5) ) {
								atomic_set(&ct->intermediateFlow, 1);
								ct->npa_timeout.expires = (jiffies) + (get_intermediate_timeout() * HZ);
								add_timer(&ct->npa_timeout);
							}'
					if struct nf_conn->timeout is of type struct timer_list; */
				}
				ct->knox_uid = skb->sk->knox_uid;
				ct->knox_pid = skb->sk->knox_pid;
				memcpy(ct->process_name,skb->sk->process_name,sizeof(ct->process_name)-1);
				ct->knox_puid = skb->sk->knox_puid;
				ct->knox_ppid = skb->sk->knox_ppid;
				memcpy(ct->parent_process_name,skb->sk->parent_process_name,sizeof(ct->parent_process_name)-1);
				memcpy(ct->domain_name,skb->sk->domain_name,sizeof(ct->domain_name)-1);
				if ( (skb->dev) ) {
					memcpy(ct->interface_name,skb->dev->name,sizeof(ct->interface_name)-1);
				} else {
					sprintf(ct->interface_name,"%s","null");
				}
				ip_header = (struct iphdr *)skb_network_header(skb);
				if ( (ip_header) && (ip_header->protocol == IPPROTO_UDP) ) {
					udp_header = (struct udphdr *)skb_transport_header(skb);
					if (udp_header) {
						int udp_payload_size = (ntohs(udp_header->len)) - sizeof(struct udphdr);
						if ( (ct->knox_sent + udp_payload_size) > ULLONG_MAX )
							ct->knox_sent = ULLONG_MAX;
						else
							ct->knox_sent = ct->knox_sent + udp_payload_size;
						if ( (ntohs(udp_header->dest) ==  DNS_PORT_NAP) && (ct->knox_uid == INIT_UID_NAP) && (skb->sk->knox_dns_uid > INIT_UID_NAP) ) {
							ct->knox_puid = skb->sk->knox_dns_uid;
							ct->knox_ppid = skb->sk->knox_dns_pid;
							memcpy(ct->parent_process_name,skb->sk->dns_process_name,sizeof(ct->parent_process_name)-1);
						}
					}
				} else if ( (ip_header) && (ip_header->protocol == IPPROTO_TCP) ) {
					tcp_header = (struct tcphdr *)skb_transport_header(skb);
					if (tcp_header) {
						int tcp_payload_size = (ntohs(ip_header->tot_len)) - (ip_header->ihl * 4) - (tcp_header->doff * 4);
						if ( (ct->knox_sent + tcp_payload_size) > ULLONG_MAX )
							ct->knox_sent = ULLONG_MAX;
						else
							ct->knox_sent = ct->knox_sent + tcp_payload_size;
						if ( (ntohs(tcp_header->dest) ==  DNS_PORT_NAP) && (ct->knox_uid == INIT_UID_NAP) && (skb->sk->knox_dns_uid > INIT_UID_NAP) ) {
							ct->knox_puid = skb->sk->knox_dns_uid;
							ct->knox_ppid = skb->sk->knox_dns_pid;
							memcpy(ct->parent_process_name,skb->sk->dns_process_name,sizeof(ct->parent_process_name)-1);
						}
					}
				} else {
					ct->knox_sent = 0;
				}
				knox_collect_conntrack_data(ct, NCM_FLOW_TYPE_OPEN, 1);
			} else if ( (ct) && (!nf_ct_is_dying(ct)) ) {
				ip_header = (struct iphdr *)skb_network_header(skb);
				if ( (ip_header) && (ip_header->protocol == IPPROTO_UDP) ) {
					udp_header = (struct udphdr *)skb_transport_header(skb);
					if (udp_header) {
						int udp_payload_size = (ntohs(udp_header->len)) - sizeof(struct udphdr);
						if ( (ct->knox_sent + udp_payload_size) > ULLONG_MAX )
							ct->knox_sent = ULLONG_MAX;
						else
							ct->knox_sent = ct->knox_sent + udp_payload_size;
					}
				} else if ( (ip_header) && (ip_header->protocol == IPPROTO_TCP) ) {
					tcp_header = (struct tcphdr *)skb_transport_header(skb);
					if (tcp_header) {
						int tcp_payload_size = (ntohs(ip_header->tot_len)) - (ip_header->ihl * 4) - (tcp_header->doff * 4);
						if ( (ct->knox_sent + tcp_payload_size) > ULLONG_MAX )
							ct->knox_sent = ULLONG_MAX;
						else
							ct->knox_sent = ct->knox_sent + tcp_payload_size;
					}
				} else {
					ct->knox_sent = 0;
				}
			}
		}
	}

	return NF_ACCEPT;
}

/* IPv6 hook function to copy information from struct socket into struct nf_conn during first packet of the network flow */
static unsigned int hook_func_ipv6_out_conntrack(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) {
	struct ipv6hdr *ipv6_header = NULL;
	struct tcphdr *tcp_header = NULL;
	struct udphdr *udp_header = NULL;
	struct nf_conn *ct = NULL;
	enum ip_conntrack_info ctinfo;
	struct nf_conntrack_tuple *tuple = NULL;
	char srcaddr[INET6_ADDRSTRLEN_NAP];
	char dstaddr[INET6_ADDRSTRLEN_NAP];

	if ( (skb) && (skb->sk) ) {
		if ( (skb->sk->knox_pid == INIT_PID_NAP) && (skb->sk->knox_uid == INIT_UID_NAP) && (skb->sk->sk_protocol == IPPROTO_TCP) ) {
			return NF_ACCEPT;
		}
		if ( (skb->sk->sk_protocol == IPPROTO_UDP) || (skb->sk->sk_protocol == IPPROTO_TCP) || (skb->sk->sk_protocol == IPPROTO_ICMP) || (skb->sk->sk_protocol == IPPROTO_SCTP) || (skb->sk->sk_protocol == IPPROTO_ICMPV6) ) {
			ct = nf_ct_get(skb, &ctinfo);
			if ( (ct) && (!atomic_read(&ct->startFlow)) && (!nf_ct_is_dying(ct)) ) {
				tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
				if (tuple) {
					sprintf(srcaddr,"%pI6",(void *)&tuple->src.u3.ip6);
					sprintf(dstaddr,"%pI6",(void *)&tuple->dst.u3.ip6);
					if ( isIpv6AddressEqualsNull(srcaddr, dstaddr) ) {
						return NF_ACCEPT;	
					}	
				} else {
					return NF_ACCEPT;
				}
				atomic_set(&ct->startFlow, 1);
				if ( check_intermediate_flag() ) {
					/* Use 'atomic_set(&ct->intermediateFlow, 1); ct->npa_timeout = ((u32)(jiffies)) + (get_intermediate_timeout() * HZ);' if struct nf_conn->timeout is of type u32; */
					ct->npa_timeout = ((u32)(jiffies)) + (get_intermediate_timeout() * HZ);
					atomic_set(&ct->intermediateFlow, 1);
					/* Use 'unsigned long timeout = ct->timeout.expires - jiffies;
							if ( (timeout > 0) && ((timeout/HZ) > 5) ) {
								atomic_set(&ct->intermediateFlow, 1);
								ct->npa_timeout.expires = (jiffies) + (get_intermediate_timeout() * HZ);
								add_timer(&ct->npa_timeout);
							}'
					if struct nf_conn->timeout is of type struct timer_list; */
				}
				ct->knox_uid = skb->sk->knox_uid;
				ct->knox_pid = skb->sk->knox_pid;
				memcpy(ct->process_name,skb->sk->process_name,sizeof(ct->process_name)-1);
				ct->knox_puid = skb->sk->knox_puid;
				ct->knox_ppid = skb->sk->knox_ppid;
				memcpy(ct->parent_process_name,skb->sk->parent_process_name,sizeof(ct->parent_process_name)-1);
				memcpy(ct->domain_name,skb->sk->domain_name,sizeof(ct->domain_name)-1);
				if ( (skb->dev) ) {
					memcpy(ct->interface_name,skb->dev->name,sizeof(ct->interface_name)-1);
				} else {
					sprintf(ct->interface_name,"%s","null");
				}
				ipv6_header = (struct ipv6hdr *)skb_network_header(skb);
				if ( (ipv6_header) && (ipv6_header->nexthdr == IPPROTO_UDP) ) {
					udp_header = (struct udphdr *)skb_transport_header(skb);
					if (udp_header) {
						int udp_payload_size = (ntohs(udp_header->len)) - sizeof(struct udphdr);
						if ( (ct->knox_sent + udp_payload_size) > ULLONG_MAX )
							ct->knox_sent = ULLONG_MAX;
						else
							ct->knox_sent = ct->knox_sent + udp_payload_size;
						if ( (ntohs(udp_header->dest) ==  DNS_PORT_NAP) && (ct->knox_uid == INIT_UID_NAP) && (skb->sk->knox_dns_uid > INIT_UID_NAP) ) {
							ct->knox_puid = skb->sk->knox_dns_uid;
							ct->knox_ppid = skb->sk->knox_dns_pid;
							memcpy(ct->parent_process_name,skb->sk->dns_process_name,sizeof(ct->parent_process_name)-1);
						}
					}
				} else if ( (ipv6_header) && (ipv6_header->nexthdr == IPPROTO_TCP) ) {
					tcp_header = (struct tcphdr *)skb_transport_header(skb);
					if (tcp_header) {
						int tcp_payload_size = (ntohs(ipv6_header->payload_len)) - (tcp_header->doff * 4);
						if ( (ct->knox_sent + tcp_payload_size) > ULLONG_MAX )
							ct->knox_sent = ULLONG_MAX;
						else
							ct->knox_sent = ct->knox_sent + tcp_payload_size;
						if ( (ntohs(tcp_header->dest) ==  DNS_PORT_NAP) && (ct->knox_uid == INIT_UID_NAP) && (skb->sk->knox_dns_uid > INIT_UID_NAP) ) {
							ct->knox_puid = skb->sk->knox_dns_uid;
							ct->knox_ppid = skb->sk->knox_dns_pid;
							memcpy(ct->parent_process_name,skb->sk->dns_process_name,sizeof(ct->parent_process_name)-1);
						}
					}
				} else {
					ct->knox_sent = 0;
				}
				knox_collect_conntrack_data(ct, NCM_FLOW_TYPE_OPEN, 2);
			} else if ( (ct) && (!nf_ct_is_dying(ct)) ) {
				ipv6_header = (struct ipv6hdr *)skb_network_header(skb);
				if ( (ipv6_header) && (ipv6_header->nexthdr == IPPROTO_UDP) ) {
					udp_header = (struct udphdr *)skb_transport_header(skb);
					if (udp_header) {
						int udp_payload_size = (ntohs(udp_header->len)) - sizeof(struct udphdr);
						if ( (ct->knox_sent + udp_payload_size) > ULLONG_MAX )
							ct->knox_sent = ULLONG_MAX;
						else
							ct->knox_sent = ct->knox_sent + udp_payload_size;
					}
				} else if ( (ipv6_header) && (ipv6_header->nexthdr == IPPROTO_TCP) ) {
					tcp_header = (struct tcphdr *)skb_transport_header(skb);
					if (tcp_header) {
						int tcp_payload_size = (ntohs(ipv6_header->payload_len)) - (tcp_header->doff * 4);
						if ( (ct->knox_sent + tcp_payload_size) > ULLONG_MAX )
							ct->knox_sent = ULLONG_MAX;
						else
							ct->knox_sent = ct->knox_sent + tcp_payload_size;
					}
				} else {
					ct->knox_sent = 0;
				}
			}
		}
	}

	return NF_ACCEPT;
}

static unsigned int hook_func_ipv4_in_conntrack(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) {
	struct iphdr *ip_header = NULL;
	struct tcphdr *tcp_header = NULL;
	struct udphdr *udp_header = NULL;
	struct nf_conn *ct = NULL;
	enum ip_conntrack_info ctinfo;

	if (skb){
		ip_header = (struct iphdr *)skb_network_header(skb);
		if ( (ip_header) && (ip_header->protocol == IPPROTO_TCP || ip_header->protocol == IPPROTO_UDP || ip_header->protocol == IPPROTO_SCTP || ip_header->protocol == IPPROTO_ICMP || ip_header->protocol == IPPROTO_ICMPV6) ) {		
			ct = nf_ct_get(skb, &ctinfo);
			if ( (ct) && (!nf_ct_is_dying(ct)) ) {
				if (ip_header->protocol == IPPROTO_TCP) {
					tcp_header = (struct tcphdr *)skb_transport_header(skb);
					if (tcp_header) {
						int tcp_payload_size = (ntohs(ip_header->tot_len)) - (ip_header->ihl * 4) - (tcp_header->doff * 4);
						if ( (ct->knox_recv + tcp_payload_size) > ULLONG_MAX )
							ct->knox_recv = ULLONG_MAX;
						else
							ct->knox_recv = ct->knox_recv + tcp_payload_size;
					}
				} else if (ip_header->protocol == IPPROTO_UDP) {
					udp_header = (struct udphdr *)skb_transport_header(skb);
					if (udp_header) {
						int udp_payload_size = (ntohs(udp_header->len)) - sizeof(struct udphdr);
						if ( (ct->knox_recv + udp_payload_size) > ULLONG_MAX )
							ct->knox_recv = ULLONG_MAX;
						else
							ct->knox_recv = ct->knox_recv + udp_payload_size;
					}
				} else {
					ct->knox_recv = 0;
				}
			}
		}
	}

	return NF_ACCEPT;
}

static unsigned int hook_func_ipv6_in_conntrack(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) {
	struct ipv6hdr *ipv6_header = NULL;
	struct tcphdr *tcp_header = NULL;
	struct udphdr *udp_header = NULL;
	struct nf_conn *ct = NULL;
	enum ip_conntrack_info ctinfo;

	if (skb){
		ipv6_header = (struct ipv6hdr *)skb_network_header(skb);
		if ( (ipv6_header) && (ipv6_header->nexthdr == IPPROTO_TCP || ipv6_header->nexthdr == IPPROTO_UDP || ipv6_header->nexthdr == IPPROTO_SCTP || ipv6_header->nexthdr == IPPROTO_ICMP || ipv6_header->nexthdr == IPPROTO_ICMPV6) ) {
			ct = nf_ct_get(skb, &ctinfo);
			if ( (ct) && (!nf_ct_is_dying(ct)) ) {
				if (ipv6_header->nexthdr == IPPROTO_TCP) {
					tcp_header = (struct tcphdr *)skb_transport_header(skb);
					if (tcp_header) {
						int tcp_payload_size = (ntohs(ipv6_header->payload_len)) - (tcp_header->doff * 4);
						if ( (ct->knox_recv + tcp_payload_size) > ULLONG_MAX )
							ct->knox_recv = ULLONG_MAX;
						else
							ct->knox_recv = ct->knox_recv + tcp_payload_size;
					}
				} else if (ipv6_header->nexthdr == IPPROTO_UDP) {
					udp_header = (struct udphdr *)skb_transport_header(skb);
					if (udp_header) {
						int udp_payload_size = (ntohs(udp_header->len)) - sizeof(struct udphdr);
						if ( (ct->knox_recv + udp_payload_size) > ULLONG_MAX )
							ct->knox_recv = ULLONG_MAX;
						else
							ct->knox_recv = ct->knox_recv + udp_payload_size;
					}
				} else {
					ct->knox_recv = 0;
				}
			}
		}
	}

	return NF_ACCEPT;
}

/* The fuction registers to listen for packets in the post-routing chain to collect detail; */
static void registerNetfilterHooks(void) {
	nfho_ipv4_pr_conntrack.hook = hook_func_ipv4_out_conntrack;
	nfho_ipv4_pr_conntrack.hooknum = NF_INET_POST_ROUTING;
	nfho_ipv4_pr_conntrack.pf = PF_INET;
	nfho_ipv4_pr_conntrack.priority = NF_IP_PRI_LAST;

	nfho_ipv6_pr_conntrack.hook = hook_func_ipv6_out_conntrack;
	nfho_ipv6_pr_conntrack.hooknum = NF_INET_POST_ROUTING;
	nfho_ipv6_pr_conntrack.pf = PF_INET6;
	nfho_ipv6_pr_conntrack.priority = NF_IP6_PRI_LAST;

	nfho_ipv4_li_conntrack.hook = hook_func_ipv4_in_conntrack;
	nfho_ipv4_li_conntrack.hooknum = NF_INET_LOCAL_IN;
	nfho_ipv4_li_conntrack.pf = PF_INET;
	nfho_ipv4_li_conntrack.priority = NF_IP_PRI_LAST;

	nfho_ipv6_li_conntrack.hook = hook_func_ipv6_in_conntrack;
	nfho_ipv6_li_conntrack.hooknum = NF_INET_LOCAL_IN;
	nfho_ipv6_li_conntrack.pf = PF_INET6;
	nfho_ipv6_li_conntrack.priority = NF_IP6_PRI_LAST;

	/* For kernel versin below 4.13
	nf_register_hook(&nfho_ipv4_pr_conntrack); nf_register_hook(&nfho_ipv6_pr_conntrack); nf_register_hook(&nfho_ipv4_li_conntrack); nf_register_hook(&nfho_ipv6_li_conntrack); */

	/* For kernel version above 4.13 */
	nf_register_net_hook(&init_net,&nfho_ipv4_pr_conntrack);
	nf_register_net_hook(&init_net,&nfho_ipv6_pr_conntrack);
	nf_register_net_hook(&init_net,&nfho_ipv4_li_conntrack);
	nf_register_net_hook(&init_net,&nfho_ipv6_li_conntrack);
}

/* The function un-registers the netfilter hook */
static void unregisterNetFilterHooks(void) {
	/* For kernel version below 4.13
	nf_unregister_hook(&nfho_ipv4_pr_conntrack); nf_unregister_hook(&nfho_ipv6_pr_conntrack); nf_unregister_hook(&nfho_ipv4_li_conntrack); nf_unregister_hook(&nfho_ipv6_li_conntrack); */

	/* For kernel version above 4.13 */
	nf_unregister_net_hook(&init_net,&nfho_ipv4_pr_conntrack);
	nf_unregister_net_hook(&init_net,&nfho_ipv6_pr_conntrack);
	nf_unregister_net_hook(&init_net,&nfho_ipv4_li_conntrack);
	nf_unregister_net_hook(&init_net,&nfho_ipv6_li_conntrack);
}

/* Function to collect the conntrack meta-data information. This function is called from ncm.c during the flows first send data and nf_conntrack_core.c when flow is removed. */
void knox_collect_conntrack_data(struct nf_conn *ct, int startStop, int where) {
	if ( check_ncm_flag() && (ncm_activated_type == startStop || ncm_activated_type == NCM_FLOW_TYPE_ALL) ) {
		struct knox_socket_metadata *ksm = kzalloc(sizeof(struct knox_socket_metadata), GFP_ATOMIC);
		struct nf_conntrack_tuple *tuple = NULL;
		struct timespec close_timespec;

		if (ksm == NULL) {
			printk("kzalloc atomic memory allocation failed\n");
			return;
		}

		ksm->knox_uid = ct->knox_uid;
		ksm->knox_pid = ct->knox_pid;
		memcpy(ksm->process_name, ct->process_name, sizeof(ksm->process_name)-1);
		ksm->trans_proto = nf_ct_protonum(ct);
		tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
		if (tuple != NULL) {
			if (nf_ct_l3num(ct) == IPV4_FAMILY_NAP) {
				sprintf(ksm->srcaddr,"%pI4",(void *)&tuple->src.u3.ip);
				sprintf(ksm->dstaddr,"%pI4",(void *)&tuple->dst.u3.ip);
			} else if (nf_ct_l3num(ct) == IPV6_FAMILY_NAP) {
				sprintf(ksm->srcaddr,"%pI6",(void *)&tuple->src.u3.ip6);
				sprintf(ksm->dstaddr,"%pI6",(void *)&tuple->dst.u3.ip6);
			}
			if (nf_ct_protonum(ct) == IPPROTO_UDP) {
				ksm->srcport = ntohs(tuple->src.u.udp.port);
				ksm->dstport = ntohs(tuple->dst.u.udp.port);
			} else if (nf_ct_protonum(ct) == IPPROTO_TCP) {
				ksm->srcport = ntohs(tuple->src.u.tcp.port);
				ksm->dstport = ntohs(tuple->dst.u.tcp.port);
			} else if (nf_ct_protonum(ct) == IPPROTO_SCTP) {
				ksm->srcport = ntohs(tuple->src.u.sctp.port);
				ksm->dstport = ntohs(tuple->dst.u.sctp.port);
			} else {
				ksm->srcport = 0;
				ksm->dstport = 0;
			}
		}
		memcpy(ksm->domain_name, ct->domain_name, sizeof(ksm->domain_name)-1);
		ksm->open_time = ct->open_time;
		if (startStop == NCM_FLOW_TYPE_OPEN) {
			ksm->close_time = 0;
		} else if (startStop == NCM_FLOW_TYPE_CLOSE) {
			close_timespec = current_kernel_time();
			ksm->close_time = close_timespec.tv_sec;
		} else if (startStop == NCM_FLOW_TYPE_INTERMEDIATE) {
			close_timespec = current_kernel_time();
			ksm->close_time = close_timespec.tv_sec;
		}
		ksm->knox_puid = ct->knox_puid;
		ksm->knox_ppid = ct->knox_ppid;
		memcpy(ksm->parent_process_name, ct->parent_process_name, sizeof(ksm->parent_process_name)-1);
		if ( (nf_ct_protonum(ct) == IPPROTO_UDP) || (nf_ct_protonum(ct) == IPPROTO_TCP) || (nf_ct_protonum(ct) == IPPROTO_SCTP) ) {
			ksm->knox_sent = ct->knox_sent;
			ksm->knox_recv = ct->knox_recv;
		} else {
			ksm->knox_sent = 0;
			ksm->knox_recv = 0;
		}
		if (ksm->dstport == DNS_PORT_NAP && ksm->knox_uid > INIT_UID_NAP) {
			ksm->knox_uid_dns = ksm->knox_uid;
		} else {
			ksm->knox_uid_dns = ksm->knox_puid;
		}
		memcpy(ksm->interface_name, ct->interface_name, sizeof(ksm->interface_name)-1);
		if (startStop == NCM_FLOW_TYPE_OPEN) {
			ksm->flow_type = 1;
		} else if (startStop == NCM_FLOW_TYPE_CLOSE) {
			ksm->flow_type = 2;
		} else if (startStop == NCM_FLOW_TYPE_INTERMEDIATE) {
			ksm->flow_type = 3;
		} else {
			ksm->flow_type = 0;
		}

		insert_data_kfifo_kthread(ksm);
	}
}
EXPORT_SYMBOL(knox_collect_conntrack_data);

/* The function opens the char device through which the userspace reads the socket meta-data information */
static int ncm_open(struct inode *inode, struct file *file) {
	NCM_LOGD("ncm_open is being called. \n");

	if ( !(IS_ENABLED(CONFIG_NF_CONNTRACK)) ) {
		NCM_LOGE("ncm_open failed:Trying to open in device conntrack module is not enabled \n");
		return -EACCES;
	}

	if (!is_system_server()) {
		NCM_LOGE("ncm_open failed:Caller is a non system process with uid %u \n", (current_uid().val));
		return -EACCES;
	}

	if (device_open_count) {
		NCM_LOGE("ncm_open failed:The device is already in open state \n");
		return -EBUSY;
	}

	device_open_count++;

	try_module_get(THIS_MODULE);

	return SUCCESS;
}

#ifdef CONFIG_64BIT
static ssize_t ncm_copy_data_user_64(char __user *buf, size_t count)
{
	struct knox_socket_metadata kcm = {0};
	struct knox_user_socket_metadata user_copy = {0};

	unsigned long copied;
	int read = 0;

	if (mutex_lock_interruptible(&ncm_lock)) {
		NCM_LOGE("ncm_copy_data_user failed:Signal interuption \n");
		return 0;
	}
	read = kfifo_out(&knox_sock_info, &kcm, 1);
	mutex_unlock(&ncm_lock);
	if (read == 0) {
		return 0;
	}

	user_copy.srcport = kcm.srcport;
	user_copy.dstport = kcm.dstport;
	user_copy.trans_proto = kcm.trans_proto;
	user_copy.knox_sent = kcm.knox_sent;
	user_copy.knox_recv = kcm.knox_recv;
	user_copy.knox_uid = kcm.knox_uid;
	user_copy.knox_pid = kcm.knox_pid;
	user_copy.knox_puid = kcm.knox_puid;
	user_copy.open_time = kcm.open_time;
	user_copy.close_time = kcm.close_time;
	user_copy.knox_uid_dns = kcm.knox_uid_dns;
	user_copy.knox_ppid = kcm.knox_ppid;
	user_copy.flow_type = kcm.flow_type;

	memcpy(user_copy.srcaddr, kcm.srcaddr, sizeof(user_copy.srcaddr));
	memcpy(user_copy.dstaddr, kcm.dstaddr, sizeof(user_copy.dstaddr));

	memcpy(user_copy.process_name, kcm.process_name, sizeof(user_copy.process_name));
	memcpy(user_copy.parent_process_name, kcm.parent_process_name, sizeof(user_copy.parent_process_name));

	memcpy(user_copy.domain_name, kcm.domain_name, sizeof(user_copy.domain_name)-1);

	memcpy(user_copy.interface_name, kcm.interface_name, sizeof(user_copy.interface_name)-1);

	copied = copy_to_user(buf, &user_copy, sizeof(struct knox_user_socket_metadata));
	return count;
}
#else
static ssize_t ncm_copy_data_user(char __user *buf, size_t count)
{
	struct knox_socket_metadata *kcm = NULL;
	struct knox_user_socket_metadata user_copy = {0};

	unsigned long copied;
	int read = 0;

	if (mutex_lock_interruptible(&ncm_lock)) {
		NCM_LOGE("ncm_copy_data_user failed:Signal interuption \n");
		return 0;
	}

	kcm = kzalloc(sizeof (struct knox_socket_metadata), GFP_KERNEL);
	if (kcm == NULL) {
		mutex_unlock(&ncm_lock);
		return 0;
	}

	read = kfifo_out(&knox_sock_info, kcm, 1);
	mutex_unlock(&ncm_lock);
	if (read == 0) {
		kfree(kcm);
		return 0;
	}

	user_copy.srcport = kcm->srcport;
	user_copy.dstport = kcm->dstport;
	user_copy.trans_proto = kcm->trans_proto;
	user_copy.knox_sent = kcm->knox_sent;
	user_copy.knox_recv = kcm->knox_recv;
	user_copy.knox_uid = kcm->knox_uid;
	user_copy.knox_pid = kcm->knox_pid;
	user_copy.knox_puid = kcm->knox_puid;
	user_copy.open_time = kcm->open_time;
	user_copy.close_time = kcm->close_time;
	user_copy.knox_uid_dns = kcm->knox_uid_dns;
	user_copy.knox_ppid = kcm->knox_ppid;
	user_copy.flow_type = kcm->flow_type;

	memcpy(user_copy.srcaddr, kcm->srcaddr, sizeof(user_copy.srcaddr));
	memcpy(user_copy.dstaddr, kcm->dstaddr, sizeof(user_copy.dstaddr));

	memcpy(user_copy.process_name, kcm->process_name, sizeof(user_copy.process_name));
	memcpy(user_copy.parent_process_name, kcm->parent_process_name, sizeof(user_copy.parent_process_name));

	memcpy(user_copy.domain_name, kcm->domain_name, sizeof(user_copy.domain_name)-1);

	memcpy(user_copy.interface_name, kcm->interface_name, sizeof(user_copy.interface_name)-1);

	copied = copy_to_user(buf, &user_copy, sizeof(struct knox_user_socket_metadata));

	kfree(kcm);

	return count;
}
#endif

/* The function writes the socket meta-data to the user-space */
static ssize_t ncm_read(struct file *file, char __user *buf, size_t count, loff_t *off) {
	if (!is_system_server()) {
		NCM_LOGE("ncm_read failed:Caller is a non system process with uid %u \n", (current_uid().val));
		return -EACCES;
	}

	if (!eWq) {
		NCM_LOGD("ewq..Single Thread created\r\n");
		eWq = create_workqueue("ncmworkqueue");
	}

	#ifdef CONFIG_64BIT
	return ncm_copy_data_user_64(buf, count);
	#else
	return ncm_copy_data_user(buf, count);
	#endif

	return 0;
}

static ssize_t ncm_write(struct file *file, const char __user *buf, size_t count, loff_t *off) {
	char intermediate_string[6];
	int intermediate_value = 0;
	if (!is_system_server()) {
		NCM_LOGE("ncm_write failed:Caller is a non system process with uid %u \n", (current_uid().val));
		return -EACCES;
	}
	memset(intermediate_string,'\0',sizeof(intermediate_string));
	copy_from_user(intermediate_string,buf,sizeof(intermediate_string)-1);
	intermediate_value = simple_strtol(intermediate_string, NULL, 10);
	if (intermediate_value > 0) {
		update_intermediate_timeout(intermediate_value);
		update_intermediate_flag(intermediate_activated_flag);
		return strlen(intermediate_string);
	}
	return intermediate_value;
}

/* The function closes the char device */
static int ncm_close(struct inode *inode, struct file *file) {
	NCM_LOGD("ncm_close is being called \n");
	if (!is_system_server()) {
		NCM_LOGE("ncm_close failed:Caller is a non system process with uid %u \n", (current_uid().val));
		return -EACCES;
	}
	device_open_count--;
	module_put(THIS_MODULE);
	if (!check_ncm_flag())  {
		NCM_LOGD("ncm_close success: The device was already in closed state \n");
		return SUCCESS;
	}
	update_ncm_flag(ncm_deactivated_flag);
	free_kfifo();
	unregisterNetFilterHooks();
	return SUCCESS;
}

/* The function sets the flag which indicates whether the ncm feature needs to be enabled or disabled */
static long ncm_ioctl_evt(struct file *file, unsigned int cmd, unsigned long arg) {
	if (!is_system_server()) {
		NCM_LOGE("ncm_ioctl_evt failed:Caller is a non system process with uid %u \n", (current_uid().val));
		return -EACCES;
	}
	switch (cmd) {
	case NCM_ACTIVATED_ALL: {
		NCM_LOGD("ncm_ioctl_evt is being NCM_ACTIVATED with the ioctl command %u \n", cmd);
		if (check_ncm_flag())
			return SUCCESS;
		registerNetfilterHooks();
		initialize_kfifo();
		initialize_ncmworkqueue();
		update_ncm_flag(ncm_activated_flag);
		update_ncm_flow_type(NCM_FLOW_TYPE_ALL);
		break;
	}
	case NCM_ACTIVATED_OPEN: {
		NCM_LOGD("ncm_ioctl_evt is being NCM_ACTIVATED with the ioctl command %u \n", cmd);
		if (check_ncm_flag())
			return SUCCESS;
		update_intermediate_timeout(0);
		update_intermediate_flag(intermediate_deactivated_flag);
		registerNetfilterHooks();
		initialize_kfifo();
		initialize_ncmworkqueue();
		update_ncm_flag(ncm_activated_flag);
		update_ncm_flow_type(NCM_FLOW_TYPE_OPEN);
		break;
	}
	case NCM_ACTIVATED_CLOSE: {
		NCM_LOGD("ncm_ioctl_evt is being NCM_ACTIVATED with the ioctl command %u \n", cmd);
		if (check_ncm_flag())
			return SUCCESS;
		update_intermediate_timeout(0);
		update_intermediate_flag(intermediate_deactivated_flag);
		registerNetfilterHooks();
		initialize_kfifo();
		initialize_ncmworkqueue();
		update_ncm_flag(ncm_activated_flag);
		update_ncm_flow_type(NCM_FLOW_TYPE_CLOSE);
		break;
	}
	case NCM_DEACTIVATED: {
		NCM_LOGD("ncm_ioctl_evt is being NCM_DEACTIVATED with the ioctl command %u \n", cmd);
		if (!check_ncm_flag())
			return SUCCESS;
		update_intermediate_flag(intermediate_deactivated_flag);
		update_ncm_flow_type(NCM_FLOW_TYPE_DEFAULT);
		update_ncm_flag(ncm_deactivated_flag);
		free_kfifo();
		unregisterNetFilterHooks();
		update_intermediate_timeout(0);
		break;
	}
	case NCM_GETVERSION: {
		NCM_LOGD("ncm_ioctl_evt is being NCM_GETVERSION with the ioctl command %u \n", cmd);
		return NCM_VERSION;
		break;
	}
	case NCM_MATCH_VERSION: {
		NCM_LOGD("ncm_ioctl_evt is being NCM_MATCH_VERSION with the ioctl command %u \n", cmd);
		return sizeof(struct knox_user_socket_metadata);
		break;
	}
	default:
		break;
	}
	return SUCCESS;
}

static unsigned int ncm_poll(struct file *file, poll_table *pt) {
	int mask = 0;
	int ret = 0;
	if (kfifo_is_empty(&knox_sock_info)) {
		ret = wait_event_interruptible_timeout(ncm_wq, !kfifo_is_empty(&knox_sock_info), msecs_to_jiffies(WAIT_TIMEOUT));
		switch (ret) {
		case -ERESTARTSYS:
			mask = -EINTR;
			break;
		case 0:
			mask = 0;
			break;
		case 1:
			mask |= POLLIN | POLLRDNORM;
			break;
		default:
			mask |= POLLIN | POLLRDNORM;
			break;
		}
		return mask;
	} else {
		mask |= POLLIN | POLLRDNORM;
	}
	return mask;
}

static const struct file_operations ncm_fops = {
	.owner          = THIS_MODULE,
	.open           = ncm_open,
	.read           = ncm_read,
	.write          = ncm_write,
	.release        = ncm_close,
	.unlocked_ioctl = ncm_ioctl_evt,
	.compat_ioctl   = ncm_ioctl_evt,
	.poll           = ncm_poll,
};

struct miscdevice ncm_misc_device = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "ncm_dev",
	.fops = &ncm_fops,
};

static int __init ncm_init(void) {
	int ret;
	ret = misc_register(&ncm_misc_device);
	if (unlikely(ret)) {
		NCM_LOGE("failed to register ncm misc device!\n");
		return ret;
	}
	NCM_LOGD("Network Context Metadata Module: initialized\n");
	return SUCCESS;
}

static void __exit ncm_exit(void) {
	misc_deregister(&ncm_misc_device);
	NCM_LOGD("Network Context Metadata Module: unloaded\n");
}

module_init(ncm_init)
module_exit(ncm_exit)

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Network Context Metadata Module:");

// KNOX NPA - END