ip_conntrack_netbios_ns.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * NetBIOS name service broadcast connection tracking helper
  3. *
  4. * (c) 2005 Patrick McHardy <kaber@trash.net>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. /*
  12. * This helper tracks locally originating NetBIOS name service
  13. * requests by issuing permanent expectations (valid until
  14. * timing out) matching all reply connections from the
  15. * destination network. The only NetBIOS specific thing is
  16. * actually the port number.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/inetdevice.h>
  24. #include <linux/if_addr.h>
  25. #include <linux/in.h>
  26. #include <linux/ip.h>
  27. #include <net/route.h>
  28. #include <linux/netfilter.h>
  29. #include <linux/netfilter_ipv4.h>
  30. #include <linux/netfilter_ipv4/ip_conntrack.h>
  31. #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
  32. #define NMBD_PORT 137
  33. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  34. MODULE_DESCRIPTION("NetBIOS name service broadcast connection tracking helper");
  35. MODULE_LICENSE("GPL");
  36. static unsigned int timeout = 3;
  37. module_param(timeout, uint, 0400);
  38. MODULE_PARM_DESC(timeout, "timeout for master connection/replies in seconds");
  39. static int help(struct sk_buff **pskb,
  40. struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
  41. {
  42. struct ip_conntrack_expect *exp;
  43. struct iphdr *iph = (*pskb)->nh.iph;
  44. struct rtable *rt = (struct rtable *)(*pskb)->dst;
  45. struct in_device *in_dev;
  46. __be32 mask = 0;
  47. /* we're only interested in locally generated packets */
  48. if ((*pskb)->sk == NULL)
  49. goto out;
  50. if (rt == NULL || !(rt->rt_flags & RTCF_BROADCAST))
  51. goto out;
  52. if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
  53. goto out;
  54. rcu_read_lock();
  55. in_dev = __in_dev_get_rcu(rt->u.dst.dev);
  56. if (in_dev != NULL) {
  57. for_primary_ifa(in_dev) {
  58. if (ifa->ifa_broadcast == iph->daddr) {
  59. mask = ifa->ifa_mask;
  60. break;
  61. }
  62. } endfor_ifa(in_dev);
  63. }
  64. rcu_read_unlock();
  65. if (mask == 0)
  66. goto out;
  67. exp = ip_conntrack_expect_alloc(ct);
  68. if (exp == NULL)
  69. goto out;
  70. exp->tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
  71. exp->tuple.src.u.udp.port = htons(NMBD_PORT);
  72. exp->mask.src.ip = mask;
  73. exp->mask.src.u.udp.port = htons(0xFFFF);
  74. exp->mask.dst.ip = htonl(0xFFFFFFFF);
  75. exp->mask.dst.u.udp.port = htons(0xFFFF);
  76. exp->mask.dst.protonum = 0xFF;
  77. exp->expectfn = NULL;
  78. exp->flags = IP_CT_EXPECT_PERMANENT;
  79. ip_conntrack_expect_related(exp);
  80. ip_conntrack_expect_put(exp);
  81. ip_ct_refresh(ct, *pskb, timeout * HZ);
  82. out:
  83. return NF_ACCEPT;
  84. }
  85. static struct ip_conntrack_helper helper = {
  86. .name = "netbios-ns",
  87. .tuple = {
  88. .src = {
  89. .u = {
  90. .udp = {
  91. .port = __constant_htons(NMBD_PORT),
  92. }
  93. }
  94. },
  95. .dst = {
  96. .protonum = IPPROTO_UDP,
  97. },
  98. },
  99. .mask = {
  100. .src = {
  101. .u = {
  102. .udp = {
  103. .port = __constant_htons(0xFFFF),
  104. }
  105. }
  106. },
  107. .dst = {
  108. .protonum = 0xFF,
  109. },
  110. },
  111. .max_expected = 1,
  112. .me = THIS_MODULE,
  113. .help = help,
  114. };
  115. static int __init ip_conntrack_netbios_ns_init(void)
  116. {
  117. helper.timeout = timeout;
  118. return ip_conntrack_helper_register(&helper);
  119. }
  120. static void __exit ip_conntrack_netbios_ns_fini(void)
  121. {
  122. ip_conntrack_helper_unregister(&helper);
  123. }
  124. module_init(ip_conntrack_netbios_ns_init);
  125. module_exit(ip_conntrack_netbios_ns_fini);