ncm.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2016 Samsung Electronics Co., Ltd.
  3. *
  4. * Network Context Metadata Module[NCM]:Implementation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA {
  19. #ifndef NCM_COMMON_H__
  20. #define NCM_COMMON_H__
  21. #define NCM_VERSION 11
  22. #define INIT_UID_NAP 0
  23. #define INIT_PID_NAP 1
  24. #define DNS_PORT_NAP 53
  25. #define IPV4_FAMILY_NAP 2
  26. #define IPV6_FAMILY_NAP 10
  27. #define INET6_ADDRSTRLEN_NAP 48
  28. #define NCM_FLOW_TYPE_DEFAULT -1
  29. #define NCM_FLOW_TYPE_ALL 0
  30. #define NCM_FLOW_TYPE_OPEN 1
  31. #define NCM_FLOW_TYPE_CLOSE 2
  32. #define NCM_FLOW_TYPE_INTERMEDIATE 3
  33. #include <linux/kernel.h>
  34. #include <linux/inet.h>
  35. #include <linux/sched.h>
  36. #include <linux/string.h>
  37. #include <net/netfilter/nf_conntrack.h>
  38. #define isIpv4AddressEqualsNull(srcaddr, dstaddr) ((((strcmp(srcaddr, "0.0.0.0")) || (strcmp(dstaddr, "0.0.0.0"))) == 0) ? 1 : 0)
  39. #define isIpv6AddressEqualsNull(srcaddr, dstaddr) ((((strcmp(srcaddr, "0000:0000:0000:0000:0000:0000:0000:0000")) || (strcmp(dstaddr, "0000:0000:0000:0000:0000:0000:0000:0000"))) == 0) ? 1 : 0)
  40. /* Struct Socket definition */
  41. struct knox_socket_metadata {
  42. /* The source port of the socket */
  43. __u16 srcport;
  44. /* The destination port of the socket */
  45. __u16 dstport;
  46. /* The Transport layer protocol of the socket*/
  47. __u16 trans_proto;
  48. /* The number of application layer bytes sent by the socket */
  49. __u64 knox_sent;
  50. /* The number of application layer bytes recieved by the socket */
  51. __u64 knox_recv;
  52. /* The uid which created the socket */
  53. uid_t knox_uid;
  54. /* The pid under which the socket was created */
  55. pid_t knox_pid;
  56. /* The parent user id under which the socket was created */
  57. uid_t knox_puid;
  58. /* The epoch time at which the socket was opened */
  59. __u64 open_time;
  60. /* The epoch time at which the socket was closed */
  61. __u64 close_time;
  62. /* The source address of the socket */
  63. char srcaddr[INET6_ADDRSTRLEN_NAP];
  64. /* The destination address of the socket */
  65. char dstaddr[INET6_ADDRSTRLEN_NAP];
  66. /* The name of the process which created the socket */
  67. char process_name[PROCESS_NAME_LEN_NAP];
  68. /* The name of the parent process which created the socket */
  69. char parent_process_name[PROCESS_NAME_LEN_NAP];
  70. /* The Domain name associated with the ip address of the socket. The size needs to be in sync with the userspace implementation */
  71. char domain_name[DOMAIN_NAME_LEN_NAP];
  72. /* The uid which originated the dns request */
  73. uid_t knox_uid_dns;
  74. /* The parent process id under which the socket was created */
  75. pid_t knox_ppid;
  76. /* The interface used by the flow to transmit packet */
  77. char interface_name[IFNAMSIZ];
  78. /* The flow type is used identify the current state of the network flow*/
  79. int flow_type;
  80. /* The struct defined is responsible for inserting the socket meta-data into kfifo */
  81. struct work_struct work_kfifo;
  82. };
  83. /* Struct Socket definition */
  84. struct knox_user_socket_metadata {
  85. /* The source port of the socket */
  86. __u16 srcport;
  87. /* The destination port of the socket */
  88. __u16 dstport;
  89. /* The Transport layer protocol of the socket*/
  90. __u16 trans_proto;
  91. /* The number of application layer bytes sent by the socket */
  92. __u64 knox_sent;
  93. /* The number of application layer bytes recieved by the socket */
  94. __u64 knox_recv;
  95. /* The uid which created the socket */
  96. uid_t knox_uid;
  97. /* The pid under which the socket was created */
  98. pid_t knox_pid;
  99. /* The parent user id under which the socket was created */
  100. uid_t knox_puid;
  101. /* The epoch time at which the socket was opened */
  102. __u64 open_time;
  103. /* The epoch time at which the socket was closed */
  104. __u64 close_time;
  105. /* The source address of the socket */
  106. char srcaddr[INET6_ADDRSTRLEN_NAP];
  107. /* The destination address of the socket */
  108. char dstaddr[INET6_ADDRSTRLEN_NAP];
  109. /* The name of the process which created the socket */
  110. char process_name[PROCESS_NAME_LEN_NAP];
  111. /* The name of the parent process which created the socket */
  112. char parent_process_name[PROCESS_NAME_LEN_NAP];
  113. /* The Domain name associated with the ip address of the socket. The size needs to be in sync with the userspace implementation */
  114. char domain_name[DOMAIN_NAME_LEN_NAP];
  115. /* The uid which originated the dns request */
  116. uid_t knox_uid_dns;
  117. /* The parent process id under which the socket was created */
  118. pid_t knox_ppid;
  119. /* The interface used by the flow to transmit packet */
  120. char interface_name[IFNAMSIZ];
  121. /* The flow type is used identify the current state of the network flow*/
  122. int flow_type;
  123. };
  124. /* The list of function which is being referenced */
  125. extern unsigned int check_ncm_flag(void);
  126. extern void knox_collect_conntrack_data(struct nf_conn *ct, int startStop, int where);
  127. extern bool kfifo_status(void);
  128. extern void insert_data_kfifo_kthread(struct knox_socket_metadata* knox_socket_metadata);
  129. extern unsigned int check_intermediate_flag(void);
  130. extern unsigned int get_intermediate_timeout(void);
  131. /* Debug */
  132. #define NCM_DEBUG 1
  133. #if NCM_DEBUG
  134. #define NCM_LOGD(...) printk("ncm: "__VA_ARGS__)
  135. #else
  136. #define NCM_LOGD(...)
  137. #endif /* NCM_DEBUG */
  138. #define NCM_LOGE(...) printk("ncm: "__VA_ARGS__)
  139. /* IOCTL definitions*/
  140. #define __NCMIOC 0x120
  141. #define NCM_ACTIVATED_OPEN _IO(__NCMIOC, 2)
  142. #define NCM_DEACTIVATED _IO(__NCMIOC, 4)
  143. #define NCM_ACTIVATED_CLOSE _IO(__NCMIOC, 8)
  144. #define NCM_ACTIVATED_ALL _IO(__NCMIOC, 16)
  145. #define NCM_GETVERSION _IO(__NCMIOC, 32)
  146. #define NCM_MATCH_VERSION _IO(__NCMIOC, 64)
  147. #endif
  148. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA }