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

Linux/Unix编程

开发平台:

Unix_Linux

  1. #include <linux/types.h>
  2. #include <linux/sched.h>
  3. #include <linux/timer.h>
  4. #include <linux/netfilter.h>
  5. #include <linux/in.h>
  6. #include <linux/udp.h>
  7. #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
  8. #define UDP_TIMEOUT (30*HZ)
  9. #define UDP_STREAM_TIMEOUT (180*HZ)
  10. static int udp_pkt_to_tuple(const void *datah, size_t datalen,
  11.     struct ip_conntrack_tuple *tuple)
  12. {
  13. const struct udphdr *hdr = datah;
  14. tuple->src.u.udp.port = hdr->source;
  15. tuple->dst.u.udp.port = hdr->dest;
  16. return 1;
  17. }
  18. static int udp_invert_tuple(struct ip_conntrack_tuple *tuple,
  19.     const struct ip_conntrack_tuple *orig)
  20. {
  21. tuple->src.u.udp.port = orig->dst.u.udp.port;
  22. tuple->dst.u.udp.port = orig->src.u.udp.port;
  23. return 1;
  24. }
  25. /* Print out the per-protocol part of the tuple. */
  26. static unsigned int udp_print_tuple(char *buffer,
  27.     const struct ip_conntrack_tuple *tuple)
  28. {
  29. return sprintf(buffer, "sport=%hu dport=%hu ",
  30.        ntohs(tuple->src.u.udp.port),
  31.        ntohs(tuple->dst.u.udp.port));
  32. }
  33. /* Print out the private part of the conntrack. */
  34. static unsigned int udp_print_conntrack(char *buffer,
  35. const struct ip_conntrack *conntrack)
  36. {
  37. return 0;
  38. }
  39. /* Returns verdict for packet, and may modify conntracktype */
  40. static int udp_packet(struct ip_conntrack *conntrack,
  41.       struct iphdr *iph, size_t len,
  42.       enum ip_conntrack_info conntrackinfo)
  43. {
  44. /* If we've seen traffic both ways, this is some kind of UDP
  45.    stream.  Extend timeout. */
  46. if (conntrack->status & IPS_SEEN_REPLY) {
  47. ip_ct_refresh(conntrack, UDP_STREAM_TIMEOUT);
  48. /* Also, more likely to be important, and not a probe */
  49. set_bit(IPS_ASSURED_BIT, &conntrack->status);
  50. } else
  51. ip_ct_refresh(conntrack, UDP_TIMEOUT);
  52. return NF_ACCEPT;
  53. }
  54. /* Called when a new connection for this protocol found. */
  55. static int udp_new(struct ip_conntrack *conntrack,
  56.      struct iphdr *iph, size_t len)
  57. {
  58. return 1;
  59. }
  60. struct ip_conntrack_protocol ip_conntrack_protocol_udp
  61. = { { NULL, NULL }, IPPROTO_UDP, "udp",
  62.     udp_pkt_to_tuple, udp_invert_tuple, udp_print_tuple, udp_print_conntrack,
  63.     udp_packet, udp_new, NULL, NULL, NULL };