Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. #include <linux/module.h> /* Needed by all modules */
  2. #include <linux/kernel.h> /* Needed for KERN_ALERT */
  3. #include <linux/netfilter.h>
  4. #include <linux/netfilter_ipv4.h>
  5. #include <linux/skbuff.h>
  6. #include <linux/compiler.h>
  7. #include <net/tcp.h>
  8. #include <linux/namei.h>
  9. #include <linux/proc_fs.h>
  10. #include <linux/slab.h>
  11. #include <asm/uaccess.h>
  12. #include <linux/list.h>
  13.  
  14. MODULE_AUTHOR ("Eike Ritter <E.Ritter@cs.bham.ac.uk>");
  15. MODULE_DESCRIPTION ("Extensions to the firewall") ;
  16. MODULE_LICENSE("GPL");
  17.  
  18. #define BUFFERLENGTH 256
  19. #define LIST_RULES 'L'
  20.  
  21. #define PROC_ENTRY_FILENAME "firewallExtension"
  22.  
  23. DECLARE_RWSEM(file_access); /* semaphore to protect counter access */
  24.  
  25. static struct proc_dir_entry *Our_Proc_File;
  26.  
  27.  
  28. /* make IP4-addresses readable */
  29.  
  30. #define NIPQUAD(addr) \
  31. ((unsigned char *)&addr)[0], \
  32. ((unsigned char *)&addr)[1], \
  33. ((unsigned char *)&addr)[2], \
  34. ((unsigned char *)&addr)[3]
  35.  
  36.  
  37. struct nf_hook_ops *reg;
  38.  
  39. struct rules_list{
  40. char* rule;
  41. struct list_head list;
  42. };
  43. struct rules_list storedRules;
  44. //firewall part
  45. unsigned int FirewallExtensionHook (const struct nf_hook_ops *ops,
  46. struct sk_buff *skb,
  47. const struct net_device *in,
  48. const struct net_device *out,
  49. int (*okfn)(struct sk_buff *)) {
  50.  
  51. struct tcphdr *tcp;
  52. struct tcphdr _tcph;
  53. struct sock *sk;
  54.  
  55.  
  56. sk = skb->sk;
  57. if (!sk) {
  58. printk (KERN_INFO "firewall: netfilter called with empty socket!\n");;
  59. return NF_ACCEPT;
  60. }
  61.  
  62. if (sk->sk_protocol != IPPROTO_TCP) {
  63. printk (KERN_INFO "firewall: netfilter called with non-TCP-packet.\n");
  64. return NF_ACCEPT;
  65. }
  66.  
  67.  
  68.  
  69. /* get the tcp-header for the packet */
  70. tcp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(struct tcphdr), &_tcph);
  71. if (!tcp) {
  72. printk (KERN_INFO "Could not get tcp-header!\n");
  73. return NF_ACCEPT;
  74. }
  75. if (tcp->syn) {
  76. struct iphdr *ip;
  77.  
  78. printk (KERN_INFO "firewall: Starting connection \n");
  79. ip = ip_hdr (skb);
  80. if (!ip) {
  81. printk (KERN_INFO "firewall: Cannot get IP header!\n!");
  82. }
  83. else {
  84. printk (KERN_INFO "firewall: Destination address = %u.%u.%u.%u\n", NIPQUAD(ip->daddr));
  85. }
  86. printk (KERN_INFO "firewall: destination port = %d\n", ntohs(tcp->dest));
  87.  
  88. //check user context, check current process name
  89. //then check for rules
  90.  
  91. if (in_irq() || in_softirq()) {
  92. printk (KERN_INFO "Not in user context - retry packet\n");
  93. return NF_ACCEPT;
  94. }
  95.  
  96.  
  97. if (ntohs (tcp->dest) == 80) {
  98. tcp_done (sk); /* terminate connection immediately */
  99. printk (KERN_INFO "Connection shut down\n");
  100. return NF_DROP;
  101. }
  102. }
  103. return NF_ACCEPT;
  104. }
  105.  
  106. EXPORT_SYMBOL (FirewallExtensionHook);
  107.  
  108. static struct nf_hook_ops firewallExtension_ops = {
  109. .hook = FirewallExtensionHook,
  110. .pf = PF_INET,
  111. .priority = NF_IP_PRI_FIRST,
  112. .hooknum = NF_INET_LOCAL_OUT
  113. };
  114.  
  115. //interface part
  116.  
  117. ssize_t kernelWrite (struct file *file, const char __user *buffer, size_t length, loff_t *offset) {
  118. int bytes_read = 0;
  119. struct rules_list* tmp;
  120. char* tempMsg;
  121. char* msgPtr;
  122. if(length <1){
  123. return -EINVAL;
  124. }
  125. printk(KERN_INFO "Length is %zu.\n",length);
  126. //get data - store in list
  127. //if L list
  128. //else add a rule
  129. tempMsg = (char*)kmalloc(length,GFP_KERNEL);
  130. msgPtr = tempMsg;
  131. while(length && bytes_read < BUFFERLENGTH){
  132. get_user(*(msgPtr++),buffer++);
  133. length--;
  134. bytes_read++;
  135. }
  136.  
  137. // do a reset mechanic for new rule set L:
  138.  
  139. printk(KERN_INFO "Added - %c.\n",tempMsg[0]);
  140. if(*tempMsg == 'L'){
  141. //print
  142. kfree(tempMsg);
  143. printk(KERN_INFO "WE FREE");
  144. }else{// doing L does below.. ?
  145. //assume its one line due to correct entry.
  146. tmp = (struct rules_list *)kmalloc(sizeof(struct rules_list),GFP_KERNEL);
  147. tmp->rule = tempMsg;
  148. down_write(&file_access);
  149. //create and add.
  150. list_add(&(tmp->list),&(storedRules.list));
  151.  
  152. up_write(&file_access);
  153. printk(KERN_INFO "Added New Rule Succesfully");
  154.  
  155. //printk(KERN_INFO *tempMsg);
  156.  
  157. }
  158. printk(KERN_INFO "Read %d bytes.\n",bytes_read);
  159. return bytes_read;
  160. }
  161.  
  162.  
  163. int procfs_open(struct inode *inode, struct file *file)
  164. {
  165. printk (KERN_INFO "firewallExtension-PROCFS opened\n");
  166. try_module_get(THIS_MODULE);
  167. return 0;
  168. }
  169.  
  170. int procfs_close(struct inode *inode, struct file *file)
  171. {
  172. printk (KERN_INFO "firewallExtension-PROCFS closed\n");
  173. module_put(THIS_MODULE);
  174. return 0; /* success */
  175. }
  176.  
  177. const struct file_operations File_Ops_4_Our_Proc_File = {
  178. .owner = THIS_MODULE,
  179. .write = kernelWrite,
  180. .open = procfs_open,
  181. .release = procfs_close,
  182. };
  183.  
  184.  
  185. int init_module(void)
  186. {
  187.  
  188. int errno;
  189.  
  190. errno = nf_register_hook (&firewallExtension_ops); /* register the hook */
  191. if (errno) {
  192. printk (KERN_INFO "Firewall extension could not be registered!\n");
  193. }
  194. else {
  195. printk(KERN_INFO "Firewall extensions module loaded\n");
  196. }
  197.  
  198. //procfile
  199. Our_Proc_File = proc_create_data (PROC_ENTRY_FILENAME, 0644, NULL, &File_Ops_4_Our_Proc_File, NULL);
  200.  
  201. /* check if the /proc file was created successfuly */
  202. if (Our_Proc_File == NULL){
  203. printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
  204. PROC_ENTRY_FILENAME);
  205. return -ENOMEM;
  206. }
  207.  
  208. printk(KERN_INFO "/proc/%s created\n", PROC_ENTRY_FILENAME);
  209.  
  210. INIT_LIST_HEAD(&storedRules.list); // right or wrong?
  211.  
  212. // A non 0 return means init_module failed; module can't be loaded.
  213. return errno;
  214. }
  215.  
  216.  
  217. void cleanup_module(void)
  218. {
  219. struct rules_list *tmp;
  220. struct list_head *pos,*q;
  221. remove_proc_entry(PROC_ENTRY_FILENAME, NULL);
  222. nf_unregister_hook (&firewallExtension_ops); /* restore everything to normal */
  223. printk(KERN_INFO "/proc/%s removed\n", PROC_ENTRY_FILENAME);
  224.  
  225. //clean the list
  226.  
  227. list_for_each_safe(pos,q,&storedRules.list){
  228. tmp =list_entry(pos,struct rules_list,list);
  229. list_del(pos);
  230. kfree(tmp->rule);
  231. kfree(tmp);
  232. }
  233.  
  234.  
  235. printk(KERN_INFO "Firewall extensions module unloaded\n");
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement