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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* IP tables module for matching the value of the IPv4 and TCP ECN bits
  2.  *
  3.  * ipt_ecn.c,v 1.3 2002/05/29 15:09:00 laforge Exp
  4.  *
  5.  * (C) 2002 by Harald Welte <laforge@gnumonks.org>
  6.  *
  7.  * This software is distributed under the terms GNU GPL v2
  8.  */
  9. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/tcp.h>
  12. #include <linux/netfilter_ipv4/ip_tables.h>
  13. #include <linux/netfilter_ipv4/ipt_ecn.h>
  14. MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
  15. MODULE_DESCRIPTION("IP tables ECN matching module");
  16. MODULE_LICENSE("GPL");
  17. static inline int match_ip(const struct sk_buff *skb,
  18.    const struct iphdr *iph,
  19.    const struct ipt_ecn_info *einfo)
  20. {
  21. return ((iph->tos&IPT_ECN_IP_MASK) == einfo->ip_ect);
  22. }
  23. static inline int match_tcp(const struct sk_buff *skb,
  24.     const struct iphdr *iph,
  25.     const struct ipt_ecn_info *einfo)
  26. {
  27. struct tcphdr *tcph = (void *)iph + iph->ihl*4;
  28. if (einfo->operation & IPT_ECN_OP_MATCH_ECE) {
  29. if (einfo->invert & IPT_ECN_OP_MATCH_ECE) {
  30. if (tcph->ece == 1)
  31. return 0;
  32. } else {
  33. if (tcph->ece == 0)
  34. return 0;
  35. }
  36. }
  37. if (einfo->operation & IPT_ECN_OP_MATCH_CWR) {
  38. if (einfo->invert & IPT_ECN_OP_MATCH_CWR) {
  39. if (tcph->cwr == 1)
  40. return 0;
  41. } else {
  42. if (tcph->cwr == 0)
  43. return 0;
  44. }
  45. }
  46. return 1;
  47. }
  48. static int match(const struct sk_buff *skb, const struct net_device *in,
  49.  const struct net_device *out, const void *matchinfo,
  50.  int offset, const void *hdr, u_int16_t datalen,
  51.  int *hotdrop)
  52. {
  53. const struct ipt_ecn_info *info = matchinfo;
  54. const struct iphdr *iph = skb->nh.iph;
  55. if (info->operation & IPT_ECN_OP_MATCH_IP)
  56. if (!match_ip(skb, iph, info))
  57. return 0;
  58. if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)) {
  59. if (iph->protocol != IPPROTO_TCP)
  60. return 0;
  61. if (!match_tcp(skb, iph, info))
  62. return 0;
  63. }
  64. return 1;
  65. }
  66. static int checkentry(const char *tablename, const struct ipt_ip *ip,
  67.       void *matchinfo, unsigned int matchsize,
  68.       unsigned int hook_mask)
  69. {
  70. const struct ipt_ecn_info *info = matchinfo;
  71. if (matchsize != IPT_ALIGN(sizeof(struct ipt_ecn_info)))
  72. return 0;
  73. if (info->operation & IPT_ECN_OP_MATCH_MASK)
  74. return 0;
  75. if (info->invert & IPT_ECN_OP_MATCH_MASK)
  76. return 0;
  77. if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)
  78.     && ip->proto != IPPROTO_TCP) {
  79. printk(KERN_WARNING "ipt_ecn: can't match TCP bits in rule for"
  80.        " non-tcp packetsn");
  81. return 0;
  82. }
  83. return 1;
  84. }
  85. static struct ipt_match ecn_match = { { NULL, NULL }, "ecn", &match,
  86. &checkentry, NULL, THIS_MODULE };
  87. static int __init init(void)
  88. {
  89. return ipt_register_match(&ecn_match);
  90. }
  91. static void __exit fini(void)
  92. {
  93. ipt_unregister_match(&ecn_match);
  94. }
  95. module_init(init);
  96. module_exit(fini);