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