ip_conntrack_proto_udp.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* (C) 1999-2001 Paul `Rusty' Russell
  2. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/types.h>
  9. #include <linux/timer.h>
  10. #include <linux/netfilter.h>
  11. #include <linux/in.h>
  12. #include <linux/ip.h>
  13. #include <linux/udp.h>
  14. #include <linux/seq_file.h>
  15. #include <net/checksum.h>
  16. #include <linux/netfilter_ipv4.h>
  17. #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
  18. unsigned int ip_ct_udp_timeout __read_mostly = 30*HZ;
  19. unsigned int ip_ct_udp_timeout_stream __read_mostly = 180*HZ;
  20. static int udp_pkt_to_tuple(const struct sk_buff *skb,
  21. unsigned int dataoff,
  22. struct ip_conntrack_tuple *tuple)
  23. {
  24. struct udphdr _hdr, *hp;
  25. /* Actually only need first 8 bytes. */
  26. hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
  27. if (hp == NULL)
  28. return 0;
  29. tuple->src.u.udp.port = hp->source;
  30. tuple->dst.u.udp.port = hp->dest;
  31. return 1;
  32. }
  33. static int udp_invert_tuple(struct ip_conntrack_tuple *tuple,
  34. const struct ip_conntrack_tuple *orig)
  35. {
  36. tuple->src.u.udp.port = orig->dst.u.udp.port;
  37. tuple->dst.u.udp.port = orig->src.u.udp.port;
  38. return 1;
  39. }
  40. /* Print out the per-protocol part of the tuple. */
  41. static int udp_print_tuple(struct seq_file *s,
  42. const struct ip_conntrack_tuple *tuple)
  43. {
  44. return seq_printf(s, "sport=%hu dport=%hu ",
  45. ntohs(tuple->src.u.udp.port),
  46. ntohs(tuple->dst.u.udp.port));
  47. }
  48. /* Print out the private part of the conntrack. */
  49. static int udp_print_conntrack(struct seq_file *s,
  50. const struct ip_conntrack *conntrack)
  51. {
  52. return 0;
  53. }
  54. /* Returns verdict for packet, and may modify conntracktype */
  55. static int udp_packet(struct ip_conntrack *conntrack,
  56. const struct sk_buff *skb,
  57. enum ip_conntrack_info ctinfo)
  58. {
  59. /* If we've seen traffic both ways, this is some kind of UDP
  60. stream. Extend timeout. */
  61. if (test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
  62. ip_ct_refresh_acct(conntrack, ctinfo, skb,
  63. ip_ct_udp_timeout_stream);
  64. /* Also, more likely to be important, and not a probe */
  65. if (!test_and_set_bit(IPS_ASSURED_BIT, &conntrack->status))
  66. ip_conntrack_event_cache(IPCT_STATUS, skb);
  67. } else
  68. ip_ct_refresh_acct(conntrack, ctinfo, skb, ip_ct_udp_timeout);
  69. return NF_ACCEPT;
  70. }
  71. /* Called when a new connection for this protocol found. */
  72. static int udp_new(struct ip_conntrack *conntrack, const struct sk_buff *skb)
  73. {
  74. return 1;
  75. }
  76. static int udp_error(struct sk_buff *skb, enum ip_conntrack_info *ctinfo,
  77. unsigned int hooknum)
  78. {
  79. struct iphdr *iph = skb->nh.iph;
  80. unsigned int udplen = skb->len - iph->ihl * 4;
  81. struct udphdr _hdr, *hdr;
  82. /* Header is too small? */
  83. hdr = skb_header_pointer(skb, iph->ihl*4, sizeof(_hdr), &_hdr);
  84. if (hdr == NULL) {
  85. if (LOG_INVALID(IPPROTO_UDP))
  86. nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
  87. "ip_ct_udp: short packet ");
  88. return -NF_ACCEPT;
  89. }
  90. /* Truncated/malformed packets */
  91. if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) {
  92. if (LOG_INVALID(IPPROTO_UDP))
  93. nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
  94. "ip_ct_udp: truncated/malformed packet ");
  95. return -NF_ACCEPT;
  96. }
  97. /* Packet with no checksum */
  98. if (!hdr->check)
  99. return NF_ACCEPT;
  100. /* Checksum invalid? Ignore.
  101. * We skip checking packets on the outgoing path
  102. * because the checksum is assumed to be correct.
  103. * FIXME: Source route IP option packets --RR */
  104. if (ip_conntrack_checksum && hooknum == NF_IP_PRE_ROUTING &&
  105. nf_ip_checksum(skb, hooknum, iph->ihl * 4, IPPROTO_UDP)) {
  106. if (LOG_INVALID(IPPROTO_UDP))
  107. nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
  108. "ip_ct_udp: bad UDP checksum ");
  109. return -NF_ACCEPT;
  110. }
  111. return NF_ACCEPT;
  112. }
  113. struct ip_conntrack_protocol ip_conntrack_protocol_udp =
  114. {
  115. .proto = IPPROTO_UDP,
  116. .name = "udp",
  117. .pkt_to_tuple = udp_pkt_to_tuple,
  118. .invert_tuple = udp_invert_tuple,
  119. .print_tuple = udp_print_tuple,
  120. .print_conntrack = udp_print_conntrack,
  121. .packet = udp_packet,
  122. .new = udp_new,
  123. .error = udp_error,
  124. #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
  125. defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
  126. .tuple_to_nfattr = ip_ct_port_tuple_to_nfattr,
  127. .nfattr_to_tuple = ip_ct_port_nfattr_to_tuple,
  128. #endif
  129. };