ip6t_owner.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:3k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* Kernel module to match various things tied to sockets associated with
  2.    locally generated outgoing packets.
  3.    Copyright (C) 2000,2001 Marc Boucher
  4.  */
  5. #include <linux/module.h>
  6. #include <linux/skbuff.h>
  7. #include <linux/file.h>
  8. #include <net/sock.h>
  9. #include <linux/netfilter_ipv6/ip6t_owner.h>
  10. #include <linux/netfilter_ipv6/ip6_tables.h>
  11. MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
  12. MODULE_DESCRIPTION("IP6 tables owner matching module");
  13. MODULE_LICENSE("GPL");
  14. static int
  15. match_pid(const struct sk_buff *skb, pid_t pid)
  16. {
  17. struct task_struct *p;
  18. struct files_struct *files;
  19. int i;
  20. read_lock(&tasklist_lock);
  21. p = find_task_by_pid(pid);
  22. if (!p)
  23. goto out;
  24. task_lock(p);
  25. files = p->files;
  26. if(files) {
  27. read_lock(&files->file_lock);
  28. for (i=0; i < files->max_fds; i++) {
  29. if (fcheck_files(files, i) == skb->sk->socket->file) {
  30. read_unlock(&files->file_lock);
  31. task_unlock(p);
  32. read_unlock(&tasklist_lock);
  33. return 1;
  34. }
  35. }
  36. read_unlock(&files->file_lock);
  37. }
  38. task_unlock(p);
  39. out:
  40. read_unlock(&tasklist_lock);
  41. return 0;
  42. }
  43. static int
  44. match_sid(const struct sk_buff *skb, pid_t sid)
  45. {
  46. struct task_struct *p;
  47. struct file *file = skb->sk->socket->file;
  48. int i, found=0;
  49. read_lock(&tasklist_lock);
  50. for_each_task(p) {
  51. struct files_struct *files;
  52. if (p->session != sid)
  53. continue;
  54. task_lock(p);
  55. files = p->files;
  56. if (files) {
  57. read_lock(&files->file_lock);
  58. for (i=0; i < files->max_fds; i++) {
  59. if (fcheck_files(files, i) == file) {
  60. found = 1;
  61. break;
  62. }
  63. }
  64. read_unlock(&files->file_lock);
  65. }
  66. task_unlock(p);
  67. if(found)
  68. break;
  69. }
  70. read_unlock(&tasklist_lock);
  71. return found;
  72. }
  73. static int
  74. match(const struct sk_buff *skb,
  75.       const struct net_device *in,
  76.       const struct net_device *out,
  77.       const void *matchinfo,
  78.       int offset,
  79.       const void *hdr,
  80.       u_int16_t datalen,
  81.       int *hotdrop)
  82. {
  83. const struct ip6t_owner_info *info = matchinfo;
  84. if (!skb->sk || !skb->sk->socket || !skb->sk->socket->file)
  85. return 0;
  86. if(info->match & IP6T_OWNER_UID) {
  87. if((skb->sk->socket->file->f_uid != info->uid) ^
  88.     !!(info->invert & IP6T_OWNER_UID))
  89. return 0;
  90. }
  91. if(info->match & IP6T_OWNER_GID) {
  92. if((skb->sk->socket->file->f_gid != info->gid) ^
  93.     !!(info->invert & IP6T_OWNER_GID))
  94. return 0;
  95. }
  96. if(info->match & IP6T_OWNER_PID) {
  97. if (!match_pid(skb, info->pid) ^
  98.     !!(info->invert & IP6T_OWNER_PID))
  99. return 0;
  100. }
  101. if(info->match & IP6T_OWNER_SID) {
  102. if (!match_sid(skb, info->sid) ^
  103.     !!(info->invert & IP6T_OWNER_SID))
  104. return 0;
  105. }
  106. return 1;
  107. }
  108. static int
  109. checkentry(const char *tablename,
  110.            const struct ip6t_ip6 *ip,
  111.            void *matchinfo,
  112.            unsigned int matchsize,
  113.            unsigned int hook_mask)
  114. {
  115.         if (hook_mask
  116.             & ~((1 << NF_IP6_LOCAL_OUT) | (1 << NF_IP6_POST_ROUTING))) {
  117.                 printk("ip6t_owner: only valid for LOCAL_OUT or POST_ROUTING.n");
  118.                 return 0;
  119.         }
  120. if (matchsize != IP6T_ALIGN(sizeof(struct ip6t_owner_info)))
  121. return 0;
  122. return 1;
  123. }
  124. static struct ip6t_match owner_match
  125. = { { NULL, NULL }, "owner", &match, &checkentry, NULL, THIS_MODULE };
  126. static int __init init(void)
  127. {
  128. return ip6t_register_match(&owner_match);
  129. }
  130. static void __exit fini(void)
  131. {
  132. ip6t_unregister_match(&owner_match);
  133. }
  134. module_init(init);
  135. module_exit(fini);