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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* Kernel module to match one of a list of TCP/UDP ports: ports are in
  2.    the same place so we can treat them as equal. */
  3. #include <linux/module.h>
  4. #include <linux/types.h>
  5. #include <linux/udp.h>
  6. #include <linux/skbuff.h>
  7. #include <linux/netfilter_ipv4/ipt_multiport.h>
  8. #include <linux/netfilter_ipv4/ip_tables.h>
  9. #if 0
  10. #define duprintf(format, args...) printk(format , ## args)
  11. #else
  12. #define duprintf(format, args...)
  13. #endif
  14. /* Returns 1 if the port is matched by the test, 0 otherwise. */
  15. static inline int
  16. ports_match(const u_int16_t *portlist, enum ipt_multiport_flags flags,
  17.     u_int8_t count, u_int16_t src, u_int16_t dst)
  18. {
  19. unsigned int i;
  20. for (i=0; i<count; i++) {
  21. if (flags != IPT_MULTIPORT_DESTINATION
  22.     && portlist[i] == src)
  23. return 1;
  24. if (flags != IPT_MULTIPORT_SOURCE
  25.     && portlist[i] == dst)
  26. return 1;
  27. }
  28. return 0;
  29. }
  30. static int
  31. match(const struct sk_buff *skb,
  32.       const struct net_device *in,
  33.       const struct net_device *out,
  34.       const void *matchinfo,
  35.       int offset,
  36.       const void *hdr,
  37.       u_int16_t datalen,
  38.       int *hotdrop)
  39. {
  40. const struct udphdr *udp = hdr;
  41. const struct ipt_multiport *multiinfo = matchinfo;
  42. /* Must be big enough to read ports. */
  43. if (offset == 0 && datalen < sizeof(struct udphdr)) {
  44. /* We've been asked to examine this packet, and we
  45.    can't.  Hence, no choice but to drop. */
  46. duprintf("ipt_multiport:"
  47.  " Dropping evil offset=0 tinygram.n");
  48. *hotdrop = 1;
  49. return 0;
  50. }
  51. /* Must not be a fragment. */
  52. return !offset
  53. && ports_match(multiinfo->ports,
  54.        multiinfo->flags, multiinfo->count,
  55.        ntohs(udp->source), ntohs(udp->dest));
  56. }
  57. /* Called when user tries to insert an entry of this type. */
  58. static int
  59. checkentry(const char *tablename,
  60.    const struct ipt_ip *ip,
  61.    void *matchinfo,
  62.    unsigned int matchsize,
  63.    unsigned int hook_mask)
  64. {
  65. const struct ipt_multiport *multiinfo = matchinfo;
  66. if (matchsize != IPT_ALIGN(sizeof(struct ipt_multiport)))
  67. return 0;
  68. /* Must specify proto == TCP/UDP, no unknown flags or bad count */
  69. return (ip->proto == IPPROTO_TCP || ip->proto == IPPROTO_UDP)
  70. && !(ip->flags & IPT_INV_PROTO)
  71. && matchsize == IPT_ALIGN(sizeof(struct ipt_multiport))
  72. && (multiinfo->flags == IPT_MULTIPORT_SOURCE
  73.     || multiinfo->flags == IPT_MULTIPORT_DESTINATION
  74.     || multiinfo->flags == IPT_MULTIPORT_EITHER)
  75. && multiinfo->count <= IPT_MULTI_PORTS;
  76. }
  77. static struct ipt_match multiport_match
  78. = { { NULL, NULL }, "multiport", &match, &checkentry, NULL, THIS_MODULE };
  79. static int __init init(void)
  80. {
  81. return ipt_register_match(&multiport_match);
  82. }
  83. static void __exit fini(void)
  84. {
  85. ipt_unregister_match(&multiport_match);
  86. }
  87. module_init(init);
  88. module_exit(fini);
  89. MODULE_LICENSE("GPL");